Ejemplo n.º 1
0
 private void WriteStartCommand(StartCommand startCommand)
 {
     StringBuilder startCommandBuilder = new StringBuilder("START\n");
     startCommandBuilder.AppendFormat("name:{0}\n", startCommand.Name);
     startCommandBuilder.Append("\n");
     Write(startCommandBuilder.ToString());
 }
Ejemplo n.º 2
0
        public void Start()
        {
            Task.Factory.StartNew(() =>
            {
                try
                {
                    _control.Connect();

                    // Send PREPARE command
                    PrepareCommand prepareCommand = new PrepareCommand {Name = _name};

                    _control.WriteCommand(prepareCommand);

                    bool abortWritten = false;

                    while (true)
                    {
                        try
                        {
                            CommandEvent commandEvent = _control.ReadEvent(200);

                            switch (commandEvent.EventKind)
                            {
                                case CommandEvent.Kind.PREPARED:
                                    _latch.NotifyPrepared();

                                    StartCommand startCommand = new StartCommand { Name = _name };
                                    _control.WriteCommand(startCommand);
                                    break;
                                case CommandEvent.Kind.STARTED:
                                    _latch.NotifyStartable();
                                    break;
                                case CommandEvent.Kind.ERROR:
                                    ErrorEvent errorEvent = (ErrorEvent)commandEvent;
                                    throw new Exception(String.Format("{0}:{1}", errorEvent.Summary, errorEvent.Description));
                                case CommandEvent.Kind.FINISHED:
                                    FinishedEvent finishedEvent = commandEvent as FinishedEvent;
                                    _expectedScript = finishedEvent.ExpectedScript;
                                    _observedScript = finishedEvent.ObservedScript;
                                    _latch.NotifyFinished();
                                    return;
                                default:
                                    throw new InvalidOperationException("Unsupported event: " + commandEvent.EventKind);
                            }
                        }
                        catch (Exception ex)
                        {
                            if (_abortScheduled && !abortWritten)
                            {
                                abortWritten = true;
                                SendAbortCommand();
                            }
                        }
                    }

                }
                catch (SocketException socketException)
                {
                    Exception exception = new Exception("Failed to connect. Is the Robot running?", socketException);
                    _latch.NotifyException(exception);
                }
                catch (Exception exception) 
                {
                    _latch.NotifyException(exception);
                }
                finally
                {
                    _control.Disconnect();
                }
            });

            // Wait until robot server is ready to accept connections
            _latch.AwaitStartable();
        }