Ejemplo n.º 1
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;
            }
        }