Skip to content

Commit 14553ca

Browse files
Extract IConnectionFactory and ConnectionFactoryBase
1 parent c12db2f commit 14553ca

File tree

11 files changed

+190
-33
lines changed

11 files changed

+190
-33
lines changed

projects/client/RabbitMQ.Client/src/client/api/AuthMechanism.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@ namespace RabbitMQ.Client
4444
///<summary>A pluggable authentication mechanism.</summary>
4545
public interface AuthMechanism {
4646
///<summary>Handle one round of challenge-response</summary>
47-
byte[] handleChallenge(byte[] challenge, ConnectionFactory factory);
47+
byte[] handleChallenge(byte[] challenge, IConnectionFactory factory);
4848
}
4949
}

projects/client/RabbitMQ.Client/src/client/api/ConnectionFactory.cs

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ namespace RabbitMQ.Client
5757
/// A simple example of connecting to a broker:
5858
///</para>
5959
///<example><code>
60-
/// ConnectionFactory factory = new ConnectionFactory();
60+
/// IConnectionFactory factory = new ConnectionFactory();
6161
/// //
6262
/// // The next six lines are optional:
6363
/// factory.UserName = ConnectionFactory.DefaultUser;
@@ -94,7 +94,7 @@ namespace RabbitMQ.Client
9494
///"amqp://foo/" (note the trailling slash) also represent the
9595
///default virtual host. The latter issue means that virtual
9696
///hosts with an empty name are not addressable. </para></remarks>
97-
public class ConnectionFactory
97+
public class ConnectionFactory : ConnectionFactoryBase, IConnectionFactory
9898
{
9999
/// <summary>Default user name (value: "guest")</summary>
100100
public const string DefaultUser = "guest"; // PLEASE KEEP THIS MATCHING THE DOC ABOVE
@@ -125,35 +125,76 @@ public class ConnectionFactory
125125
public static AuthMechanismFactory[] DefaultAuthMechanisms =
126126
new AuthMechanismFactory[] { new PlainMechanismFactory() };
127127

128+
private string m_username = DefaultUser;
128129
/// <summary>Username to use when authenticating to the server</summary>
129-
public string UserName = DefaultUser;
130+
public string UserName
131+
{
132+
get { return m_username; }
133+
set { m_username = value; }
134+
}
130135

136+
private string m_password = DefaultPass;
131137
/// <summary>Password to use when authenticating to the server</summary>
132-
public string Password = DefaultPass;
138+
public string Password
139+
{
140+
get { return m_password; }
141+
set { m_password = value; }
142+
}
133143

144+
private string m_vhost = DefaultVHost;
134145
/// <summary>Virtual host to access during this connection</summary>
135-
public string VirtualHost = DefaultVHost;
146+
public string VirtualHost
147+
{
148+
get { return m_vhost; }
149+
set { m_vhost = value; }
150+
}
136151

152+
private ushort m_requestedChannelMax = DefaultChannelMax;
137153
/// <summary>Maximum channel number to ask for</summary>
138-
public ushort RequestedChannelMax = DefaultChannelMax;
154+
public ushort RequestedChannelMax
155+
{
156+
get { return m_requestedChannelMax; }
157+
set { m_requestedChannelMax = value; }
158+
}
139159

160+
private uint m_requestedFrameMax = DefaultFrameMax;
140161
/// <summary>Frame-max parameter to ask for (in bytes)</summary>
141-
public uint RequestedFrameMax = DefaultFrameMax;
162+
public uint RequestedFrameMax
163+
{
164+
get { return m_requestedFrameMax; }
165+
set { m_requestedFrameMax = value; }
166+
}
142167

168+
private ushort m_requestedHeartbeat = DefaultHeartbeat;
143169
/// <summary>Heartbeat setting to request (in seconds)</summary>
144-
public ushort RequestedHeartbeat = DefaultHeartbeat;
170+
public ushort RequestedHeartbeat
171+
{
172+
get { return m_requestedHeartbeat; }
173+
set { m_requestedHeartbeat = value; }
174+
}
145175

146176
/// <summary>Timeout setting for connection attempts (in milliseconds)</summary>
147177
public int RequestedConnectionTimeout = DefaultConnectionTimeout;
148178

179+
private bool m_useBackgroundThreadsForIO = false;
149180
/// <summary>
150181
/// When set to true, background threads will be used for I/O and heartbeats.
151182
/// </summary>
152-
public bool UseBackgroundThreadsForIO = false;
183+
public bool UseBackgroundThreadsForIO
184+
{
185+
get { return m_useBackgroundThreadsForIO; }
186+
set { m_useBackgroundThreadsForIO = value; }
187+
}
153188

189+
private IDictionary<string, object> m_clientProperties =
190+
ConnectionBase.DefaultClientProperties();
154191
/// <summary>Dictionary of client properties to be sent to the
155192
/// server</summary>
156-
public IDictionary<string, object> ClientProperties = ConnectionBase.DefaultClientProperties();
193+
public IDictionary<string, object> ClientProperties
194+
{
195+
get { return m_clientProperties; }
196+
set { m_clientProperties = value; }
197+
}
157198

158199
///<summary>Ssl options setting</summary>
159200
public SslOption Ssl = new SslOption();
@@ -199,11 +240,6 @@ public String Uri
199240
set { SetUri(new Uri(value, UriKind.Absolute)); }
200241
}
201242

202-
public delegate TcpClient ObtainSocket(AddressFamily addressFamily);
203-
204-
///<summary>Set custom socket options by providing a SocketFactory</summary>
205-
public ObtainSocket SocketFactory = DefaultSocketFactory;
206-
207243
///<summary>Construct a fresh instance, with all fields set to
208244
///their respective defaults.</summary>
209245
public ConnectionFactory() { }
@@ -242,12 +278,6 @@ public AuthMechanismFactory AuthMechanismFactory(string[] mechs) {
242278
return null;
243279
}
244280

245-
public static TcpClient DefaultSocketFactory(AddressFamily addressFamily)
246-
{
247-
TcpClient tcpClient = new TcpClient(addressFamily);
248-
tcpClient.NoDelay = true;
249-
return tcpClient;
250-
}
251281

252282
private void SetUri(Uri uri)
253283
{
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// This source code is dual-licensed under the Apache License, version
2+
// 2.0, and the Mozilla Public License, version 1.1.
3+
//
4+
// The APL v2.0:
5+
//
6+
//---------------------------------------------------------------------------
7+
// Copyright (C) 2007-2014 GoPivotal, Inc.
8+
//
9+
// Licensed under the Apache License, Version 2.0 (the "License");
10+
// you may not use this file except in compliance with the License.
11+
// You may obtain a copy of the License at
12+
//
13+
// http://www.apache.org/licenses/LICENSE-2.0
14+
//
15+
// Unless required by applicable law or agreed to in writing, software
16+
// distributed under the License is distributed on an "AS IS" BASIS,
17+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
// See the License for the specific language governing permissions and
19+
// limitations under the License.
20+
//---------------------------------------------------------------------------
21+
//
22+
// The MPL v1.1:
23+
//
24+
//---------------------------------------------------------------------------
25+
// The contents of this file are subject to the Mozilla Public License
26+
// Version 1.1 (the "License"); you may not use this file except in
27+
// compliance with the License. You may obtain a copy of the License
28+
// at http://www.mozilla.org/MPL/
29+
//
30+
// Software distributed under the License is distributed on an "AS IS"
31+
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
32+
// the License for the specific language governing rights and
33+
// limitations under the License.
34+
//
35+
// The Original Code is RabbitMQ.
36+
//
37+
// The Initial Developer of the Original Code is GoPivotal, Inc.
38+
// Copyright (c) 2007-2014 GoPivotal, Inc. All rights reserved.
39+
//---------------------------------------------------------------------------
40+
41+
using System.Net.Sockets;
42+
43+
namespace RabbitMQ.Client
44+
{
45+
public class ConnectionFactoryBase
46+
{
47+
public delegate TcpClient ObtainSocket(AddressFamily addressFamily);
48+
49+
///<summary>Set custom socket options by providing a SocketFactory</summary>
50+
public ObtainSocket SocketFactory = DefaultSocketFactory;
51+
52+
public static TcpClient DefaultSocketFactory(AddressFamily addressFamily)
53+
{
54+
TcpClient tcpClient = new TcpClient(addressFamily);
55+
tcpClient.NoDelay = true;
56+
return tcpClient;
57+
}
58+
}
59+
}

projects/client/RabbitMQ.Client/src/client/api/ExternalMechanism.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
namespace RabbitMQ.Client
4545
{
4646
public class ExternalMechanism : AuthMechanism {
47-
public byte[] handleChallenge(byte[] challenge, ConnectionFactory factory) {
47+
public byte[] handleChallenge(byte[] challenge, IConnectionFactory factory) {
4848

4949
return new byte[0];
5050
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// This source code is dual-licensed under the Apache License, version
2+
// 2.0, and the Mozilla Public License, version 1.1.
3+
//
4+
// The APL v2.0:
5+
//
6+
//---------------------------------------------------------------------------
7+
// Copyright (C) 2007-2014 GoPivotal, Inc.
8+
//
9+
// Licensed under the Apache License, Version 2.0 (the "License");
10+
// you may not use this file except in compliance with the License.
11+
// You may obtain a copy of the License at
12+
//
13+
// http://www.apache.org/licenses/LICENSE-2.0
14+
//
15+
// Unless required by applicable law or agreed to in writing, software
16+
// distributed under the License is distributed on an "AS IS" BASIS,
17+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
// See the License for the specific language governing permissions and
19+
// limitations under the License.
20+
//---------------------------------------------------------------------------
21+
//
22+
// The MPL v1.1:
23+
//
24+
//---------------------------------------------------------------------------
25+
// The contents of this file are subject to the Mozilla Public License
26+
// Version 1.1 (the "License"); you may not use this file except in
27+
// compliance with the License. You may obtain a copy of the License
28+
// at http://www.mozilla.org/MPL/
29+
//
30+
// Software distributed under the License is distributed on an "AS IS"
31+
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
32+
// the License for the specific language governing rights and
33+
// limitations under the License.
34+
//
35+
// The Original Code is RabbitMQ.
36+
//
37+
// The Initial Developer of the Original Code is GoPivotal, Inc.
38+
// Copyright (c) 2007-2014 GoPivotal, Inc. All rights reserved.
39+
//---------------------------------------------------------------------------
40+
41+
using System;
42+
using System.Collections.Generic;
43+
44+
namespace RabbitMQ.Client
45+
{
46+
public interface IConnectionFactory
47+
{
48+
IConnection CreateConnection();
49+
50+
string UserName { get; set; }
51+
52+
string Password { get; set; }
53+
54+
string VirtualHost { get; set; }
55+
56+
ushort RequestedChannelMax { get; set; }
57+
58+
uint RequestedFrameMax { get; set; }
59+
60+
ushort RequestedHeartbeat { get; set; }
61+
62+
bool UseBackgroundThreadsForIO { get; set; }
63+
64+
IDictionary<String, object> ClientProperties { get; set; }
65+
66+
AuthMechanismFactory AuthMechanismFactory(string[] mechs);
67+
}
68+
}

projects/client/RabbitMQ.Client/src/client/api/IProtocol.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ public interface IProtocol
6161

6262
///<summary>Construct a frame handler for a given endpoint.</summary>
6363
IFrameHandler CreateFrameHandler(AmqpTcpEndpoint endpoint,
64-
ConnectionFactory.ObtainSocket socketFactory,
64+
ConnectionFactoryBase.ObtainSocket socketFactory,
6565
int timeout);
6666
///<summary>Construct a connection from a given set of
6767
///parameters and a frame handler. The "insist" parameter is
6868
///passed on to the AMQP connection.open method.</summary>
69-
IConnection CreateConnection(ConnectionFactory factory,
69+
IConnection CreateConnection(IConnectionFactory factory,
7070
bool insist,
7171
IFrameHandler frameHandler);
7272
///<summary>Construct a protocol model atop a given session.</summary>

projects/client/RabbitMQ.Client/src/client/api/PlainMechanism.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
namespace RabbitMQ.Client
4545
{
4646
public class PlainMechanism : AuthMechanism {
47-
public byte[] handleChallenge(byte[] challenge, ConnectionFactory factory) {
47+
public byte[] handleChallenge(byte[] challenge, IConnectionFactory factory) {
4848

4949
return Encoding.UTF8.GetBytes("\0" + factory.UserName +
5050
"\0" + factory.Password);

projects/client/RabbitMQ.Client/src/client/impl/ConnectionBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public abstract class ConnectionBase : IConnection
7070
///complete (milliseconds)</summary>
7171
public static int HandshakeTimeout = 10000;
7272

73-
public ConnectionFactory m_factory;
73+
public IConnectionFactory m_factory;
7474
public IFrameHandler m_frameHandler;
7575
public uint m_frameMax = 0;
7676
public ushort m_heartbeat = 0;
@@ -105,7 +105,7 @@ public abstract class ConnectionBase : IConnection
105105

106106
public IList<ShutdownReportEntry> m_shutdownReport = new SynchronizedList<ShutdownReportEntry>(new List<ShutdownReportEntry>());
107107

108-
public ConnectionBase(ConnectionFactory factory,
108+
public ConnectionBase(IConnectionFactory factory,
109109
bool insist,
110110
IFrameHandler frameHandler)
111111
{

projects/client/RabbitMQ.Client/src/client/impl/SocketFrameHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public class SocketFrameHandler : IFrameHandler
6464
private Object m_semaphore = new object();
6565

6666
public SocketFrameHandler(AmqpTcpEndpoint endpoint,
67-
ConnectionFactory.ObtainSocket socketFactory,
67+
ConnectionFactoryBase.ObtainSocket socketFactory,
6868
int timeout)
6969
{
7070
m_endpoint = endpoint;

projects/client/RabbitMQ.Client/src/client/impl/v0_9_1/Connection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444
namespace RabbitMQ.Client.Framing.Impl.v0_9_1 {
4545
public class Connection: ConnectionBase {
46-
public Connection(ConnectionFactory factory, bool insist, IFrameHandler frameHandler)
46+
public Connection(IConnectionFactory factory, bool insist, IFrameHandler frameHandler)
4747
: base(factory, insist, frameHandler) {}
4848

4949
public override void Open(bool insist)

0 commit comments

Comments
 (0)