Advertisement
konalisp

Agent Example

Sep 21st, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.49 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ExampleAgent
  4. {
  5.     using System.Collections.Generic;
  6.     using System.Linq;
  7.     using System.Threading;
  8.    
  9.     public class Program
  10.     {
  11.         public static int Main(string[] args)
  12.         {
  13.             var agent = new StringAgent();
  14.             agent.Start();
  15.            
  16.             //demo 10 times.
  17.             for (int i = 0; i < 10; i++)
  18.             {
  19.                 agent.SendMessage(new StringAgent.Message("New message!"));
  20.                 Thread.Sleep(50);
  21.                 var msgs = agent.RecieveMessages("main");
  22.                 Thread.Sleep(50);
  23.                 foreach (var m in msgs)
  24.                 {
  25.                     Console.WriteLine(m.Data);
  26.                 }
  27.             }
  28.            
  29.             agent.Stop();
  30.            
  31.             Console.WriteLine("Done.");
  32.             Console.ReadLine();
  33.            
  34.             return 0;
  35.         }
  36.     }
  37.    
  38.     public class StringAgent
  39.     {
  40.         private object locker = new object();
  41.         //our message queue.
  42.         private List<Message> MessageQueue;
  43.         private string Resource;
  44.        
  45.         private Thread AgentThread;
  46.         private bool Running;
  47.        
  48.         public StringAgent()
  49.         {
  50.             MessageQueue = new List<Message>();
  51.             Resource = "Hello!";
  52.             Running = true;
  53.            
  54.             //start the agent in a new thread.
  55.             AgentThread = new Thread(new ThreadStart(Run));
  56.             AgentThread.IsBackground = true;
  57.         }
  58.        
  59.         public void Start()
  60.         {
  61.             AgentThread.Start();
  62.         }
  63.        
  64.         public void Stop()
  65.         {
  66.             Running = false;
  67.             AgentThread.Join();
  68.         }
  69.        
  70.         //this is the brains of our agent, this method will run
  71.         //in it's own thread and manage our resources.
  72.         public void Run()
  73.         {
  74.             while (Running)
  75.             {
  76.                 Thread.Sleep(50); //just to make it easier on our CPU.
  77.                 lock (locker) //lock our fields so they don't get mangled by other threads.
  78.                 {
  79.                     //now the agent works on it's messages
  80.                     foreach (Message om in MessageQueue)
  81.                     {
  82.                         if ((!om.Recieved.Contains("agent")) && om.Outgoing == false)
  83.                         {
  84.                             om.Recieved.Add("agent");
  85.                             Resource = om.Data;
  86.                         }
  87.                     }
  88.                    
  89.                     if (!string.IsNullOrEmpty(Resource))
  90.                     {
  91.                         //lets pretend we don't want exclaimation points in our resource
  92.                         if (Resource.Contains("!"))
  93.                         {
  94.                             Resource = Resource.Replace("!", "?");
  95.                         }
  96.                     }
  97.                
  98.                     //delete old messages so the queue doesn't clog up.
  99.                     //we have to use a new list because foreach will
  100.                     //throw an exception if we modify the list it's
  101.                     //currently working on.
  102.                     List<Message> nml = new List<Message>();
  103.                     foreach (Message nm in MessageQueue)
  104.                     {
  105.                         nm.Lifetime--; //decrease the message's life.
  106.                         if (nm.Lifetime <= 0) continue; //if it's old, don't include it in the new list.
  107.                         else nml.Add(nm);
  108.                     }
  109.                     MessageQueue = nml;
  110.                    
  111.                     var m = new Message(string.Format("Resource is {0}", Resource)) { Outgoing = true };
  112.                     MessageQueue.Add(m);
  113.                 }
  114.             }
  115.         }
  116.        
  117.         public void SendMessage(Message m)
  118.         {
  119.             lock (locker)
  120.             {
  121.                 MessageQueue.Add(m);
  122.             }
  123.         }
  124.        
  125.         public List<Message> RecieveMessages(string name)
  126.         {
  127.             List<Message> ml = new List<Message>();
  128.             lock (locker)
  129.             {
  130.                 foreach (Message m in MessageQueue)
  131.                 {
  132.                     //if the requester never saw this message
  133.                     if ((!m.Recieved.Contains(name)) && m.Outgoing == true)
  134.                     {
  135.                         //now they've read it.
  136.                         m.Recieved.Add(name);
  137.                         ml.Add(m);
  138.                     }
  139.                 }
  140.             }
  141.             return ml;
  142.         }
  143.        
  144.         //Our message type, a simple class inside of our agent's class.
  145.         public class Message
  146.         {
  147.             //this will tell the agent if this message was created by the
  148.             //agent or by someone else for the agent.
  149.             public bool Outgoing;
  150.             //this is a list of objects that already read this message,
  151.             //so they don't read it again by mistake.
  152.             public List<string> Recieved;
  153.             //how long our messages should live before we delete them from the queue.
  154.             public int Lifetime;
  155.             //the data that the message holds.
  156.             public string Data;
  157.            
  158.             public Message(string data)
  159.             {
  160.                 Outgoing = false;
  161.                 Recieved = new List<string>();
  162.                 Lifetime = 10;
  163.                 Data = data;
  164.             }
  165.         }
  166.        
  167.     }  
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement