added support for exceptions and for IConnectionFactory

git-svn-id: https://svn.apache.org/repos/asf/incubator/activemq/trunk@367642 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
James Strachan 2006-01-10 15:28:53 +00:00
parent 2ba813b311
commit a47deab320
10 changed files with 176 additions and 11 deletions

View File

@ -0,0 +1,17 @@
using System;
using System.Collections;
using OpenWire.Client.Commands;
using OpenWire.Client.Core;
namespace OpenWire.Client {
/// <summary>
/// Exception thrown when the broker returns an error
/// </summary>
public class BrokerException : OpenWireException {
public BrokerException(BrokerError cause) : base("The operation failed: Type: "
+ cause.ExceptionClass
+ " stack: "
+ cause.StackTrace) {
}
}
}

View File

@ -19,7 +19,7 @@ namespace OpenWire.Client.Commands
{
public const byte ID_ConnectionError = 16;
byte[] exception;
BrokerError exception;
ConnectionId connectionId;
@ -36,7 +36,7 @@ namespace OpenWire.Client.Commands
// Properties
public byte[] Exception
public BrokerError Exception
{
get { return exception; }
set { this.exception = value; }

View File

@ -19,7 +19,7 @@ namespace OpenWire.Client.Commands
{
public const byte ID_ExceptionResponse = 31;
byte[] exception;
BrokerError exception;
@ -35,7 +35,7 @@ namespace OpenWire.Client.Commands
// Properties
public byte[] Exception
public BrokerError Exception
{
get { return exception; }
set { this.exception = value; }

View File

@ -13,6 +13,7 @@ namespace OpenWire.Client {
private Transport transport;
IList sessions = new ArrayList();
private bool transacted;
private bool connected;
private bool closed;
private AcknowledgementMode acknowledgementMode;
private long sessionCounter;
@ -33,8 +34,9 @@ namespace OpenWire.Client {
/// Creates a new session to work on this connection
/// </summary>
public ISession CreateSession(bool transacted, AcknowledgementMode acknowledgementMode) {
CheckClosed();
CheckConnected();
SessionInfo info = CreateSessionInfo(transacted, acknowledgementMode);
SyncRequest(info);
Session session = new Session(this, info);
sessions.Add(session);
return session;
@ -65,13 +67,23 @@ namespace OpenWire.Client {
set { this.acknowledgementMode = value; }
}
public string ClientId {
get { return info.ClientId; }
set {
if (connected) {
throw new OpenWireException("You cannot change the ClientId once the Connection is connected");
}
info.ClientId = value;
}
}
// Implementation methods
/// <summary>
/// Performs a synchronous request-response with the broker
/// </summary>
public Response SyncRequest(Command command) {
CheckClosed();
CheckConnected();
Response response = Transport.Request(command);
if (response is ExceptionResponse) {
ExceptionResponse exceptionResponse = (ExceptionResponse) response;
@ -93,9 +105,13 @@ namespace OpenWire.Client {
return answer;
}
protected void CheckClosed() {
protected void CheckConnected() {
if (closed) {
throw new ConnectionClosedException();
}
if (!connected) {
SyncRequest(info);
connected = true;
}
}
}

View File

@ -0,0 +1,77 @@
using System;
using System.Collections;
using OpenWire.Client.Commands;
using OpenWire.Client.Core;
namespace OpenWire.Client {
/// <summary>
/// Represents a connection with a message broker
/// </summary>
public class ConnectionFactory : IConnectionFactory {
private string host = "localhost";
private int port = 61616;
private string userName;
private string password;
private string clientId;
public IConnection CreateConnection() {
return CreateConnection(userName, password);
}
public IConnection CreateConnection(string userName, string password) {
ConnectionInfo info = CreateConnectionInfo(userName, password);
Transport transport = CreateTransport();
Connection connection = new Connection(transport, info);
connection.ClientId = clientId;
return connection;
}
// Properties
public string Host {
get { return host; }
set { host = value; }
}
public int Port {
get { return port; }
set { port = value; }
}
public string UserName {
get { return userName; }
set { userName = value; }
}
public string Password {
get { return password; }
set { password = value; }
}
public string ClientId {
get { return clientId; }
set { clientId = value; }
}
// Implementation methods
protected ConnectionInfo CreateConnectionInfo(string userName, string password) {
ConnectionInfo answer = new ConnectionInfo();
ConnectionId connectionId = new ConnectionId();
connectionId.Value = CreateNewConnectionID();
answer.ConnectionId = connectionId;
answer.UserName = userName;
answer.Password = password;
return answer;
}
protected string CreateNewConnectionID() {
return Guid.NewGuid().ToString();
}
protected Transport CreateTransport() {
// TODO
return null;
}
}
}

View File

@ -28,6 +28,18 @@ namespace OpenWire.Client.Core {
// empty body to allow generated code to call base method
}
protected virtual BrokerError ReadBrokerError(BinaryReader dataIn) {
BrokerError answer = new BrokerError();
answer.ExceptionClass = dataIn.ReadString();
answer.StackTrace = dataIn.ReadString();
return answer;
}
protected virtual void WriteBrokerError(BrokerError command, BinaryWriter dataOut) {
dataOut.Write(command.ExceptionClass);
dataOut.Write(command.StackTrace);
}
protected virtual ActiveMQDestination ReadDestination(BinaryReader dataIn) {
return (ActiveMQDestination) CommandMarshallerRegistry.ReadCommand(dataIn);
}

View File

@ -0,0 +1,22 @@
using System;
using OpenWire.Client.Core;
namespace OpenWire.Client.Core {
/// <summary>
/// Represents an exception on the broker
/// </summary>
public class BrokerError : AbstractCommand {
private string exceptionClass;
private string stackTrace;
public string ExceptionClass {
get { return exceptionClass; }
set { exceptionClass = value; }
}
public string StackTrace {
get { return stackTrace; }
set { stackTrace = value; }
}
}
}

View File

@ -0,0 +1,21 @@
using System;
using OpenWire.Client.Commands;
namespace OpenWire.Client {
/// <summary>
/// A Factory of IConnection objects
/// </summary>
public interface IConnectionFactory {
/// <summary>
/// Creates a new connection
/// </summary>
IConnection CreateConnection();
/// <summary>
/// Creates a new connection with the given user name and password
/// </summary>
IConnection CreateConnection(string userName, string password);
}
}

View File

@ -30,7 +30,7 @@ namespace OpenWire.Client.IO
base.BuildCommand(command, dataIn);
ConnectionError info = (ConnectionError) command;
info.Exception = ReadBytes(dataIn);
info.Exception = ReadBrokerError(dataIn);
info.ConnectionId = (ConnectionId) CommandMarshallerRegistry.ConnectionIdMarshaller.ReadCommand(dataIn);
}
@ -39,7 +39,7 @@ namespace OpenWire.Client.IO
base.WriteCommand(command, dataOut);
ConnectionError info = (ConnectionError) command;
WriteBytes(info.Exception, dataOut);
WriteBrokerError(info.Exception, dataOut);
CommandMarshallerRegistry.ConnectionIdMarshaller.WriteCommand(info.ConnectionId, dataOut);
}

View File

@ -30,7 +30,7 @@ namespace OpenWire.Client.IO
base.BuildCommand(command, dataIn);
ExceptionResponse info = (ExceptionResponse) command;
info.Exception = ReadBytes(dataIn);
info.Exception = ReadBrokerError(dataIn);
}
@ -38,7 +38,7 @@ namespace OpenWire.Client.IO
base.WriteCommand(command, dataOut);
ExceptionResponse info = (ExceptionResponse) command;
WriteBytes(info.Exception, dataOut);
WriteBrokerError(info.Exception, dataOut);
}
}