コード例 #1
0
        static int Main(string[] arguments)
        {
            Assembly.Load("GeneratedCode");

            Action printUsage = () =>
            {
                Console.WriteLine("Usage: Client <hostname> <port> <client_id>");
                Console.WriteLine("Connects to a demo deployment.");
                Console.WriteLine("    <hostname>      - hostname of the receptionist to connect to.");
                Console.WriteLine("    <port>          - port to use.");
                Console.WriteLine("    <client_id>     - name of the client.");
                Console.WriteLine("Alternatively: Client <snapshotfile> will generate a snapshot and exit.");
            };



            if (arguments.Length == 1)
            {
                SnapshotGenerator.GenerateSnapshot(arguments[0], WorkerAttributes);
                return(0);
            }
            else
            {
                printUsage();
                return(ErrorExitStatus);
            }
        }
コード例 #2
0
        static int Main(string[] arguments)
        {
            Action printUsage = () =>
            {
                Console.WriteLine("Usage 1: Client local <hostname> <port> <client id>");
                Console.WriteLine("Connect to a local deployment.");
                Console.WriteLine("");
                Console.WriteLine("Usage 2: Client cloud <hostname> <player identity token> <login token>");
                Console.WriteLine("Connect to a cloud deployment.");
                Console.WriteLine("");
                Console.WriteLine("Usage 3: Client snapshot <snapshot file>");
                Console.WriteLine("Generate a snapshot and exit.");
            };

            if (arguments.Length == 2 && arguments[0] == "snapshot")
            {
                SnapshotGenerator.GenerateSnapshot(arguments[1], WorkerAttributes);
                return(0);
            }

            if (arguments.Length != 4 || (arguments[0] != "local" && arguments[0] != "cloud"))
            {
                printUsage();
                return(ErrorExitStatus);
            }
            var connectToCloud = arguments[0] == "cloud";

            Console.WriteLine("Client Starting...");
            using (var connection = connectToCloud ? ConnectClientLocator(arguments) : ConnectClientReceptionist(arguments))
            {
                Console.WriteLine("Client connected to the deployment.");
                var dispatcher        = new Dispatcher();
                var isConnected       = true;
                var entitiesToRespond = new HashSet <EntityId>(EntityIds);

                dispatcher.OnDisconnect(op =>
                {
                    Console.Error.WriteLine("[disconnect] {0}", op.Reason);
                    isConnected = false;
                });

                dispatcher.OnLogMessage(op =>
                {
                    connection.SendLogMessage(op.Level, LoggerName, op.Message);
                    Console.WriteLine("Log Message: {0}", op.Message);
                    if (op.Level == LogLevel.Fatal)
                    {
                        Console.Error.WriteLine("Fatal error: {0}", op.Message);
                        Environment.Exit(ErrorExitStatus);
                    }
                });
                dispatcher.OnAuthorityChange(Position.Metaclass, cb =>
                {
                    Console.WriteLine("authority change {0}", cb.Authority);
                });

                dispatcher.OnCommandResponse(PingResponder.Commands.Ping.Metaclass, response =>
                {
                    HandlePong(response, connection);
                });

                connection.SendLogMessage(LogLevel.Info, LoggerName,
                                          "Successfully connected using TCP and the Receptionist");

                var pingTimer = new Timer(pingIntervalMs);
                pingTimer.Elapsed += (source, e) =>
                {
                    foreach (var entityId in entitiesToRespond)
                    {
                        connection.SendCommandRequest(PingResponder.Commands.Ping.Metaclass, entityId, new PingRequest(), CommandRequestTimeoutMS, null);
                    }
                };
                pingTimer.Start();

                while (isConnected)
                {
                    var opList = connection.GetOpList(GetOpListTimeoutInMilliseconds);
                    dispatcher.Process(opList);
                }
            }

            return(0);
        }
コード例 #3
0
        static int Main(string[] arguments)
        {
            Action printUsage = () =>
            {
                Console.WriteLine("Usage: Client <hostname> <port> <client_id>");
                Console.WriteLine("Connects to a demo deployment.");
                Console.WriteLine("    <hostname>      - hostname of the receptionist to connect to.");
                Console.WriteLine("    <port>          - port to use.");
                Console.WriteLine("    <client_id>     - name of the client.");
                Console.WriteLine("Alternatively: Client <snapshotfile> will generate a snapshot and exit.");
            };

            if (arguments.Length != 1 && arguments.Length != 3)
            {
                printUsage();
                return(ErrorExitStatus);
            }

            if (arguments.Length == 1)
            {
                SnapshotGenerator.GenerateSnapshot(arguments[0], WorkerAttributes);
                return(0);
            }

            Console.WriteLine("Client Starting...");
            using (var connection = ConnectClient(arguments))
            {
                using (var dispatcher = new Dispatcher())
                {
                    var watch = new Stopwatch();
                    watch.Start();

                    var isConnected       = true;
                    var entitiesToRespond = new HashSet <EntityId>(EntityIds);

                    dispatcher.OnDisconnect(op =>
                    {
                        Console.Error.WriteLine("[disconnect] {0}", op.Reason);
                        isConnected = false;
                    });

                    dispatcher.OnLogMessage(op =>
                    {
                        connection.SendLogMessage(op.Level, LoggerName, op.Message);
                        Console.WriteLine("Log Message: {0}", op.Message);
                        if (op.Level == LogLevel.Fatal)
                        {
                            Console.Error.WriteLine("Fatal error: {0}", op.Message);
                            Environment.Exit(ErrorExitStatus);
                        }
                    });
                    dispatcher.OnAuthorityChange <Position>(cb =>
                    {
                        Console.WriteLine("authority change {0}", cb.Authority);
                    });

                    dispatcher.OnCommandResponse <PingResponder.Commands.Ping>(response =>
                    {
                        HandlePong(response, connection);
                    });

                    connection.SendLogMessage(LogLevel.Info, LoggerName,
                                              "Successfully connected using TCP and the Receptionist");

                    var pingTimer = new Timer(pingIntervalMs);
                    pingTimer.Elapsed += (source, e) =>
                    {
                        foreach (var entityId in entitiesToRespond)
                        {
                            SendGetWorkerTypeCommand(connection, entityId);
                        }
                    };
                    pingTimer.Start();

                    while (isConnected)
                    {
                        var opList = connection.GetOpList(GetOpListTimeoutInMilliseconds);
                        dispatcher.Process(opList);
                    }
                }
            }

            return(0);
        }