Skip to content

Commit

Permalink
Use auto assigned port for InnerServer
Browse files Browse the repository at this point in the history
  • Loading branch information
bayandin authored and NickAb committed Oct 4, 2016
1 parent aa73102 commit 5975a11
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace WindowsPhoneDriver.Common
{
public class ConnectionInformation
{
#region Public Properties

public const string FileName = ".WindowsPhoneDriver.ConnectionInformation.json";

public string RemotePort { get; set; }

public override string ToString()
{
return string.Format("RemotePort: {0}", this.RemotePort);
}

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="CommandResponse.cs" />
<Compile Include="ConnectionInformation.cs" />
<Compile Include="DriverCommand.cs" />
<Compile Include="Exceptions\AutomationException.cs" />
<Compile Include="Exceptions\InnerDriverRequestException.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
namespace WindowsPhoneDriver.InnerDriver
{
using System;
using System.Globalization;
using System.Net;
using System.Threading.Tasks;
using System.Windows;

using Windows.Networking.Sockets;
using Windows.Storage;
using Windows.Storage.Streams;

using Newtonsoft.Json;

using WindowsPhoneDriver.Common;

public class AutomationServer
Expand All @@ -27,8 +29,6 @@ public class AutomationServer

private StreamSocketListener listener;

private int listeningPort;

#endregion

#region Public Methods and Operators
Expand All @@ -44,59 +44,51 @@ public class AutomationServer
public void InitializeAndStart(UIElement visualRoot)
{
this.SetAutomator(visualRoot);
this.Start(9998);
}

/// <summary>
/// Initializes and starts <see cref="AutomationServer"/> with specified parameters.
/// </summary>
/// <remarks>
/// Use it in conjuction with <see cref="Instance"/> to simplify inclusion of server in tested app.
/// </remarks>
/// <param name="visualRoot">
/// </param>
/// <param name="port">
/// </param>
public void InitializeAndStart(UIElement visualRoot, int port)
{
this.SetAutomator(visualRoot);
this.Start(port);
this.Start();
}

public void SetAutomator(UIElement visualRoot)
{
this.automator = new Automator(visualRoot);
}

public async void Start(int port)
public async void Start()
{
if (this.isServerActive)
{
return;
}

this.listeningPort = port;

this.isServerActive = true;
this.listener = new StreamSocketListener();
this.listener.Control.QualityOfService = SocketQualityOfService.Normal;
this.listener.ConnectionReceived += this.ListenerConnectionReceived;
await this.listener.BindServiceNameAsync(this.listeningPort.ToString(CultureInfo.InvariantCulture));
await this.listener.BindServiceNameAsync(string.Empty);
await this.WriteConnectionData();
this.isServerActive = true;
}

public void Stop()
{
if (this.isServerActive)
if (!this.isServerActive)
{
this.listener.Dispose();
this.isServerActive = false;
return;
}
this.listener.Dispose();
this.isServerActive = false;
}

#endregion

#region Methods

private async Task WriteConnectionData()
{
var file = await ApplicationData.Current.TemporaryFolder.CreateFileAsync(ConnectionInformation.FileName, CreationCollisionOption.ReplaceExisting);

var information = new ConnectionInformation { RemotePort = this.listener.Information.LocalPort };
await FileIO.WriteTextAsync(file, JsonConvert.SerializeObject(information));
}

private async void HandleRequest(StreamSocket socket)
{
var reader = new DataReader(socket.InputStream) { InputStreamOptions = InputStreamOptions.Partial };
Expand Down Expand Up @@ -128,7 +120,7 @@ private async void HandleRequest(StreamSocket socket)
}

private async void ListenerConnectionReceived(
StreamSocketListener sender,
StreamSocketListener sender,
StreamSocketListenerConnectionReceivedEventArgs args)
{
await Task.Run(() => this.HandleRequest(args.Socket));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ internal Capabilities()
this.LaunchDelay = 0;
this.LaunchTimeout = 10000;
this.DebugConnectToRunningApp = false;
this.InnerPort = 9998;
this.IsJavascriptEnabled = true;
}

Expand All @@ -40,9 +39,6 @@ public static string PlatformName
[JsonProperty("deviceName")]
public string DeviceName { get; set; }

[JsonProperty("innerPort")]
public int InnerPort { get; set; }

[JsonProperty("launchDelay")]
public int LaunchDelay { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

using System;
using System.Diagnostics;
using System.IO;
using System.Threading;

using Newtonsoft.Json;

using OpenQA.Selenium.Remote;

using WindowsPhoneDriver.Common;
using WindowsPhoneDriver.Common.Exceptions;
using WindowsPhoneDriver.OuterDriver.Automator;

#endregion
Expand All @@ -29,7 +31,10 @@ protected override string DoImpl()

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

this.Automator.CommandForwarder = new Requester(innerIp, this.Automator.ActualCapabilities.InnerPort);
var connectionInformation = this.GetConnectionInformation();
Logger.Info("Received connection information from device {0}", connectionInformation);

this.Automator.CommandForwarder = new Requester(innerIp, connectionInformation.RemotePort);

this.ConnectToApp();

Expand All @@ -43,6 +48,17 @@ protected override string DoImpl()
return jsonResponse;
}

private ConnectionInformation GetConnectionInformation()
{
var filePath = Path.GetTempFileName();
this.Automator.Deployer.ReceiveFile("Temp", ConnectionInformation.FileName, filePath);
using (var file = File.OpenText(filePath))
{
var serializer = new JsonSerializer();
return (ConnectionInformation)serializer.Deserialize(file, typeof(ConnectionInformation));
}
}

private void ConnectToApp()
{
long timeout = this.Automator.ActualCapabilities.LaunchTimeout;
Expand Down Expand Up @@ -70,6 +86,12 @@ private void ConnectToApp()
private string InitializeApplication(bool debugDoNotDeploy = false)
{
var appPath = this.Automator.ActualCapabilities.App;

if (string.IsNullOrWhiteSpace(appPath))
{
throw new AutomationException("app capability is not set.", ResponseStatus.SessionNotCreatedException);
}

this.Automator.Deployer = new Winium.Mobile.Connectivity.Deployer(this.Automator.ActualCapabilities.DeviceName, false);
if (!debugDoNotDeploy)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ internal class Requester

private readonly string ip;

private readonly int port;
private readonly string port;

#endregion

#region Constructors and Destructors

public Requester(string ip, int port)
public Requester(string ip, string port)
{
this.ip = ip;
this.port = port;
Expand Down

0 comments on commit 5975a11

Please sign in to comment.