# lemalcs/khernet architecture

> Architecture diagram of the GitHub repository lemalcs/khernet, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/lemalcs/khernet
- Repository: https://github.com/lemalcs/khernet

## Overview

##### Main Components
- Database Layer:
– There are two main database folders (CONFIG and KH) that contain scripts for creating the database, generators, stored procedures, tables, and triggers. This clearly represents the persistence and configuration aspects of the application.
- Core Engine (Khernet.Core):
– Sub–modules here include:
▪ Common: Contains configuration files, constants, and basic utilities.
▪ Container: Likely implements an IoC (dependency injection) container to manage dependencies.
▪ Data: Manages database interaction using data models and possibly the account/communication data.
▪ Entity: Represents core business entities such as messages, peers, and processing results.
▪ Host: Manages the application’s hosting environment and services like authentication and peer watching.
▪ Processor: Implements the main business logic for handling messaging, file transfers, notifications, and uses various manager classes.
▪ Utility: Provides helper functions for encryption, compression, file handling, logging, etc.
- Services Layer:
– Found in Khernet.Core/Services; it’s broken down into several projects:
▪ A client component which manages outbound communications (e.g., CertificateValidator, CommunicatorClient, GatewayClient).
▪ Common, Contracts, Messages, and a WCF–based service layer indicate how remote calls and inter–peer messaging are handled (likely using WCF and custom service inspectors).
- User Interface (Khernet.UI):
– Contains both container and presentation projects.
– The presentation layer includes a host of XAML pages, dialog controls, converters, view models, and other components to deliver a rich WPF user interface.
– There’s evidence of MVVM structure (ViewModels, Views, and IoC for dependency management), which segregates UI concerns from business logic.
- Installer:
– The Khernet.Installer folder with NSIS or Inno Setup configurations indicates the mechanism to deploy and install the application in “installed mode.”
- Resources/Dependencies:
– Bundled packages like nuget executables, Firebird database client, and other libraries are provided in the Resources folder and represent third–party or packaging dependencies.

##### Relationships and Interactions
- The UI Layer interacts with the Core Engine via ViewModels and IoC interfaces (see the IoC folders) – using patterns such as dependency injection and observer patterns.
- The Core Engine (especially the Processor and Host modules) works closely with the Services Layer to handle communication between peers, manage encryption, and process messages.
- The Core Engine also communicates with the Database Layer by executing SQL scripts stored in the CONFIG and KH folders, managing data persistence (like saving messages, peer identities, notifications, etc.).
- The Services Layer (for example, WCF services) facilitates communication for file transfers, notifications, and other multi–peer interactions, acting as the glue between the UI and underlying messaging infrastructure.
- The Installer component packages and deploys the overall system for end users and distinguishes between portable and installed modes.

##### Architectural Patterns and Design Principles
- Layered Architecture: Clearly differentiate between the Data/Database layer, Business Logic/Core Engine, Service Communication, and the UI.
- Modular/Component–Based Design: Each sub–module (Common, Container, Data, Host, Processor, Utility) encapsulates specific responsibilities.
- IoC/Dependency Injection: Evident from the “Container” and IoC folders that help decouple components for easier testing and enhanced scalability.
- MVVM Pattern: The UI project follows MVVM, separating UI presentation (XAML) from business logic (ViewModels).
- Peer–to–Peer Communication: Built to operate without a centralized server by using decentralized service components.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Interface Layer
    subgraph "User Interface"
        UI_CONTAINER["UI Container"]:::ui
        UI_PRESENT["UI Presentation"]:::ui
    end

    %% Core Engine Layer
    subgraph "Core Engine"
        COMMON["Common"]:::core
        CONTAINER["Container"]:::core
        DATA["Data"]:::core
        ENTITY["Entity"]:::core
        HOST["Host"]:::core
        PROCESSOR["Processor"]:::core
        UTILITY["Utility"]:::core
    end

    %% Services Layer
    subgraph "Services Layer"
        CLIENT["Client Services"]:::service
        S_COMMON["Common Services"]:::service
        CONTRACTS["Contracts"]:::service
        MESSAGING["Messaging Services"]:::service
        WCF["WCF Services"]:::service
    end

    %% Database Layer
    subgraph "Database Layer"
        CONFIG["CONFIG Scripts"]:::database
        KH["KH Scripts"]:::database
    end

    %% Installer and Resources
    INSTALLER["Installer/Deployment"]:::installer
    RESOURCES["Resources/Dependencies"]:::resources

    %% Interactions
    UI_PRESENT -->|"calls"| HOST
    UI_CONTAINER -->|"initializes"| HOST

    HOST -->|"utilizes"| PROCESSOR
    PROCESSOR <-->|"communicates"| CLIENT
    HOST <-->|"exchanges"| WCF

    DATA -->|"executes"| CONFIG
    DATA -->|"executes"| KH

    RESOURCES -->|"provides"| COMMON
    RESOURCES -->|"provides"| CLIENT
    RESOURCES -->|"supports"| UI_PRESENT

    INSTALLER -->|"deploys"| UI_CONTAINER
    INSTALLER -->|"deploys"| HOST

    %% Styles
    classDef database fill:#f9e79f,stroke:#e67e22,stroke-width:2px;
    classDef core fill:#aed6f1,stroke:#3498db,stroke-width:2px;
    classDef service fill:#d5f5e3,stroke:#27ae60,stroke-width:2px;
    classDef ui fill:#fad7a0,stroke:#f39c12,stroke-width:2px;
    classDef installer fill:#f5b7b1,stroke:#c0392b,stroke-width:2px;
    classDef resources fill:#d2b4de,stroke:#8e44ad,stroke-width:2px;

    %% Click Events
    click CONFIG "https://github.com/lemalcs/khernet/tree/master/Database/CONFIG"
    click KH "https://github.com/lemalcs/khernet/tree/master/Database/KH"
    click COMMON "https://github.com/lemalcs/khernet/blob/master/Khernet.Core/Engine/Khernet.Core.Common"
    click CONTAINER "https://github.com/lemalcs/khernet/blob/master/Khernet.Core/Engine/Khernet.Core.Container"
    click DATA "https://github.com/lemalcs/khernet/blob/master/Khernet.Core/Engine/Khernet.Core.Data"
    click ENTITY "https://github.com/lemalcs/khernet/blob/master/Khernet.Core/Engine/Khernet.Core.Entity"
    click HOST "https://github.com/lemalcs/khernet/blob/master/Khernet.Core/Engine/Khernet.Core.Host"
    click PROCESSOR "https://github.com/lemalcs/khernet/blob/master/Khernet.Core/Engine/Khernet.Core.Processor"
    click UTILITY "https://github.com/lemalcs/khernet/blob/master/Khernet.Core/Engine/Khernet.Core.Utility"
    click CLIENT "https://github.com/lemalcs/khernet/blob/master/Khernet.Core/Services/Khernet.Services.Client"
    click S_COMMON "https://github.com/lemalcs/khernet/blob/master/Khernet.Core/Services/Khernet.Services.Common"
    click CONTRACTS "https://github.com/lemalcs/khernet/blob/master/Khernet.Core/Services/Khernet.Services.Contracts"
    click MESSAGING "https://github.com/lemalcs/khernet/blob/master/Khernet.Core/Services/Khernet.Services.Messages"
    click WCF "https://github.com/lemalcs/khernet/blob/master/Khernet.Core/Services/Khernet.Services.WCF"
    click UI_CONTAINER "https://github.com/lemalcs/khernet/blob/master/Khernet.UI/Khernet.UI.Container"
    click UI_PRESENT "https://github.com/lemalcs/khernet/blob/master/Khernet.UI/Khernet.UI.Presentation"
    click INSTALLER "https://github.com/lemalcs/khernet/blob/master/Khernet.Installer"
    click RESOURCES "https://github.com/lemalcs/khernet/tree/master/Resources"
```
