private string ParseDMessage(string inputLine)
        {
            UnityEngine.Debug.Log("OCMessageHandler::ParseDMessage");

            string answer = null;

            string contents = inputLine.Substring(1);

            if (_state == READING_MESSAGES)
            {
                if (_lineCount > 0)
                {
                    _message.Append("\n");
                }
                _message.Append(contents);
                _lineCount++;
            }
            else
            {
                OCLogger.Error("onLine: Unexpected dataline. Discarding line [" +
                               inputLine + "]");
                answer = OCNetworkElement.FAILED_MESSAGE;
            }

            return(answer);
        }
        public void UpdateMessagesSync(System.Net.Sockets.Socket workSocket)
        {
            UnityEngine.Debug.Log("OCMessageHandler::UpdateMessagesSync");

            StreamReader reader = null;
            StreamWriter writer = null;

            try
            {
                Stream s = new NetworkStream(workSocket);
                reader = new StreamReader(s);
                writer = new StreamWriter(s);
            }
            catch (IOException ioe)
            {
                workSocket.Close();
                OCLogger.Error("An I/O error occured.  [" + ioe.Message + "].");
            }

            bool endInput = false;

            while (!endInput)
            {
                try
                {
                    //@TODO Make some tests to judge the read time.
                    string line = reader.ReadLine();

                    if (line != null)
                    {
                        UnityEngine.Debug.Log("Parsing line: " + line);

                        string answer = Parse(line);
                    }
                    else
                    {
                        endInput = true;
                    }
                }
                catch (IOException ioe)
                {
                    UnityEngine.Debug.Log("An I/O error occured.  [" + ioe.Message + "].");
                    endInput = true;
                }
            }

            try
            {
                reader.Close();
                writer.Close();
                workSocket.Close();
            }
            catch (IOException ioe)
            {
                UnityEngine.Debug.Log("Something went wrong: " + ioe.Message);
                //OCLogger.Error("An I/O error occured.  [" + ioe.Message + "].");
                endInput = true;
            }
        }
Ejemplo n.º 3
0
        //---------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------

        #region Public Member Functions

        //---------------------------------------------------------------------------

        void Start()
        {
            _face = transform.Find("robotG/mainG/head_GEO");
            if (!_face)
            {
                OCLogger.Error("Face of the robot is not found");
            }
        }
Ejemplo n.º 4
0
        //---------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------

        #region Accessors and Mutators

        //---------------------------------------------------------------------------



        //---------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------

        #region Public Member Functions

        //---------------------------------------------------------------------------

        public static int AllocatePort()
        {
            int port = MIN_PORT_NUMBER;

            while (_usedPorts.Contains(port) && port < 65535)
            {
                port++;
            }

            if (port >= 65535)     // No ports are available
            {
                OCLogger.Error("No more ports available between " + MIN_PORT_NUMBER + " and " + port + ".");
                return(-1);
            }

            _usedPorts.Add(port);
            return(port);
        }
        private string ParseCMessage(string inputLine)
        {
            UnityEngine.Debug.Log("OCMessageHandler::ParseCMessage");

            string contents = inputLine.Substring(1);
            string answer   = null;

            string[]    tokenArr = contents.Split(' ');
            IEnumerator token    = tokenArr.GetEnumerator();

            token.MoveNext();
            string command = token.Current.ToString();

            if (command.Equals("NOTIFY_NEW_MESSAGE"))
            {
                answer = ParseNotifyNewMessage(token);
            }
            else if (command.Equals("UNAVAILABLE_ELEMENT"))
            {
                answer = ParseUnavailableElement(token);
            }
            else if (command.Equals("AVAILABLE_ELEMENT"))
            {
                answer = ParseAvailableElement(token);
            }
            else if (command.Equals("START_MESSAGE"))    // Parse a common message
            {
                answer = ParseStartMessage(inputLine, command, token);
            }
            else if (command.Equals("NO_MORE_MESSAGES"))
            {
                answer = ParseNoMoreMessages(inputLine, command, token);
            }
            else
            {
                OCLogger.Error("onLine: Unexpected command [" + command + "]. Discarding line [" + inputLine + "]");
                answer = OCNetworkElement.FAILED_MESSAGE;
            }     // end processing command.

            return(answer);
        }
Ejemplo n.º 6
0
        public void Stop()
        {
            _shouldStop = true;

            if (_listener == null)
            {
                UnityEngine.Debug.Log("_listener == null, nothing to call Stop on...");
            }
            else
            {
                try
                {
                    _listener.Stop();
                    _listener = null;
                }
                catch (SocketException se)
                {
                    OCLogger.Error(se.Message);
                }
            }
        }
        /// <summary>
        /// Parse a text line from message received.
        /// </summary>
        /// <param name='inputLine'>
        /// The raw data that received by server socket.
        /// </param>
        /// <returns>
        /// An 'OK' string if the line was successfully parsed,
        /// a 'FAILED' string if something went wrong,
        /// null if there is still more to parse.
        /// </returns>
        private string Parse(string inputLine)
        {
            UnityEngine.Debug.Log("OCMessageHandler::Parse (" + inputLine + ")");

            string answer = null;

            if (_networkElement == null)
            {
                _networkElement = OCNetworkElement.Instance;
            }

            UnityEngine.Debug.Log("_networkElement == null? I wonder..." + ((_networkElement == null) ? "yes...it is..." : "no...it isn't"));

//		if (_networkElement != null) {
//			//UnityEngine.Debug.Log ("OCMessageHandler is using a NetworkElement with ID " + _networkElement.VerificationGuid + "...");
//		}
//		else {
//			//UnityEngine.Debug.Log("_networkElement == null");
//		}

            char selector = inputLine[0];

            if (selector == 'c')
            {
                answer = ParseCMessage(inputLine);
            }
            else if (selector == 'd')
            {
                answer = ParseDMessage(inputLine);
            }
            else
            {
                OCLogger.Error("onLine: Invalid selector [" + selector
                               + "]. Discarding line [" + inputLine + "].");
                answer = OCNetworkElement.FAILED_MESSAGE;
            }

            return(answer);
        }
        private string ParseStartMessage(string inputLine, string command, IEnumerator token)
        {
            UnityEngine.Debug.Log("OCMessageHandler::ParseStartMessage");

            string answer = null;

            if (_state == READING_MESSAGES)
            {
                // A previous message was already read.
                OCLogger.Debugging("onLine: From [" + _messageFrom +
                                   "] to [" + _messageTo +
                                   "] Type [" + _messageType + "]");

                OCMessage message = OCMessage.CreateMessage(_messageFrom,
                                                            _messageTo,
                                                            _messageType,
                                                            _message.ToString());
                if (message == null)
                {
                    OCLogger.Error("Could not factory message from the following string: " +
                                   _message.ToString());
                }
                if (_useMessageBuffer)
                {
                    _messageBuffer.Add(message);
                    if (_messageBuffer.Count > _maxMessagesInBuffer)
                    {
                        _networkElement.PullMessage(_messageBuffer);
                        _messageBuffer.Clear();
                    }
                }
                else
                {
                    _networkElement.PullMessage(message);
                }

                _lineCount   = 0;
                _messageTo   = "";
                _messageFrom = "";
                _messageType = OCMessage.MessageType.NONE;
                _message.Remove(0, _message.Length);
            }
            else
            {
                if (_state == DOING_NOTHING)
                {
                    // Enter reading state from idle state.
                    _state = READING_MESSAGES;
                }
                else
                {
                    OCLogger.Error("onLine: Unexepcted command [" +
                                   command + "]. Discarding line [" +
                                   inputLine + "]");
                }
            }

            if (token.MoveNext())
            {
                _messageFrom = token.Current.ToString();

                if (token.MoveNext())
                {
                    _messageTo = token.Current.ToString();
                    if (token.MoveNext())
                    {
                        _messageType = (OCMessage.MessageType) int.Parse(token.Current.ToString());
                    }
                    else
                    {
                        answer = OCNetworkElement.FAILED_MESSAGE;
                    }
                }
                else
                {
                    answer = OCNetworkElement.FAILED_MESSAGE;
                }
            }
            else
            {
                answer = OCNetworkElement.FAILED_MESSAGE;
            }
            _lineCount = 0;

            return(answer);
        }
        //---------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------

        #region Public Member Functions

        //---------------------------------------------------------------------------


//	public IEnumerator StartProcessing(System.Net.Sockets.Socket workSocket)
//	{
//		UnityEngine.Debug.Log ("OCMessageHandler::StartProcessing");
//		yield return StartCoroutine(UpdateMessages(workSocket));
////		UnityEngine.Debug.Log ("BALLS!");
////		yield return null;
//	}

        public IEnumerator UpdateMessages(System.Net.Sockets.Socket workSocket)
        {
            UnityEngine.Debug.Log("OCMessageHandler::UpdateMessages");

            StreamReader reader = null;
            StreamWriter writer = null;

            try
            {
                Stream s = new NetworkStream(workSocket);
                reader = new StreamReader(s);
                writer = new StreamWriter(s);
            }
            catch (IOException ioe)
            {
                workSocket.Close();
                OCLogger.Error("An I/O error occured.  [" + ioe.Message + "].");
            }

            bool endInput = false;

            while (true)
            {
                while (!endInput)
                {
                    try
                    {
                        //@TODO Make some tests to judge the read time.
                        string line = reader.ReadLine();

                        if (line != null)
                        {
                            string answer = Parse(line);

                            //UnityEngine.Debug.Log ("Just parsed '" + line + "'");
                        }
                        else
                        {
                            UnityEngine.Debug.Log("No more input, line == null");

                            endInput = true;

                            //					UnityEngine.Debug.Log ("Setting OCNetworkElement.IsHandling to false...");
                            //
                            //					OCNetworkElement.Instance.IsHandlingMessages = false;
                        }
                    }
                    catch (IOException ioe)
                    {
                        UnityEngine.Debug.Log("An I/O error occured.  [" + ioe.Message + "].");
                        endInput = true;
                    }
                    catch (System.Exception ex)
                    {
                        UnityEngine.Debug.Log("A general error occured.  [" + ex.Message + "].");
                        endInput = true;
                    }

                    yield return(new UnityEngine.WaitForSeconds(0.1f));
                }

                if (endInput)
                {
                    yield return(new UnityEngine.WaitForSeconds(0.5f));

                    endInput = false;
                }
            }

            try
            {
                reader.Close();
                writer.Close();
                workSocket.Close();
            }
            catch (IOException ioe)
            {
                UnityEngine.Debug.Log("Something went wrong: " + ioe.Message);
                //OCLogger.Error("An I/O error occured.  [" + ioe.Message + "].");
                endInput = true;
            }

            yield return(null);
        }