Esempio n. 1
0
        static void Main(string[] args)
        {
            if (args.Length > 1)
            {
                if (args[0] == "table")
                {
                    // Instantiate a new space repository and add a gate to it.
                    SpaceRepository repository = new SpaceRepository();
                    repository.AddGate("tcp://" + args[1] + ":31415?KEEP");
                    repository.AddGate("tcp://" + args[1] + ":31416?KEEP");
                    repository.AddGate("tcp://" + args[1] + ":31417?KEEP");
                    repository.AddGate("tcp://" + args[1] + ":31418?KEEP");
                    repository.AddGate("tcp://" + args[1] + ":31419?KEEP");

                    // Add a new Fifo based space to the repository.
                    repository.AddSpace("DiningTable", new SequentialSpace());

                    // Insert the forks that the philosophers must share.
                    repository.Put("DiningTable", "FORK", 1);
                    repository.Put("DiningTable", "FORK", 2);
                    repository.Put("DiningTable", "FORK", 3);
                    repository.Put("DiningTable", "FORK", 4);
                    repository.Put("DiningTable", "FORK", 5);
                    return;
                }
                else if (args[0] == "philosopher")
                {
                    Console.WriteLine("connecting " + args[1]);

                    // Instantiate a new remote space, thereby allowing a persistant networked connection to the repository.
                    ISpace remotespace1 = new RemoteSpace("tcp://" + args[1] + ":31415/DiningTable?KEEP");
                    ISpace remotespace2 = new RemoteSpace("tcp://" + args[1] + ":31416/DiningTable?KEEP");
                    ISpace remotespace3 = new RemoteSpace("tcp://" + args[1] + ":31417/DiningTable?KEEP");
                    ISpace remotespace4 = new RemoteSpace("tcp://" + args[1] + ":31418/DiningTable?KEEP");
                    ISpace remotespace5 = new RemoteSpace("tcp://" + args[1] + ":31419/DiningTable?KEEP");

                    // Instantiate the philosopher agents and let them use the same connection to access the repository.
                    List <AgentBase> agents = new List <AgentBase>();
                    agents.Add(new Philosopher("Alice", 1, 5, remotespace1));
                    agents.Add(new Philosopher("Charlie", 2, 5, remotespace2));
                    agents.Add(new Philosopher("Bob", 3, 5, remotespace3));
                    agents.Add(new Philosopher("Dave", 4, 5, remotespace4));
                    agents.Add(new Philosopher("Homer", 5, 5, remotespace5));

                    // Let the philosophers eat.
                    agents.ForEach(a => a.Start());
                    Console.Read();
                    return;
                }
            }
            Console.WriteLine("Please specify [table|philosopher] and IP address to use");
            Console.Read();
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            try
            {
                // Instantiate a new space repository, and add two seperate gates.
                SpaceRepository repository = new SpaceRepository();
                repository.AddGate("tcp://127.0.0.1:123?KEEP");
                repository.AddGate("tcp://127.0.0.1:124?KEEP");

                // Create a new fifo based space, and insert the ping.
                repository.AddSpace("pingpong", new SequentialSpace());
                repository.Put("pingpong", "ping", 0);

                // Create two seperate remotespaces and agents.
                // The agents use their own private remotespace.
                RemoteSpace remotespace1 = new RemoteSpace("tcp://127.0.0.1:123/pingpong?KEEP");
                PingPong    a1           = new PingPong("ping", "pong", remotespace1);

                RemoteSpace remotespace2 = new RemoteSpace("tcp://127.0.0.1:124/pingpong?KEEP");
                PingPong    a2           = new PingPong("pong", "ping", remotespace2);

                // Start the agents
                a1.Start();
                a2.Start();
                Console.Read();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine("Please specify your name");
                return;
            }
            // Instantiate a new Space repository.
            SpaceRepository repository = new SpaceRepository();

            // Add a gate, such that we can connect to it.
            repository.AddGate("tcp://127.0.0.1:123?CONN");

            // Add a new Fifo based space.
            repository.AddSpace("dtu", new SequentialSpace());

            // Insert a tuple with a message.
            repository.Put("dtu", "Hello student!");

            // Instantiate a remotespace (a networked space) thereby connecting to the spacerepository.
            ISpace remotespace = new RemoteSpace("tcp://127.0.0.1:123/dtu?CONN");

            // Instantiate a new agent, assign the tuple space and start it.
            AgentBase student = new Student(args[0], remotespace);

            student.Start();

            // Wait and retrieve the message from the agent.
            ITuple tuple = repository.Get("dtu", typeof(string), typeof(string));

            // Print the contents to the console.
            Console.WriteLine(string.Format("{0}, you are attending course {1}", tuple[0], tuple[1]));
            Console.Read();
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            // Instantiate a new Space repository.
            SpaceRepository repository = new SpaceRepository();

            // Add a gate, such that we can connect to it.
            repository.AddGate("tcp://127.0.0.1:9876?KEEP");

            // Add a new Fifo based space.
            repository.AddSpace("dtu", new SequentialSpace());

            Console.WriteLine("Starting");
            repository.Put("dtu", 0);

            // Instantiate a remotespace (a networked space) thereby connecting to the spacerepository.
            ISpace remotespace = new RemoteSpace("tcp://127.0.0.1:9876/dtu?KEEP");

            // Instantiate a new agent, assign the tuple space and start it.
            AgentBase userA = new User("A", remotespace);
            AgentBase userB = new User("B", remotespace);

            userA.Start();
            userB.Start();
        }