Exemplo n.º 1
0
    public static void QueueCommand(string cmds, SocketCommunicator sc)
    {
        //Initialize RobotCommandList
        List <RobotCommand> robotCommandList = new List <RobotCommand>();

        string[] cmdList = cmds.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
        foreach (string cmd in cmdList)
        {
            robotCommandList.Add(new RobotCommand(cmd));
        }
        //Use RobotCommandList to update current robot commands
        foreach (RobotCommand cmd in robotCommandList)
        {
            if (cmd.id == 1) //double checking is probably not necessary
            {
                foreach (RobotCommand _cmd in commandQueue)
                {
                    cachedRobotCommands[_cmd[0]] = _cmd;
                }
                updatedFromCache     = false;
                currentRobotCommands = new Dictionary <string, RobotCommand>(cachedRobotCommands);
                updatedFromCache     = true;
                commandQueue.Clear();
                return;
            }
            commandQueue.Add(cmd);
        }
    }
Exemplo n.º 2
0
 public byte Read(ObjectLocation objLocation)
 {
     SocketCommunicator.Send(_socket, new ReadMessage {
         ObjectLocation = objLocation
     });
     return(GetReadResponse().Value);
 }
Exemplo n.º 3
0
    void StartClient()
    {
        string msg;

        msg = sc.GetMessage();
        if (msg == "RESET")
        {
            while (true)
            {
                msg = sc.GetMessage();
                if (!connected)
                {
                    sc.SendMessage("RESET");
                    CommandHandler.ClearCommands();
                    break;
                }
                if (msg == "RESET")
                {
                    break;
                }
                CommandHandler.QueueCommand(msg, sc);
            }
        }
        else
        {
            sc.SendMessage("RESET");
        }
        connected = false;
        sc.CloseClient();
        sc = null;
    }
Exemplo n.º 4
0
        public void HandleEndStagingPhaseMessage(EndStagingPhaseMessage endStagingPhaseMessage, Socket socket)
        {
            Vote vote = _dataManager.EndStagingPhase();

            SocketCommunicator.Send(socket, new EndStagingPhaseResponse {
                Vote = vote
            });
        }
Exemplo n.º 5
0
        public void HandleReadMessage(ReadMessage readMessage, Socket socket)
        {
            byte readValue = _dataManager.Read(readMessage.ObjectLocation);

            SocketCommunicator.Send(socket, new ReadResponse {
                Value = readValue
            });
        }
Exemplo n.º 6
0
        void helper_MessageReceivedEvent(SocketCommunicator helper, object message)
        {
            MessageUpdateDelegate del = ClientMessageReceivedEvent;

            if (del != null)
            {
                del(this, (SocketCommunicatorEx)helper, message);
            }
        }
Exemplo n.º 7
0
        void helper_SendAsyncCompleteEvent(SocketCommunicator helper, SocketCommunicator.AsyncMessageSendInfo info)
        {
            AsyncMessageSendUpdateDelegate del = ClientAsyncMessageSendEvent;

            if (del != null)
            {
                del(this, helper as SocketCommunicatorEx, info);
            }
        }
Exemplo n.º 8
0
        void _messageClient_ConnectedEvent(SocketCommunicator helper)
        {
            // Send an update of the clients to server.
            if (_socketClient == helper)
            {
                SendAccessControlMessage();

                SendClientsUpdate();
            }
        }
Exemplo n.º 9
0
        void _messageClient_SendAsyncCompleteEvent(SocketCommunicator helper, SocketCommunicator.AsyncMessageSendInfo info)
        {
            // Message sent to server.
            MessageUpdateDelegate del = MessageUpdateEvent;

            if (del != null)
            {
                del(this, info.Message);
            }
        }
Exemplo n.º 10
0
        void helper_DisconnectedEvent(SocketCommunicator client)
        {
            //Monitor.ReportImportant("Client [" + client.Id + "] disconnected.");

            ServerClientUpdateDelegate delegateInstance = ClientDisconnectedEvent;

            if (delegateInstance != null)
            {
                delegateInstance(this, client as SocketCommunicatorEx);
            }
        }
Exemplo n.º 11
0
        private void HandleConnection(object receivingSocketObj)
        {
            Socket receivingSocket = (Socket)receivingSocketObj;

            while (receivingSocket.Connected)
            {
                BaseMessage message = SocketCommunicator.Receive(receivingSocket);

                CommandMessage commandMessage = (CommandMessage)message;
                commandMessage.HandleCommandMessage(this, receivingSocket);
            }
            Console.WriteLine("Conn closed");
        }
Exemplo n.º 12
0
        void _messageClient_DisconnectedEvent(SocketCommunicator helper)
        {
            ICollection <ClientId> ids = _originalServerClientsHotSwap.Values;

            _originalServerClientsHotSwap.Clear();

            //lock (_syncRoot)
            //{
            //    _localToRemoteId.Clear();
            //}

            // Removing all server clients.
            foreach (ClientId id in ids)
            {
                // Notify of clients removal, with non permanent remove, since they may later be restored.
                RaiseClientRemovedEvent(id, false);
            }
        }
Exemplo n.º 13
0
 void StartSimulation()
 {
     connected = true;
     try
     {
         sc        = new SocketCommunicator(29071);
         sc.thread = new Thread(new ThreadStart(StartClient));
         sc.thread.Start();
     }
     catch (SocketException e)
     {
         Debug.LogError("There was an error connecting to the controller.");
     }
     catch (Exception e)
     {
         Debug.Log(e.ToString());
     }
 }
Exemplo n.º 14
0
        void _messageClient_MessageReceivedEvent(SocketCommunicator helper, object message)
        {
            if (message is EnvelopeMessage)
            {
                EnvelopeMessage envelopeMessage = (EnvelopeMessage)message;

                // Remove the remote message bus index association.
                envelopeMessage.Sender.LocalMessageBusIndex = ClientId.InvalidMessageBusClientIndex;

                foreach (ClientId id in envelopeMessage.Receivers)
                {
                    // Decode the id.
                    id.LocalMessageBusIndex = base.GetClientIndexByGuid(id.Guid);

                    if (id.IsMessageBusIndexValid)
                    {
                        // Assign as a part of the local bus.
                        id.MessageBus = this;
                        if (DoSendToClient(envelopeMessage.Sender, id, envelopeMessage.Envelope, null) != SendToClientResultEnum.Success)
                        {
#if Matrix_Diagnostics
                            InstanceMonitor.OperationError(string.Format("Failed to accept envelope message [{0}].", envelopeMessage.ToString()));
#endif
                        }
                    }
                    else
                    {
#if Matrix_Diagnostics
                        InstanceMonitor.OperationError(string.Format("Failed to accept envelope message [{0}] due to unrecognized receiver id.", envelopeMessage.ToString()));
#endif
                    }
                }
            }
            else if (message is ClientsListMessage)
            {// Received client update from server.
                ClientsListMessage listMessage = (ClientsListMessage)message;

                int jef = 0;
                foreach (var client in listMessage.Ids)
                {
                    Console.WriteLine("Incoming client id: " + client + " Source type: " + listMessage.SourcesTypes[jef]);
                    jef++;
                }

                List <ClientId> existingIds = new List <ClientId>();
                lock (_syncRoot)
                {
                    existingIds.AddRange(_originalServerClientsHotSwap.Values);

                    _originalServerClientsHotSwap.Clear();
                    _originalServerClientsTypesHotSwap.Clear();
                    _originalServerClientsSourcesTypesHotNamesSwap.Clear();

                    // Preprocess Ids, by assigning them new indeces and adding to the local message bus register.
                    for (int i = 0; i < listMessage.Ids.Count; i++)
                    {
                        // Add an original copy to the list.
                        _originalServerClientsHotSwap.Add(listMessage.Ids[i].Guid, listMessage.Ids[i]);

                        _originalServerClientsTypesHotSwap.Add(listMessage.Ids[i].Guid, listMessage.Types[i]);
                        _originalServerClientsSourcesTypesHotNamesSwap.Add(listMessage.Ids[i].Guid, listMessage.SourcesTypes[i]);

                        // Add the client to a new spot.
                        //_clientsHotSwap.Add(null);
                        //int messageBusIndex = _clientsHotSwap.Count - 1;

                        // This type of assignment will also work with multiple entries.
                        // This performs an internal hotswap.
                        //_guidToIndexHotSwap[id.Guid] = messageBusIndex;

                        // Also add to this classes collection.
                        //_localToRemoteId[messageBusIndex] = id;
                    }
                }

                foreach (ClientId id in listMessage.Ids)
                {
                    existingIds.Remove(id);
                    RaiseClientAddedEvent(id);
                }

                // Raise for any that were removed.
                foreach (ClientId id in existingIds)
                {
                    RaiseClientRemovedEvent(id, true);
                }
            }
            else if (message is RequestClientListUpdateMessage)
            {
                SendClientsUpdate();
            }
            else if (message is ClientUpdateMessage)
            {
                ClientUpdateMessage updateMessage = (ClientUpdateMessage)message;

                if (_originalServerClientsHotSwap.ContainsKey(updateMessage.ClientId.Guid))
                {
                    RaiseClientUpdateEvent(updateMessage.ClientId);
                }
                else
                {
#if Matrix_Diagnostics
                    InstanceMonitor.OperationError(string.Format("Failed to raise update event for client [{0}], since client not found.", updateMessage.ClientId.ToString()));
#endif
                }
            }
            else if (message is StateUpdateMessage)
            {
                RaiseCounterPartyUpdateEvent("Server", ((StateUpdateMessage)message).State.ToString());
            }
            else
            {
#if Matrix_Diagnostics
                InstanceMonitor.Warning(string.Format("Message [{0}] not recognized.", message.GetType().Name));
#endif
            }
        }
Exemplo n.º 15
0
        private ReadResponse GetReadResponse()
        {
            object message = SocketCommunicator.Receive(_socket);

            return((ReadResponse)message);
        }
Exemplo n.º 16
0
 public void Begin()
 {
     SocketCommunicator.Send(_socket, new BeginMessage());
 }
Exemplo n.º 17
0
 public void End()
 {
     SocketCommunicator.Send(_socket, new EndMessage());
 }
Exemplo n.º 18
0
 void helper_ConnectedEvent(SocketCommunicator helper)
 {
     // This is never invoked, since we create the helpers directly on the connected sockets.
 }
Exemplo n.º 19
0
 public void Abort()
 {
     SocketCommunicator.Send(_socket, new AbortMessage());
 }
Exemplo n.º 20
0
 public void Restart()
 {
     SocketCommunicator.Send(_socket, new RestartMessage());
 }
Exemplo n.º 21
0
 public void Setup()
 {
     ConfigurationMock  = new Mock <IConfiguration>();
     CommandSerializer  = new CommandSerializer();
     SocketCommunicator = new SocketCommunicator <Command>(ConfigurationMock.Object, CommandSerializer);
 }
Exemplo n.º 22
0
        private EndStagingPhaseResponse GetEndStagingPhaseResponse()
        {
            object message = SocketCommunicator.Receive(_socket);

            return((EndStagingPhaseResponse)message);
        }
Exemplo n.º 23
0
 public Vote EndStagingPhase()
 {
     SocketCommunicator.Send(_socket, new EndStagingPhaseMessage());
     return(GetEndStagingPhaseResponse().Vote);
 }
Exemplo n.º 24
0
 public void StageChange(Change change)
 {
     SocketCommunicator.Send(_socket, new StageChangeMessage {
         Change = change
     });
 }
Exemplo n.º 25
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MazeServer"/> class.
 /// </summary>
 /// <param name="ip">The ip of the server.</param>
 /// <param name="port">The port of the server.</param>
 public MazeServer(string ip, int port)
 {
     this.server = new SocketCommunicator(ip, port);
 }
 // Start is called before the first frame update
 void Start()
 {
     socket   = new SocketCommunicator("127.0.0.1", 56789, 1024);
     dataList = new float[3];
     StartCoroutine(Communicate());
 }