# gatherai/nmssh architecture

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

- Interactive diagram: https://gitdiagram.com/gatherai/nmssh
- Repository: https://github.com/gatherai/nmssh

## Overview

##### Project Type and Purpose
- This is an open-source, cross-platform (iOS & macOS) Objective-C framework called NMSSH. It wraps the C library libssh2 (and OpenSSL) to provide a clean, high-level SSH/SFTP API.
- The README shows installation via CocoaPods/Carthage, sample usage, and points to example apps and unit tests.

##### File-Structure Analysis
- Top level:
- NMSSH/            → Core Objective-C wrapper classes
- NMSSH.xcodeproj   → Main framework target (OSX & iOS builds)
- NMSSH-iOS/        → iOS-specific project & headers
- NMSSH-OSX/        → OSX example & static libs
- Examples/         → PTYExample Xcode workspace demonstrating usage
- NMSSHTests/       → Unit tests (NMSSHSession, channel, SFTP, etc.)
- NMSSH.podspec     → CocoaPods spec
- Under Libraries/ in each platform folder: precompiled .a binaries of libssh2 + OpenSSL + headers.
- Under NMSSH/Config and NMSSH/Protocols: supporting config classes and delegate protocols.

##### Key Components to Put
A. External Dependencies
– libssh2.a (C library)
– libcrypto.a, libssl.a (OpenSSL)
– System frameworks (CFNetwork, Security)
B. NMSSH Core Framework
– NMSSHSession (connect/authenticate/disconnect)
– NMSSHChannel (exec, shell)
– NMSFTP + NMSFTPFile (upload/download/list)
– NMSSHConfig / NMSSHHostConfig / socket_helper
– NMSSHLogger
– Delegate protocols: NMSSHSessionDelegate, NMSSHChannelDelegate
C. Consumers
– Example apps (PTYExample)
– Third-party clients via CocoaPods/Carthage
– Unit tests (invoking NMSSHSession, Channel, SFTP)
D. External Service
– SSH Server over the network

##### Relationships & Data Flows
- The client app calls NMSSHSession.connect→NMSSHSession.authenticate→session.channel.execute/upload…
- NMSSHSession and NMSSHChannel delegate to libssh2 via socket_helper.
- libssh2 uses OpenSSL for crypto, then sends/receives packets over TCP to/from an SSH server.
- The framework is built per platform and linked into apps or tests.

##### Architectural Patterns
- Wrapper Pattern: Objective-C classes encapsulate C library calls.
- Delegate Pattern: Protocols allow callbacks on session/channel events.
- Layered Architecture:
- Top layer: Public API (NMSSHSession, Channel, SFTP)
- Mid layer: Config & Logging
- Bottom layer: C libraries (libssh2, OpenSSL)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Consumers
    subgraph "Consumers" 
        direction TB
        ExampleApp["PTYExample App"]:::consumer
        CocoaPodsCarthage["CocoaPods / Carthage"]:::consumer
        UnitTests["Unit Tests"]:::consumer
    end

    %% NMSSH Framework Boundary
    subgraph "NMSSH Framework" 
        direction TB

        subgraph "Public API Layer" 
            direction TB
            Session["NMSSHSession"]:::api
            Channel["NMSSHChannel"]:::api
            SFTP["NMSFTP"]:::api
            SFTPFile["NMSFTPFile"]:::api
        end

        subgraph "Config & Logging Layer" 
            direction TB
            Config["NMSSHConfig"]:::config
            HostConfig["NMSSHHostConfig"]:::config
            Logger["NMSSHLogger"]:::config
            SocketHelper["socket_helper"]:::config
        end

        subgraph "Delegate Protocols" 
            direction TB
            SessionDelegate["NMSSHSessionDelegate"]:::config
            ChannelDelegate["NMSSHChannelDelegate"]:::config
        end

        subgraph "Native C Libraries" 
            direction TB
            LibSSH2["libssh2.a"]:::clib
            OpenSSL["libcrypto.a / libssl.a"]:::clib
        end
    end

    %% External SSH Server
    SSHServer["SSH Server"]:::external

    %% Distribution
    Podspec["NMSSH.podspec"]:::distribution
    Xcodeproj["NMSSH.xcodeproj"]:::distribution

    %% Connections
    ExampleApp -->|calls| Session
    CocoaPodsCarthage -.->|distributes| Podspec
    CocoaPodsCarthage -.->|links via| Xcodeproj
    UnitTests -->|tests| Session

    Session -->|creates| Channel
    Session -->|creates| SFTP
    Session -->|creates| SFTPFile

    Session -->|uses| SocketHelper
    Channel -->|delegates| SocketHelper
    SocketHelper -->|wraps calls| LibSSH2
    LibSSH2 -->|uses| OpenSSL
    LibSSH2 -->|TCP packets| SSHServer
    SSHServer -->|TCP packets| LibSSH2

    %% Click Events
    click LibSSH2 "https://github.com/gatherai/nmssh/blob/master/NMSSH-OSX/Libraries/lib/libssh2.a"
    click OpenSSL "https://github.com/gatherai/nmssh/blob/master/NMSSH-OSX/Libraries/lib/libcrypto.a"
    click Session "https://github.com/gatherai/nmssh/blob/master/NMSSH/NMSSHSession.h"
    click Channel "https://github.com/gatherai/nmssh/blob/master/NMSSH/NMSSHChannel.h"
    click SFTP "https://github.com/gatherai/nmssh/blob/master/NMSSH/NMSFTP.h"
    click SFTPFile "https://github.com/gatherai/nmssh/blob/master/NMSSH/NMSFTPFile.h"
    click Config "https://github.com/gatherai/nmssh/blob/master/NMSSH/NMSSHConfig.h"
    click HostConfig "https://github.com/gatherai/nmssh/blob/master/NMSSH/NMSSHHostConfig.h"
    click Logger "https://github.com/gatherai/nmssh/blob/master/NMSSH/Config/NMSSHLogger.h"
    click SocketHelper "https://github.com/gatherai/nmssh/blob/master/NMSSH/Config/socket_helper.h"
    click SessionDelegate "https://github.com/gatherai/nmssh/blob/master/NMSSH/Protocols/NMSSHSessionDelegate.h"
    click ChannelDelegate "https://github.com/gatherai/nmssh/blob/master/NMSSH/Protocols/NMSSHChannelDelegate.h"
    click Podspec "https://github.com/gatherai/nmssh/blob/master/NMSSH.podspec"
    click Xcodeproj "https://github.com/gatherai/nmssh/blob/master/NMSSH.xcodeproj"
    click ExampleApp "https://github.com/gatherai/nmssh/blob/master/Examples/PTYExample/PTYExample.xcodeproj"
    click UnitTests "https://github.com/gatherai/nmssh/blob/master/NMSSHTests/NMSSHSessionTests.h"

    %% Styles
    classDef api fill:#bbdefb,stroke:#1e88e5,color:#0d47a1
    classDef config fill:#c8e6c9,stroke:#43a047,color:#1b5e20
    classDef clib fill:#eeeeee,stroke:#9e9e9e,color:#424242
    classDef consumer fill:#ffe0b2,stroke:#fb8c00,color:#e65100
    classDef distribution fill:#f3e5f5,stroke:#8e24aa,color:#4a148c
    classDef external fill:#fce4ec,stroke:#d81b60,color:#880e4f
```
