Ejemplo n.º 1
0
        private void OnSocketMessageAvailable(ProsthesisMessage message, TCP.ConnectionState state)
        {
            //Only one message allowed in at a time
            lock (this)
            {
                ProsthesisStateBase newState = mCurrentState;
                //Capture handshakes and send appropriate events
                if (message is ProsthesisHandshakeRequest)
                {
                    ProsthesisHandshakeRequest hsReq = message as ProsthesisHandshakeRequest;
                    if (hsReq.VersionId == ProsthesisCore.ProsthesisConstants.OSVersion)
                    {
                        newState = mCurrentState.OnClientAuthorization(state, true);
                        mAuthorizedConnection = state;
                    }
                    else
                    {
                        newState = mCurrentState.OnClientAuthorization(state, false);
                    }
                }
                else if (state == mAuthorizedConnection)
                {
                    //Capture commands and send appropriate events
                    if (message is ProsthesisCommand)
                    {
                        ProsthesisCommand command = message as ProsthesisCommand;
                        mLogger.LogMessage(Logger.LoggerChannels.Events, string.Format("Received command {0} from {1}", command.Command, state.RemoteEndPoint));


                        ProsthesisCommandAck ack = new ProsthesisCommandAck();
                        ack.Command   = command.Command;
                        ack.Timestamp = System.DateTime.Now.Ticks;
                        ProsthesisDataPacket pack = ProsthesisDataPacket.BoxMessage <ProsthesisCommandAck>(ack);

                        state._conn.Send(pack.Bytes, pack.Bytes.Length, System.Net.Sockets.SocketFlags.None);

                        if (command.Command == ProsthesisConstants.ProsthesisCommand.EmergencyStop)
                        {
                            Terminate(string.Format("Emergency stop from {0}", state.RemoteEndPoint));
                        }
                        else
                        {
                            newState = mCurrentState.OnProsthesisCommand(command.Command, state);
                        }
                    }
                    else
                    {
                        newState = mCurrentState.OnSocketMessage(message, state);
                    }
                }
                else
                {
                    state._server.DropConnection(state);
                }

                ChangeState(newState);
            }
        }
Ejemplo n.º 2
0
        public override ProsthesisStateBase OnSocketMessage(ProsthesisCore.Messages.ProsthesisMessage message, TCP.ConnectionState state)
        {
            ProsthesisStateBase newState = mCurrentState.OnSocketMessage(message, state);

            if (newState != mCurrentState)
            {
                ChangeState(newState);
            }
            return(this);
        }
Ejemplo n.º 3
0
        public void ChangeState(ProsthesisStateBase to)
        {
            if (!mRunning)
            {
                return;
            }

            if (mDeferredStateChange != null)
            {
                mDeferredStateChange = to;
                return;
            }

            ProsthesisStateBase oldState = mCurrentState;

            if (mCurrentState != to)
            {
                if (mCurrentState != null)
                {
                    mCurrentState.OnExit();
                }

                if (to != null)
                {
                    mDeferredStateChange = to.OnEnter();
                }
                mCurrentState = to;

                if (mRunning)
                {
                    mLogger.LogMessage(Logger.LoggerChannels.StateMachine, string.Format("Changing state from {0} to {1}",
                                                                                         oldState != null ? oldState.GetType().ToString() : "<none>",
                                                                                         to != null ? to.GetType().ToString() : "<none>"));

                    if (StateChanged != null)
                    {
                        StateChanged(oldState, mCurrentState);
                    }
                }
            }

            if (mDeferredStateChange != null && mRunning)
            {
                ProsthesisStateBase nextState = mDeferredStateChange;
                mDeferredStateChange = null;
                ChangeState(nextState);
            }
        }
Ejemplo n.º 4
0
        public override ProsthesisStateBase OnProsthesisCommand(ProsthesisCore.ProsthesisConstants.ProsthesisCommand command, TCP.ConnectionState from)
        {
            switch (command)
            {
            case ProsthesisCore.ProsthesisConstants.ProsthesisCommand.Shutdown:
                return(new Shutdown(this));

            default:
            {
                ProsthesisStateBase newState = mCurrentState.OnProsthesisCommand(command, from);
                if (newState != mCurrentState)
                {
                    ChangeState(newState);
                }
            }
            break;
            }

            return(this);
        }
Ejemplo n.º 5
0
        private void OnDisconnection(TCP.ConnectionState state)
        {
            ProsthesisStateBase newState = mCurrentState.OnDisconnection(state);

            ChangeState(newState);
        }