Esempio n. 1
0
 public int execute(NotFoundCommand command)
 {
     Console.WriteLine("Recieved " + command.getType() + " command from " + command.getIssuerId());
     return(0);
 }
Esempio n. 2
0
        static void Main(string[] args)
        {
            try
            {
                // Client Information
                string username   = args[0];
                string client_url = args[1];
                string server_url = args[2];

                // Start Connection
                Regex r = new Regex(@"^(?<protocol>\w+)://[^/]+?:(?<port>\d+)?/",
                                    RegexOptions.None, TimeSpan.FromMilliseconds(100));
                Match m    = r.Match(client_url);
                int   port = Int32.Parse(m.Result("${port}"));

                BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider();
                IDictionary props = new Hashtable();
                props["port"]    = port;
                props["timeout"] = 5000; // in milliseconds
                TcpChannel channel = new TcpChannel(props, null, provider);
                //TcpChannel channel = new TcpChannel(port);

                ClientObject client = new ClientObject();
                client.setUsername(username);
                client.setUrl(client_url);
                client.setPreferedServerUrl(server_url);
                RemotingServices.Marshal(client, "ClientObject", typeof(ClientObject));

                ChannelServices.RegisterChannel(channel, false);

                // Read Commands
                int             sequeneNumber = 0;
                InstructsParser parser        = new InstructsParser();

                string clientScript = args[3];

                string[] lines = File.ReadAllLines(clientScript);

                foreach (string line in lines)
                {
                    if (args.Length == 5)
                    {
                        Console.WriteLine("Press any key to execute next step: " + line);
                        Console.ReadLine();
                    }

                    ServerInterface server = client.getServer(server_url);

                    if (server == null)
                    {
                        Console.WriteLine("Server is unreachable");
                        return;
                    }

                    server.addClient(client_url);

                    char[]   delimiter        = { ' ' };
                    string[] instructionParts = line.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);

                    string myId = username + "-" + client_url;
                    if (instructionParts[0] == "create")
                    {
                        CreateCommand command = parser.parseCreateCommand(instructionParts, myId);
                        command.setIssuerId(myId);
                        command.setSentByClient(true);
                        command.setSequenceNumber(sequeneNumber);
                        command.setTimestamp(DateTime.Now);
                        sequeneNumber++;
                        Console.WriteLine(command.getType() + " - " + command.getSequenceNumber());

                        int res = -1;
                        while (res == -1)
                        {
                            try
                            {
                                res = server.execute(command);
                            }
                            catch (System.Net.Sockets.SocketException)
                            {
                                Console.WriteLine("Timeout: server " + client.getPreferedServerUrl() + " seems to be frozen, trying another server");
                                server = client.getServer("ANOTHER_SERVER");
                            }
                        }

                        Console.WriteLine("Result: {0}", res);


                        // gossip
                        List <string> clients = client.getClientsForGossip();
                        client.gossip(command.getMeetingProposal(), clients);
                    }
                    else if (instructionParts[0] == "list")
                    {
                        ListCommand command = parser.parseListCommand(instructionParts);
                        command.setIssuerId(myId);
                        command.setSentByClient(true);
                        command.setSequenceNumber(sequeneNumber);
                        command.setTimestamp(DateTime.Now);
                        sequeneNumber++; // lock it
                        Console.WriteLine(command.getType());

                        List <MeetingProposal> proposals = null;

                        while (proposals == null)
                        {
                            try
                            {
                                proposals = server.execute(command);
                            }
                            catch (System.Net.Sockets.SocketException)
                            {
                                Console.WriteLine("Timeout: server " + client.getPreferedServerUrl() + " seems to be frozen, trying another server");
                                server = client.getServer("ANOTHER_SERVER");
                            }
                        }

                        client.listMeetings(proposals);
                    }
                    else if (instructionParts[0] == "join")
                    {
                        JoinCommand command = parser.parseJoinCommand(instructionParts);
                        command.setIssuerId(myId);
                        command.setSentByClient(true);
                        command.setSequenceNumber(sequeneNumber);
                        command.setTimestamp(DateTime.Now);
                        sequeneNumber++; // lock it
                        Console.WriteLine(command.getType());

                        int res = -1;
                        while (res == -1)
                        {
                            try
                            {
                                res = server.execute(command);
                            }
                            catch (System.Net.Sockets.SocketException)
                            {
                                Console.WriteLine("Timeout: server " + client.getPreferedServerUrl() + " seems to be frozen, trying another server");
                                server = client.getServer("ANOTHER_SERVER");
                            }
                        }

                        Console.WriteLine("Result: {0}", res);
                    }
                    else if (instructionParts[0] == "close")
                    {
                        CloseCommand command = parser.parseCloseCommand(instructionParts);
                        command.setIssuerId(myId);
                        command.setSentByClient(true);
                        command.setSequenceNumber(sequeneNumber);
                        command.setTimestamp(DateTime.Now);
                        sequeneNumber++; // lock it
                        Console.WriteLine(command.getType());

                        int res = -1;
                        while (res == -1)
                        {
                            try
                            {
                                res = server.execute(command);
                            }
                            catch (System.Net.Sockets.SocketException)
                            {
                                Console.WriteLine("Timeout: server " + client.getPreferedServerUrl() + " seems to be frozen, trying another server");
                                server = client.getServer("ANOTHER_SERVER");
                            }
                        }

                        Console.WriteLine("Result: {0}", res);
                    }
                    else if (instructionParts[0] == "wait")
                    {
                        WaitCommand command = parser.parseWaitCommand(instructionParts);
                        command.setIssuerId(myId);
                        command.setSentByClient(true);
                        command.setSequenceNumber(sequeneNumber);
                        command.setTimestamp(DateTime.Now);
                        sequeneNumber++; // lock it
                        Console.WriteLine(command.getType());

                        int res = -1;
                        while (res == -1)
                        {
                            try
                            {
                                res = server.execute(command);
                            }
                            catch (System.Net.Sockets.SocketException)
                            {
                                Console.WriteLine("Timeout: server " + client.getPreferedServerUrl() + " seems to be frozen, trying another server");
                                server = client.getServer("ANOTHER_SERVER");
                            }
                        }

                        Console.WriteLine("Result: {0}", res);
                    }
                    else
                    {
                        NotFoundCommand command = new NotFoundCommand();
                        Console.WriteLine(command.getType());
                    }
                }

                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.ToString());
                Console.WriteLine(e.StackTrace);

                // diagnostic: to see the error before exiting
                System.Threading.Thread.Sleep(60000);
            }
        }