Example #1
0
        private void BroadcastTake(DIDATuple didaTake)
        {
            string mypath     = System.Reflection.Assembly.GetEntryAssembly().Location;
            string finalpath  = mypath.Substring(0, mypath.Length - 10);
            string newpath    = Path.GetFullPath(Path.Combine(finalpath, @"..\..\"));
            string pathToList = newpath + "ListaServers.txt";

            string[] lines = System.IO.File.ReadAllLines(pathToList);
            foreach (string line in lines)
            {
                //port : name
                string[] args = line.Split(':');

                int    server_port = Int32.Parse(args[0]);
                string server_name = args[1];

                if (didaTake.getName() != server_name)
                {
                    try
                    {
                        string           url         = "tcp://localhost:" + server_port + "/" + server_name;
                        TcpClientChannel channelnovo = new TcpClientChannel(server_name, null);
                        ChannelServices.RegisterChannel(channelnovo, false);
                        IServerInterface servernovo = (IServerInterface)Activator.GetObject(typeof(IServerInterface), url);
                        DIDATuple        didaToSend = didaTake;
                        didaToSend.setName(server_name);

                        servernovo.Take(didaToSend, null, 0);
                        ChannelServices.UnregisterChannel(channelnovo);
                    }
                    catch
                    {
                    }
                }
            }
        }
Example #2
0
        public void Parser(List <string> command, string server_name, string server_port)
        {
            Console.WriteLine();
            Random r = new Random();

            switch (command[0].ToLower())
            {
            case "add":
                Console.Write("Adding new tuple : ");
                List <string> fieldsList = command.ToList();
                fieldsList.RemoveAt(0);
                DIDATuple didatuple = new DIDATuple(fieldsList);
                didatuple.setName(server_name);
                didatuple.setPort(Int32.Parse(server_port));
                Add(didatuple);
                this.PrintTuple(didatuple);
                break;

            case "read":
                Console.Write("Reading tuple    : ");
                fieldsList = command.ToList();
                fieldsList.RemoveAt(0);
                didatuple = new DIDATuple(fieldsList);

                didatuple.setName(server_name);
                didatuple.setPort(Int32.Parse(server_port));

                int    channel_port = r.Next(7000, 8000);
                string channel_name = "client" + channel_port;

                IDIDATuple didatuplefinal = (DIDATuple)Read(didatuple, channel_name, channel_port);
                this.PrintTuple(didatuple);

                if (didatuplefinal == null)
                {
                    Console.WriteLine("-> Unsuccesful search! Waiting..");

                    TcpServerChannel channel = new TcpServerChannel(channel_name, channel_port);
                    ChannelServices.RegisterChannel(channel, false);
                    RemotingConfiguration.RegisterWellKnownServiceType(
                        typeof(Client), channel_name, WellKnownObjectMode.Singleton);

                    BlockAndWait();
                    Read(didatuple, channel_name, channel_port);
                    Console.WriteLine("-> Succesful Read!");
                    ChannelServices.UnregisterChannel(channel);
                }
                else
                {
                    Console.WriteLine("-> Succesful Read!");
                }
                break;

            case "take":
                Console.Write("Removing tuple   : ");
                fieldsList = command.ToList();
                fieldsList.RemoveAt(0);
                didatuple = new DIDATuple(fieldsList);

                didatuple.setName(server_name);
                didatuple.setPort(Int32.Parse(server_port));

                int    channel_port2 = r.Next(7000, 8000);
                string channel_name2 = "client" + channel_port2;

                IDIDATuple takeFromServer = (DIDATuple)Take(didatuple, channel_name2, channel_port2);
                this.PrintTuple(didatuple);

                if (takeFromServer == null)
                {
                    Console.WriteLine("-> Unsuccesful take! Waiting..");

                    TcpServerChannel channel2 = new TcpServerChannel(channel_name2, channel_port2);
                    ChannelServices.RegisterChannel(channel2, false);
                    RemotingConfiguration.RegisterWellKnownServiceType(
                        typeof(Client), channel_name2, WellKnownObjectMode.Singleton);

                    BlockAndWait();
                    Take(didatuple, channel_name2, channel_port2);
                    Console.WriteLine("-> Succesful take!");
                    ChannelServices.UnregisterChannel(channel2);
                }
                else
                {
                    Console.WriteLine("-> Succesful take!");
                }
                break;

            case "wait":
                Console.WriteLine("Waiting");
                Wait(Int32.Parse(command[1]));
                break;

            case "begin-repeat":
                Console.WriteLine("Start Repeat");
                BeginRepeat(command[1]);
                break;

            case "end-repeat":
                EndRepeat(server_name, server_port);
                break;
            }
        }