Skip to content

Commit

Permalink
Added automatic resolution of binding based on protocol to ServerConn…
Browse files Browse the repository at this point in the history
…ection.
  • Loading branch information
csut017 committed Feb 21, 2011
1 parent 3c0deb4 commit b75b232
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 7 deletions.
Expand Up @@ -40,6 +40,7 @@
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.Xaml" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
Expand Down
64 changes: 63 additions & 1 deletion Playground/CruiseControl.Common.Tests/ServerConnectionTests.cs
@@ -1,5 +1,7 @@
namespace CruiseControl.Common.Tests
{
using System;
using System.ServiceModel;
using NUnit.Framework;

[TestFixture]
Expand All @@ -9,9 +11,69 @@ public class ServerConnectionTests
[Test]
public void PingReturnsFalseOnFailure()
{
var result = ServerConnection.Ping("net.tcp://nowhere");
var connection = new ServerConnection("net.tcp://nowhere");
var result = connection.Ping();
Assert.IsFalse(result);
}

[Test]
public void GenerateBindingFromProtocolFailsWhenAddressIsMissing()
{
var connection = new ServerConnection();
Assert.Throws<InvalidOperationException>(connection.GenerateBindingFromProtocol);
}

[Test]
public void GenerateBindingFromProtocolFailsWhenProtocolIsMissing()
{
var connection = new ServerConnection
{
Address = "somewhere.com"
};
Assert.Throws<InvalidOperationException>(connection.GenerateBindingFromProtocol);
}

[Test]
public void GenerateBindingFromProtocolFailsWithUnknownProtocol()
{
var connection = new ServerConnection
{
Address = "unknown://somewhere.com"
};
Assert.Throws<InvalidOperationException>(connection.GenerateBindingFromProtocol);
}

[Test]
public void ConstructorSetsProperties()
{
var binding = new NetTcpBinding();
var address = "net.tcp://somewhere";
var connection = new ServerConnection(address, binding);
Assert.AreEqual(connection.Address, address);
Assert.AreSame(binding, connection.Binding);
}

[Test]
public void GenerateBindingFromProtocolGeneratesBasicHttpForHttpProtocol()
{
var connection = new ServerConnection
{
Address = "http://somewhere"
};
connection.GenerateBindingFromProtocol();
Assert.IsInstanceOf<BasicHttpBinding>(connection.Binding);
}

[Test]
public void GenerateBindingFromProtocolGeneratesBasicHttpForNetTcpProtocol()
{
var connection = new ServerConnection
{
Address = "net.tcp://somewhere"
};
connection.GenerateBindingFromProtocol();
Assert.IsInstanceOf<NetTcpBinding>(connection.Binding);
}
#endregion
}
}
98 changes: 93 additions & 5 deletions Playground/CruiseControl.Common/ServerConnection.cs
Expand Up @@ -2,29 +2,83 @@
{
using System;
using System.ServiceModel;
using System.ServiceModel.Channels;

/// <summary>
/// A connection to a server.
/// </summary>
public class ServerConnection
{
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="ServerConnection"/> class.
/// </summary>
public ServerConnection()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ServerConnection"/> class.
/// </summary>
/// <param name="address">The address.</param>
public ServerConnection(string address)
: this(address, null)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ServerConnection"/> class.
/// </summary>
/// <param name="address">The address.</param>
/// <param name="binding">The binding.</param>
public ServerConnection(string address, Binding binding)
{
this.Address = address;
this.Binding = binding;
if (binding == null)
{
this.GenerateBindingFromProtocol();
}
}
#endregion

#region Public properties
#region Address
/// <summary>
/// Gets or sets the address.
/// </summary>
/// <value>
/// The address.
/// </value>
public string Address { get; set; }
#endregion

#region Binding
/// <summary>
/// Gets or sets the binding.
/// </summary>
/// <value>
/// The binding.
/// </value>
public Binding Binding { get; set; }
#endregion
#endregion

#region Public methods
#region Ping()
/// <summary>
/// Attempt to ping the server.
/// </summary>
/// <param name="uri">The URI for the server.</param>
/// <returns>
/// <c>true</c> if the ping was successful; <c>false</c> otherwise.
/// </returns>
public static bool Ping(string uri)
public bool Ping()
{
var binding = new NetTcpBinding();
var address = new EndpointAddress(uri);
var address = new EndpointAddress(this.Address);
try
{
var channel = ChannelFactory<ICommunicationsChannel>
.CreateChannel(binding, address);
.CreateChannel(this.Binding, address);
try
{
return channel.Ping();
Expand All @@ -44,6 +98,40 @@ public static bool Ping(string uri)
}
}
#endregion

#region GenerateBindingFromProtocol()
/// <summary>
/// Generates the binding from protocol.
/// </summary>
public void GenerateBindingFromProtocol()
{
if (string.IsNullOrEmpty(this.Address))
{
throw new InvalidOperationException("Cannot generate binding when address is not set");
}

var protocolLength = this.Address.IndexOf("://");
if (protocolLength < 0)
{
throw new InvalidOperationException("Unable to find protocol within address");
}

var protocol = this.Address.Substring(0, protocolLength).ToLowerInvariant();
switch (protocol)
{
case "http":
this.Binding = new BasicHttpBinding();
break;

case "net.tcp":
this.Binding = new NetTcpBinding();
break;

default:
throw new InvalidOperationException("Unknown protocol: " + protocol);
}
}
#endregion
#endregion
}
}
4 changes: 3 additions & 1 deletion Playground/CruiseControl.Core.Tests/Channels/WcfTests.cs
@@ -1,5 +1,6 @@
namespace CruiseControl.Core.Tests.Channels
{
using System.ServiceModel;
using CruiseControl.Common;
using CruiseControl.Core.Channels;
using CruiseControl.Core.Tests.Stubs;
Expand Down Expand Up @@ -38,7 +39,8 @@ public void InitialiseOpensTheWcfHost()
{
opened = channel.Initialise(null);
Assert.IsTrue(opened);
var canPing = ServerConnection.Ping(address);
var connection = new ServerConnection(address, new NetTcpBinding());
var canPing = connection.Ping();
Assert.IsTrue(canPing);
}
finally
Expand Down

0 comments on commit b75b232

Please sign in to comment.