C# Fast Prototyping with C2Agents

A Programming Framework

Posted by dave on September 07, 2024

Purpose

Provide a very simple but flexible framework that enables creating C# programs that run as part of the Windows operating system as Windows Services. The framework includes logging, internal and external configuration, install.msi generation along with other useful functions.

Applications that can benefit from using this framework are larger and more complex than what typically would be addressed by a Python script but smaller than a commercial enterprise framework.

This framework is used as the basis for the CurrentForCarbon system: https://upperbay.com/v1/blogs/beachblogs/current-for-carbon

C2_Agent Code: https://github.com/upperbayhawk/C2_Agents.git

Structure

An enterprise typically has a president or CEO in charge of achieving the goals and functions of the company. The president would normally delegate work to a group of assistants such as VPs, directors and managers. These assistants would get work done by enlisting the help of workers specialized in performing the tasks needed. This very elementary structure can be used to solve a wide variety of problems.

The framework structure mirrors the structure of a typical enterprise. It uses containment instead of inheritance in order to decouple components and maintain flexibility.

The AgentObject is the CEO and orchestrates program activities through the use of Assistants. Assistants in turn delegate to Workers as needed.

AgentObjects have 4 entry points: OnInitialize, OnStart, OnFire, OnStop and have properties that are decorated with attributes in the main agent code file.

Assistants inspect C# properties and can act on the properties that have the corresponding attribute (e.g. "publish", "subscribe").

AgentObjects initialize, start their Assistants and run them before, and/or after, any code logic in OnFire.

Assistants use Workers and other libraries as needed to accomplish their task.

Code Namespaces

Upperbay.Agent

Housekeeping code for Windows Services

Cell: App domain container for Agent

Colony: currently consists of one service running one agent. Future extension to include a colony of services with each service running multiple agents. Colony is a cluster of agents running in a cluster of services inside cell app domains with metadata about each Windows Service installer for Cell app domains Colony services and agents are defined in app.config

ColonyMatrix: Common utility services for Agents including Datavariable cache, EventVariable cache and Interfaces which include common interfaces for Agents

Upperbay.Core

Common code for logging, configuration along with misc. code targeted to multiple agents

Upperbay.AgentObject

Main Agent code

ClusterAgent: CurrentForCarbon application ModelAgent: Simplified agent skeleton

Upperbay.Assistants and Upperbay.Workers

MQTTPublisher Loops through the agent properties and publishes the value to the local and remote broker. Uses MQTT worker to connect to and publish. The Worker also sets up a subscription. Subscribed values are cached.

MQTTSubscriber Loops through the agent properties and updates the property value from the cache.

ManualInput Loops through the agent properties and updates the property value based on the value in the configuration file

Simulator Loops through the agent properties and changes the value

Some Framework Functionality

  • Installation package for Windows Service
  • Configuration variables can be defined in hard code, app.config and environment. The search order is the same. Hard code is default but the config file overrides hard code.
    • Hard coded in MyAppConfig.cs, for cluster level variables
    • In the XML file ClusterAgent.app.config, for agent level variables
    • As environment variables, for security variables that should not be in the source code such as:
      • SMSAccountName=
      • SMSAccountKey=
      • LMPKey=
      • EthereumContractAddress=
      • EthereumClusterKey=
      • EthereumClusterAddress=
      • MqttCloudSecureLoginName=
      • MqttCloudSecurePassword=
      • MqttCloudSecureIpAddress=
      • MqttCloudIpAddress=
      • RemoteEthereumServerURL=
      • EthereumExternalPrivateKey=
      • EthereumExternalAddress=
      • LocalEthereumServerURL=

Installation Details

Note: These libraries will indicate security vulnerabilities. There are used by 3rd party libraries and will be upgraded as time allows.

  • BouncyCastle 1.8.2
  • Newtonsoft 10.0.3
  • RestSharp 105.1.0

Application Details

  • Compile colony.sln
  • Run Install.msi
  • Install service using installutil.exe Upperbay.Agent.Colony.exe
  • Start service CurrentForCarbon
  • Stop service CurrentForCarbon
  • Uninstall service using installutil.exe Upperbay.Agent.Colony.exe /u

Create New Agent Object from ModelAgent.cs

  • Copy ModelAgent dir to xxx dir
  • Rename ModelAgent files
  • Rename ModelAgent inside files
  • Add new agent to solution
  • Delete ModelAgent.cs and add xxxAgent.cs
  • Replace one instance of ModelAgent in xxxAgent.cs
  • Compile colony.sln
  • Run Install.msi

Have Fun With Agents!

Dave Hardin