Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace ExampleAgent
- {
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading;
- public class Program
- {
- public static int Main(string[] args)
- {
- var agent = new StringAgent();
- agent.Start();
- //demo 10 times.
- for (int i = 0; i < 10; i++)
- {
- agent.SendMessage(new StringAgent.Message("New message!"));
- Thread.Sleep(50);
- var msgs = agent.RecieveMessages("main");
- Thread.Sleep(50);
- foreach (var m in msgs)
- {
- Console.WriteLine(m.Data);
- }
- }
- agent.Stop();
- Console.WriteLine("Done.");
- Console.ReadLine();
- return 0;
- }
- }
- public class StringAgent
- {
- private object locker = new object();
- //our message queue.
- private List<Message> MessageQueue;
- private string Resource;
- private Thread AgentThread;
- private bool Running;
- public StringAgent()
- {
- MessageQueue = new List<Message>();
- Resource = "Hello!";
- Running = true;
- //start the agent in a new thread.
- AgentThread = new Thread(new ThreadStart(Run));
- AgentThread.IsBackground = true;
- }
- public void Start()
- {
- AgentThread.Start();
- }
- public void Stop()
- {
- Running = false;
- AgentThread.Join();
- }
- //this is the brains of our agent, this method will run
- //in it's own thread and manage our resources.
- public void Run()
- {
- while (Running)
- {
- Thread.Sleep(50); //just to make it easier on our CPU.
- lock (locker) //lock our fields so they don't get mangled by other threads.
- {
- //now the agent works on it's messages
- foreach (Message om in MessageQueue)
- {
- if ((!om.Recieved.Contains("agent")) && om.Outgoing == false)
- {
- om.Recieved.Add("agent");
- Resource = om.Data;
- }
- }
- if (!string.IsNullOrEmpty(Resource))
- {
- //lets pretend we don't want exclaimation points in our resource
- if (Resource.Contains("!"))
- {
- Resource = Resource.Replace("!", "?");
- }
- }
- //delete old messages so the queue doesn't clog up.
- //we have to use a new list because foreach will
- //throw an exception if we modify the list it's
- //currently working on.
- List<Message> nml = new List<Message>();
- foreach (Message nm in MessageQueue)
- {
- nm.Lifetime--; //decrease the message's life.
- if (nm.Lifetime <= 0) continue; //if it's old, don't include it in the new list.
- else nml.Add(nm);
- }
- MessageQueue = nml;
- var m = new Message(string.Format("Resource is {0}", Resource)) { Outgoing = true };
- MessageQueue.Add(m);
- }
- }
- }
- public void SendMessage(Message m)
- {
- lock (locker)
- {
- MessageQueue.Add(m);
- }
- }
- public List<Message> RecieveMessages(string name)
- {
- List<Message> ml = new List<Message>();
- lock (locker)
- {
- foreach (Message m in MessageQueue)
- {
- //if the requester never saw this message
- if ((!m.Recieved.Contains(name)) && m.Outgoing == true)
- {
- //now they've read it.
- m.Recieved.Add(name);
- ml.Add(m);
- }
- }
- }
- return ml;
- }
- //Our message type, a simple class inside of our agent's class.
- public class Message
- {
- //this will tell the agent if this message was created by the
- //agent or by someone else for the agent.
- public bool Outgoing;
- //this is a list of objects that already read this message,
- //so they don't read it again by mistake.
- public List<string> Recieved;
- //how long our messages should live before we delete them from the queue.
- public int Lifetime;
- //the data that the message holds.
- public string Data;
- public Message(string data)
- {
- Outgoing = false;
- Recieved = new List<string>();
- Lifetime = 10;
- Data = data;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement