Exemplo n.º 1
0
 public override void Initialize()
 {
     if (SupportedCommands.Contains(DeviceCommands.GetStatus))
     {
         GetStatus();
     }
 }
Exemplo n.º 2
0
        protected Command(char identifier)
        {
            if (!SupportedCommands.Contains(identifier))
            {
                throw new UnsupportedCommandIdentiferException(identifier);
            }

            Identifier = identifier;
        }
Exemplo n.º 3
0
        public virtual void PaperCut()
        {
            if (SupportedCommands.Contains(DeviceCommands.GetStatus))
            {
                GetStatus();
            }

            SendMessage(defaultEnc.GetBytes("\n \n \n \n\x1B\x69"));
        }
Exemplo n.º 4
0
        private void SetCommand(string commandName)
        {
            if (!SupportedCommands.Contains(commandName, StringComparer.CurrentCultureIgnoreCase))
            {
                BadOption("command");
            }
            ;

            CommandName = commandName;
        }
Exemplo n.º 5
0
 public override async Task <BurnResult> BurnAsync(Command request, IEnumerable <IDataEntity> entities, DeviceOperationScope scope, AsyncOperationInfo cancellation)
 {
     if (SupportedCommands.Contains(request))
     {
         return(await _base.BurnAsync(request, entities, scope, cancellation));
     }
     else
     {
         return(new BurnResult(BurnStatus.NOT_SUPPORTED_BY_INTERFACE, ResponseData.NONE));
     }
 }
Exemplo n.º 6
0
 public override async Task <ReadResult> ReadAsync(Command request, DeviceOperationScope scope, AsyncOperationInfo cancellation)
 {
     if (SupportedCommands.Contains(request))
     {
         return(await _base.ReadAsync(request, scope, cancellation));
     }
     else
     {
         return(new ReadResult(ReadStatus.NOT_SUPPORTED_BY_INTERFACE, Enumerable.Empty <IDataEntity>(), ResponseData.NONE));
     }
 }
Exemplo n.º 7
0
        protected Command(byte commandNumber, IEnumerable <byte> data)
        {
            if (!SupportedCommands.Contains(commandNumber))
            {
                throw new InvalidOperationException("Unsupported command: " + commandNumber);
            }

            ValidateCommand(commandNumber, data);

            CommandNumber = commandNumber;
            Data          = data;
        }
Exemplo n.º 8
0
        public bool UseUTF8()
        {
            if (SupportedCommands.Contains("UTF8"))
            {
                SendCommandReadAnswer("OPTS UTF8 ON");

                if (ConnectionType == 200)
                {
                    return(true);
                }
            }
            return(false);
        }
Exemplo n.º 9
0
        public virtual void PrintFreeText(string text)
        {
            if (SupportedCommands.Contains(DeviceCommands.GetStatus))
            {
                GetStatus();
            }

            text  = text.Replace("\r\n", "\n");
            text  = text.Replace("\r", "\n");
            text  = string.Join("\n", text.Wrap(textCharsPerLine, 20));
            text += "\n";

            SendMessage(GetTextBytes(text));
        }
Exemplo n.º 10
0
        private void _useExplicitSSL()
        {
            if (!SupportedCommands.Contains("AUTH TLS"))
            {
                return;
            }

            if (ActiveClient != null)
            {
                string answer = SendCommandReadAnswer("AUTH TLS");

                if (ConnectionType == 234)
                {
                    SslStream sslStream = new SslStream(ActiveClient.GetStream(), false, _validateServerCertificate);

                    sslStream.AuthenticateAsClient(realServer);
                    _activeClientStream = sslStream;
                    ActiveClientReader  = new BinaryReader(_activeClientStream);
                    ActiveClientWriter  = new StreamWriter(_activeClientStream);
                }
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// Executes a command and returns a corresponding response.
        /// </summary>
        /// <param name="command">The command.</param>
        protected virtual Response ExecuteCommand(Command command)
        {
            switch (command.Name)
            {
            case "protocol_version":
                return(new Response(command, "2"));

            case "name":
                return(new Response(command, this.Name.Replace('\n', ' ')));

            case "version":
                return(new Response(command, this.Version.Replace('\n', ' ')));

            case "list_commands":
                return(new Response(command, string.Join("\n", this.SupportedCommands)));

            case "known_command":
                return(new Response(
                           command,
                           command.Arguments.Count > 0 && SupportedCommands.Contains(command.Arguments[0]) ? "true" : "false"
                           ));

            case "quit":
                return(new Response(command));

            case "boardsize":
                try {
                    int size = int.Parse(command.Arguments[0]);
                    if (size > 25)
                    {
                        throw new Exception();
                    }
                    this.Board = new Board(size);
                    return(new Response(command));
                } catch {}

                return(new Response(command, "unacceptable size", true));

            case "clear_board":
                this.MoveHistory.Clear();
                this.Board.Clear();

                return(new Response(command));

            case "komi":
                try {
                    this.Komi = float.Parse(command.Arguments[0]);
                    return(new Response(command));
                } catch {}

                return(new Response(command, "syntax error", true));

            case "fixed_handicap":
                if (!this.Board.IsEmpty())
                {
                    return(new Response(command, "board not empty", true));
                }

                try {
                    int count = int.Parse(command.Arguments[0]);
                    if (count < 2 || count > 9 || this.Board.Size < 7)
                    {
                        return(new Response(command, "invalid number of stones", true));
                    }

                    foreach (Vertex v in Board.GetHandicapPlacement(count))
                    {
                        Board[v] = 1;
                    }
                    return(new Response(command));
                } catch {}

                return(new Response(command, "syntax error", true));

            case "set_free_handicap":
                if (!this.Board.IsEmpty())
                {
                    return(new Response(command, "board not empty", true));
                }

                try {
                    List <Vertex> vs = new List <Vertex>();
                    foreach (string input in command.Arguments)
                    {
                        Vertex v = new Vertex(input);
                        if (v == Vertex.Pass || vs.Contains(v))
                        {
                            return(new Response(command, "bad vertex list", true));
                        }
                        vs.Add(v);
                    }

                    foreach (Vertex v in vs)
                    {
                        this.Board[v] = 1;
                    }
                    return(new Response(command));
                } catch {}

                return(new Response(command, "syntax error", true));

            case "play":
                try {
                    Move move = Move.Parse(string.Join(" ", command.Arguments));
                    this.Play(move);
                    return(new Response(command));
                } catch (FormatException) {
                    return(new Response(command, "syntax error", true));
                } catch (InvalidOperationException) {
                    return(new Response(command, "illegal move", true));
                }

            case "genmove":
                try {
                    Color  color  = (Color)Enum.Parse(typeof(Color), command.Arguments[0], true);
                    Vertex?vertex = GenerateMove(color);

                    if (vertex.HasValue)
                    {
                        // Make move and add to move history
                        Move move = new Move(color, vertex.Value);
                        this.Play(move);
                    }

                    return(new Response(command, vertex.HasValue ? vertex.ToString() : "resign"));
                } catch {}

                return(new Response(command, "syntax error", true));

            case "undo":
                if (MoveHistory.Count == 0)
                {
                    return(new Response(command, "cannot undo", true));
                }

                Tuple <Move, Board> tuple = MoveHistory.Pop();
                this.Board = tuple.Item2;

                return(new Response(command));

            case "showboard":
                string result = this.Board.ToString() + "\n-\n";
                result += "(X) captured " + this.Board.Captures[Color.Black] + "\n";
                result += "(O) captured " + this.Board.Captures[Color.White];
                return(new Response(command, result));
            }

            return(new Response(command, "unknown command", true));
        }
Exemplo n.º 12
0
 public bool IsSupportedCommand(Command command)
 {
     return(SupportedCommands.Contains(command));
 }
Exemplo n.º 13
0
        public override async Task <BurnResult> BurnAsync(Command request, IEnumerable <IDataEntity> entities, DeviceOperationScope scope, AsyncOperationInfo cancellation)
        {
            var baseResult = await base.BurnAsync(request, entities, scope, cancellation);

            if (baseResult.Status == BurnStatus.OK &&
                request.GetInfo().WriteResponse == Attributes.WriteResponse.RICH)
            {
                var richResponse = new RichResponse(baseResult.ResponseSections[SalachovProtocol.BODY_SECTION_KEY]);
                if (richResponse.Type == RichResponseType.OPERATION_STATUS)
                {
                    if (richResponse.StatusResponseData != null)
                    {
                        switch (richResponse.StatusResponseData.Status)
                        {
                        case LastWriteRequestStatus.EXECUTED:
                            return(baseResult);

                        case LastWriteRequestStatus.ERROR:
                            return(new BurnResult(BurnStatus.ERROR_ON_DEVICE, baseResult.Response, baseResult.ResponseSections));

                        case LastWriteRequestStatus.PROCESSING:
                        {
                            var result = await waitTillReadyAsync();

                            return(new BurnResult(result, baseResult.Response, baseResult.ResponseSections));
                        }

                        case LastWriteRequestStatus.BUSY:
                        {
                            var result = await waitTillReadyAsync();

                            if (result == BurnStatus.OK)
                            {
                                return(await BurnAsync(request, entities, scope, cancellation));
                            }
                        }
                        break;

                        default:
                            throw new NotSupportedException();
                        }
                    }
                }
            }

            return(baseResult);

            async Task <BurnStatus> waitTillReadyAsync()
            {
                var isCheckSupported = SupportedCommands.Contains(Command.PING);

                if (isCheckSupported)
                {
                    var period = new PeriodDelay(PING_SEND_PERIOD);
                    while (true)
                    {
                        await period.WaitTimeLeftAsync(cancellation);

                        var pingResult = await ReadAsync(Command.PING, scope, cancellation);

                        if (pingResult.Status == ReadStatus.OK)
                        {
                            var status = new RichResponse(pingResult.AnswerSections[SalachovProtocol.BODY_SECTION_KEY]).StatusResponseData;
                            if (status.Status.IsOneOf(LastWriteRequestStatus.BUSY, LastWriteRequestStatus.PROCESSING))
                            {
                                continue;
                            }
                            else if (status.Status == LastWriteRequestStatus.EXECUTED)
                            {
                                return(BurnStatus.OK);
                            }
                            else
                            {
                                return(BurnStatus.COULD_NOT_COMPLETE_WAIT_OPERATION);
                            }
                        }
                        else
                        {
                            return(BurnStatus.COULD_NOT_COMPLETE_WAIT_OPERATION);
                        }
                    }
                }
                else
                {
                    return(BurnStatus.COULD_NOT_COMPLETE_WAIT_OPERATION);
                }
            }
        }