private static bool SendPing(Session session)
 {
     Logger.Debug("Sending ping request to Test Server");
     var pingCmd = new Command("ping");
     var response = session.CommandForwarder.ForwardCommand(pingCmd, false, 2500);
     return response.Trim().Equals("pong");
 }
        public string ForwardCommand(Command commandToForward, bool verbose = true, int timeout = 0)
        {
            var serializedCommand = JsonConvert.SerializeObject(commandToForward);

            var response = this.SendRequest(serializedCommand, verbose, timeout);
            if (response.Key == HttpStatusCode.OK)
            {
                return response.Value;
            }

            throw new InnerDriverRequestException(response.Value, response.Key);
        }
        protected override string DoImpl()
        {
            // It is easier to reparse desired capabilities as JSON instead of re-mapping keys to attributes and calling type conversions, 
            // so we will take possible one time performance hit by serializing Dictionary and deserializing it as Capabilities object
            var serializedCapability =
                JsonConvert.SerializeObject(this.ExecutedCommand.Parameters["desiredCapabilities"]);
            this.Automator.ActualCapabilities = Capabilities.CapabilitiesFromJsonString(serializedCapability);

            var innerIp = this.InitializeApplication(this.Automator.ActualCapabilities.DebugConnectToRunningApp);

            this.Automator.CommandForwarder = new Requester(innerIp, this.Automator.ActualCapabilities.InnerPort);

            long timeout = this.Automator.ActualCapabilities.LaunchTimeout;

            const int PingStep = 500;
            var stopWatch = new Stopwatch();
            while (timeout > 0)
            {
                stopWatch.Restart();

                Logger.Trace("Ping inner driver");
                var pingCommand = new Command("ping");
                var responseBody = this.Automator.CommandForwarder.ForwardCommand(pingCommand, false, 2000);
                if (responseBody.StartsWith("<pong>", StringComparison.Ordinal))
                {
                    break;
                }

                Thread.Sleep(PingStep);
                stopWatch.Stop();
                timeout -= stopWatch.ElapsedMilliseconds;
            }

            // TODO throw AutomationException with SessionNotCreatedException if timeout and uninstall the app
            Console.WriteLine();

            // Gives sometime to load visuals (needed only in case of slow emulation)
            Thread.Sleep(this.Automator.ActualCapabilities.LaunchDelay);

            return this.JsonResponse(ResponseStatus.Success, this.Automator.ActualCapabilities);
        }
        public void Close()
        {
            if (this.ActualCapabilities.DebugCodedUi)
            {
                return;
            }

            if (this.CommandForwarder == null)
            {
                return;
            }

            try
            {
                var quitCommand = new Command(DriverCommand.Quit);
                this.CommandForwarder.ForwardCommand(quitCommand);
                this.DeviceBridge.Dispose();
            }
            catch (Exception e)
            {
                Logger.Error("Exception occured while trying to close session {0}", e);
            }
        }
 public static void CloseApp(Automator automator)
 {
     var remoteCommand = new Command(DriverCommand.CloseApp);
     automator.CommandForwarder.ForwardCommand(remoteCommand);
     Thread.Sleep(TimeSpan.FromMilliseconds(500));
 }
        private CommandResponse ProcessCommand(Command command)
        {
            Logger.Info("COMMAND {0}\r\n{1}", command.Name, command.Parameters.ToString());
            var executor = this.executorDispatcher.GetExecutor(command.Name);
            executor.ExecutedCommand = command;
            var response = executor.Do();
            Logger.Debug("RESPONSE:\r\n{0}", response);

            return response;
        }
        private string HandleRequest(HttpRequest acceptedRequest)
        {
            var firstHeaderTokens = acceptedRequest.StartingLine.Split(' ');
            var method = firstHeaderTokens[0];
            var resourcePath = firstHeaderTokens[1];

            var uriToMatch = new Uri(this.baseAddress, resourcePath);
            var matched = this.dispatcher.Match(method, uriToMatch);

            if (matched == null)
            {
                Logger.Warn("Unknown command recived: {0}", uriToMatch);
                return HttpResponseHelper.ResponseString(HttpStatusCode.NotFound, "Unknown command " + uriToMatch);
            }

            var commandName = matched.Data.ToString();
            var commandToExecute = new Command(commandName, acceptedRequest.MessageBody);
            foreach (string variableName in matched.BoundVariables.Keys)
            {
                commandToExecute.Parameters[variableName] = matched.BoundVariables[variableName];
            }

            var commandResponse = this.ProcessCommand(commandToExecute);
            return HttpResponseHelper.ResponseString(commandResponse.HttpStatusCode, commandResponse.Content);
        }