From e9bbdbf7d622fd97a91b06b04ae2e8f33ce94ba4 Mon Sep 17 00:00:00 2001 From: Rupert Smith Date: Tue, 19 Jun 2007 15:34:07 +0000 Subject: Merged revisions 544508 via svnmerge from https://svn.apache.org/repos/asf/incubator/qpid/branches/M2 ........ r544508 | rupertlssmith | 2007-06-05 16:02:09 +0100 (Tue, 05 Jun 2007) | 1 line Interop tests ported to .Net ........ git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@548768 13f79535-47bb-0310-9956-ffa450edef68 --- dotnet/Qpid.Client.Tests/Qpid.Client.Tests.csproj | 5 + .../interop/InteropClientTestCase.cs | 68 ++++ .../interop/TestCases/TestCase1DummyRun.cs | 70 +++++ .../interop/TestCases/TestCase2BasicP2P.cs | 186 +++++++++++ .../interop/TestCases/TestCase3BasicPubSub.cs | 215 +++++++++++++ dotnet/Qpid.Client.Tests/interop/TestClient.cs | 341 +++++++++++++++++++++ 6 files changed, 885 insertions(+) create mode 100644 dotnet/Qpid.Client.Tests/interop/InteropClientTestCase.cs create mode 100644 dotnet/Qpid.Client.Tests/interop/TestCases/TestCase1DummyRun.cs create mode 100644 dotnet/Qpid.Client.Tests/interop/TestCases/TestCase2BasicP2P.cs create mode 100644 dotnet/Qpid.Client.Tests/interop/TestCases/TestCase3BasicPubSub.cs create mode 100644 dotnet/Qpid.Client.Tests/interop/TestClient.cs diff --git a/dotnet/Qpid.Client.Tests/Qpid.Client.Tests.csproj b/dotnet/Qpid.Client.Tests/Qpid.Client.Tests.csproj index cbe632ace9..a2c71a5432 100644 --- a/dotnet/Qpid.Client.Tests/Qpid.Client.Tests.csproj +++ b/dotnet/Qpid.Client.Tests/Qpid.Client.Tests.csproj @@ -47,6 +47,11 @@ + + + + + diff --git a/dotnet/Qpid.Client.Tests/interop/InteropClientTestCase.cs b/dotnet/Qpid.Client.Tests/interop/InteropClientTestCase.cs new file mode 100644 index 0000000000..8e93bd8fa6 --- /dev/null +++ b/dotnet/Qpid.Client.Tests/interop/InteropClientTestCase.cs @@ -0,0 +1,68 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Qpid.Messaging; + +namespace Qpid.Client.Tests.interop +{ + /// Defines the possible test case roles that an interop test case can take on. + public enum Roles { SENDER, RECEIVER }; + + /// + /// InteropClientTestCase provides an interface that classes implementing test cases from the interop testing spec + /// (http://cwiki.apache.org/confluence/display/qpid/Interop+Testing+Specification) should implement. + /// + ///

+ ///
CRC Card
Responsibilities + ///
Supply the name of the test case that this implements. + ///
Accept/Reject invites based on test parameters. + ///
Adapt to assigned roles. + ///
Perform test case actions. + ///
Generate test reports. + ///
+ ///

+ interface InteropClientTestCase + { + /// + /// Should provide the name of the test case that this class implements. The exact names are defined in the + /// interop testing spec. + /// + /// + /// The name of the test case that this implements. + string GetName(); + + /// + /// Determines whether the test invite that matched this test case is acceptable. + /// + /// + /// The invitation to accept or reject. + /// + /// true to accept the invitation, false to reject it. + /// + /// @throws JMSException Any JMSException resulting from reading the message are allowed to fall through. + bool AcceptInvite(IMessage inviteMessage); + + /// + /// Assigns the role to be played by this test case. The test parameters are fully specified in the + /// assignment message. When this method return the test case will be ready to execute. + /// + /// + /// The role to be played; sender or receiver. + /// The role assingment message, contains the full test parameters. + void AssignRole(Roles role, IMessage assignRoleMessage); + + /// + /// Performs the test case actions. + /// + void Start(); + + /// + /// Gets a report on the actions performed by the test case in its assigned role. + /// + /// + /// The session to create the report message in. + /// + /// The report message. + IMessage GetReport(IChannel channel); + } +} diff --git a/dotnet/Qpid.Client.Tests/interop/TestCases/TestCase1DummyRun.cs b/dotnet/Qpid.Client.Tests/interop/TestCases/TestCase1DummyRun.cs new file mode 100644 index 0000000000..2001d14956 --- /dev/null +++ b/dotnet/Qpid.Client.Tests/interop/TestCases/TestCase1DummyRun.cs @@ -0,0 +1,70 @@ +using System; +using System.Collections.Generic; +using System.Text; +using log4net; +using Qpid.Messaging; + +namespace Qpid.Client.Tests.interop.TestCases +{ + /// + /// Implements tet case 1, dummy run. This test case sends no test messages, it exists to confirm that the test harness + /// is interacting with the coordinator correctly. + /// + ///

+ ///
CRC Card
Responsibilities Collaborations + ///
Supply the name of the test case that this implements. + ///
Accept/Reject invites based on test parameters. + ///
Adapt to assigned roles. + ///
Perform test case actions. + ///
Generate test reports. + ///
+ ///

+ public class TestCase1DummyRun : InteropClientTestCase + { + private static ILog log = LogManager.GetLogger(typeof(TestCase1DummyRun)); + + public String GetName() + { + log.Debug("public String getName(): called"); + + return "TC1_DummyRun"; + } + + public bool AcceptInvite(IMessage inviteMessage) + { + log.Debug("public boolean acceptInvite(Message inviteMessage): called"); + + // Test parameters don't matter, accept all invites. + return true; + } + + public void AssignRole(Roles role, IMessage assignRoleMessage) + { + log.Debug("public void assignRole(Roles role, Message assignRoleMessage): called"); + + // Do nothing, both roles are the same. + } + + public void Start() + { + log.Debug("public void start(): called"); + + // Do nothing. + } + + public IMessage GetReport(IChannel channel) + { + log.Debug("public Message getReport(Session session): called"); + + // Generate a dummy report, the coordinator expects a report but doesn't care what it is. + return channel.CreateTextMessage("Dummy Run, Ok."); + } + + public void OnMessage(IMessage message) + { + log.Debug("public void onMessage(Message message = " + message + "): called"); + + // Ignore any messages. + } + } +} diff --git a/dotnet/Qpid.Client.Tests/interop/TestCases/TestCase2BasicP2P.cs b/dotnet/Qpid.Client.Tests/interop/TestCases/TestCase2BasicP2P.cs new file mode 100644 index 0000000000..46c644c243 --- /dev/null +++ b/dotnet/Qpid.Client.Tests/interop/TestCases/TestCase2BasicP2P.cs @@ -0,0 +1,186 @@ +using System; +using System.Collections.Generic; +using System.Text; +using log4net; +using Qpid.Messaging; + +namespace Qpid.Client.Tests.interop.TestCases +{ + /// + /// Implements test case 2, basic P2P. Sends/receives a specified number of messages to a specified route on the + /// default direct exchange. Produces reports on the actual number of messages sent/received. + /// + ///

+ ///
CRC Card
Responsibilities Collaborations + ///
Supply the name of the test case that this implements. + ///
Accept/Reject invites based on test parameters. + ///
Adapt to assigned roles. + ///
Send required number of test messages. + ///
Generate test reports. + ///
+ ///

+ public class TestCase2BasicP2P : InteropClientTestCase + { + /// Used for debugging. + private static ILog log = LogManager.GetLogger(typeof(TestCase2BasicP2P)); + + /// Holds the count of test messages received. + private int messageCount; + + /// The role to be played by the test. + private Roles role; + + /// The number of test messages to send. + private int numMessages; + + /// The routing key to send them to on the default direct exchange. + private string sendDestination; + + /// The connection to send the test messages on. + private IConnection connection; + + /// The session to send the test messages on. + private IChannel channel; + + /// The producer to send the test messages with. + private IMessagePublisher publisher; + + /// + /// Should provide the name of the test case that this class implements. The exact names are defined in the + /// interop testing spec. + /// + /// + /// The name of the test case that this implements. + public String GetName() + { + log.Debug("public String GetName(): called"); + + return "TC2_BasicP2P"; + } + + /// + /// Determines whether the test invite that matched this test case is acceptable. + /// + /// + /// The invitation to accept or reject. + /// + /// true to accept the invitation, false to reject it. + public bool AcceptInvite(IMessage inviteMessage) + { + log.Debug("public boolean AcceptInvite(Message inviteMessage = " + inviteMessage + "): called"); + + // All invites are acceptable. + return true; + } + + /// + /// Assigns the role to be played by this test case. The test parameters are fully specified in the + /// assignment message. When this method return the test case will be ready to execute. + /// + /// + /// The role to be played; sender or receiver. + /// The role assingment message, contains the full test parameters. + public void AssignRole(Roles role, IMessage assignRoleMessage) + { + log.Debug("public void AssignRole(Roles role = " + role + ", Message assignRoleMessage = " + assignRoleMessage + + "): called"); + + // Reset the message count for a new test. + messageCount = 0; + + // Take note of the role to be played. + this.role = role; + + // Create a new connection to pass the test messages on. + connection = + TestClient.CreateConnection(TestClient.brokerUrl, TestClient.virtualHost); + channel = connection.CreateChannel(false, AcknowledgeMode.AutoAcknowledge); + + // Extract and retain the test parameters. + numMessages = assignRoleMessage.Headers.GetInt("P2P_NUM_MESSAGES"); + string queueAndKeyName = assignRoleMessage.Headers.GetString("P2P_QUEUE_AND_KEY_NAME"); + channel.DeclareQueue(queueAndKeyName, false, true, true); + channel.Bind(queueAndKeyName, ExchangeNameDefaults.DIRECT, queueAndKeyName); + sendDestination = queueAndKeyName; + + log.Debug("numMessages = " + numMessages); + log.Debug("sendDestination = " + sendDestination); + log.Debug("role = " + role); + + switch (role) + { + // Check if the sender role is being assigned, and set up a message producer if so. + case Roles.SENDER: + publisher = channel.CreatePublisherBuilder() + .WithExchangeName(ExchangeNameDefaults.DIRECT) + .WithRoutingKey(sendDestination) + .Create(); + break; + + // Otherwise the receiver role is being assigned, so set this up to listen for messages. + case Roles.RECEIVER: + IMessageConsumer consumer = channel.CreateConsumerBuilder(sendDestination).Create(); + consumer.OnMessage += new MessageReceivedDelegate(OnMessage); + + break; + } + + connection.Start(); + } + + /// Performs the test case actions. + public void Start() + { + log.Debug("public void start(): called"); + + // Check that the sender role is being performed. + if (role == Roles.SENDER) + { + IMessage testMessage = channel.CreateTextMessage("test"); + + for (int i = 0; i < numMessages; i++) + { + publisher.Send(testMessage); + + // Increment the message count. + messageCount++; + } + } + } + + /// + /// Gets a report on the actions performed by the test case in its assigned role. + /// + /// + /// The session to create the report message in. + /// + /// The report message. + public IMessage GetReport(IChannel channel) + { + log.Debug("public Message GetReport(IChannel channel): called"); + + // Close the test connection. + connection.Stop(); + + // Generate a report message containing the count of the number of messages passed. + IMessage report = channel.CreateMessage(); + report.Headers.SetString("CONTROL_TYPE", "REPORT"); + report.Headers.SetInt("MESSAGE_COUNT", messageCount); + + return report; + } + + /// + /// Counts incoming test messages. + /// + /// + /// The incoming test message. + public void OnMessage(IMessage message) + { + log.Debug("public void OnMessage(IMessage message = " + message + "): called"); + + // Increment the message count. + messageCount++; + } + } +} diff --git a/dotnet/Qpid.Client.Tests/interop/TestCases/TestCase3BasicPubSub.cs b/dotnet/Qpid.Client.Tests/interop/TestCases/TestCase3BasicPubSub.cs new file mode 100644 index 0000000000..61c02f13d8 --- /dev/null +++ b/dotnet/Qpid.Client.Tests/interop/TestCases/TestCase3BasicPubSub.cs @@ -0,0 +1,215 @@ +using System; +using System.Collections.Generic; +using System.Text; +using log4net; +using Qpid.Messaging; + +namespace Qpid.Client.Tests.interop.TestCases +{ + /// + /// Implements test case 3, basic pub/sub. Sends/received a specified number of messages to a specified route on the + /// default topic exchange, using the specified number of receiver connections. Produces reports on the actual number of + /// messages sent/received. + /// + ///

+ ///
CRC Card
Responsibilities Collaborations + ///
Supply the name of the test case that this implements. + ///
Accept/Reject invites based on test parameters. + ///
Adapt to assigned roles. + ///
Send required number of test messages using pub/sub. + ///
Generate test reports. + ///
+ ///

+ public class TestCase3BasicPubSub : InteropClientTestCase + { + /// Used for debugging. + private static ILog log = LogManager.GetLogger(typeof(TestCase3BasicPubSub)); + + /// Holds the count of test messages received. + private int messageCount; + + /// The role to be played by the test. + private Roles role; + + /// The number of test messages to send. + private int numMessages; + + /// The number of receiver connection to use. + private int numReceivers; + + /// The routing key to send them to on the default direct exchange. + private string sendDestination; + + /// The connections to send/receive the test messages on. + private IConnection[] connection; + + /// The sessions to send/receive the test messages on. + private IChannel[] channel; + + /// The producer to send the test messages with. + IMessagePublisher publisher; + + /// + /// Should provide the name of the test case that this class implements. The exact names are defined in the + /// interop testing spec. + /// + /// + /// The name of the test case that this implements. + public String GetName() + { + log.Debug("public String GetName(): called"); + + return "TC3_BasicPubSub"; + } + + /// + /// Determines whether the test invite that matched this test case is acceptable. + /// + /// + /// The invitation to accept or reject. + /// + /// true to accept the invitation, false to reject it. + public bool AcceptInvite(IMessage inviteMessage) + { + log.Debug("public boolean AcceptInvite(IMessage inviteMessage = " + inviteMessage + "): called"); + + // All invites are acceptable. + return true; + } + + /// + /// Assigns the role to be played by this test case. The test parameters are fully specified in the + /// assignment message. When this method return the test case will be ready to execute. + /// + /// + /// The role to be played; sender or receiver. + /// The role assingment message, contains the full test parameters. + public void AssignRole(Roles role, IMessage assignRoleMessage) + { + log.Debug("public void assignRole(Roles role = " + role + ", IMessage assignRoleMessage = " + assignRoleMessage + + "): called"); + + // Reset the message count for a new test. + messageCount = 0; + + // Take note of the role to be played. + this.role = role; + + // Extract and retain the test parameters. + numMessages = assignRoleMessage.Headers.GetInt("PUBSUB_NUM_MESSAGES"); + numReceivers = assignRoleMessage.Headers.GetInt("PUBSUB_NUM_RECEIVERS"); + string sendKey = assignRoleMessage.Headers.GetString("PUBSUB_KEY"); + sendDestination = sendKey; + + log.Debug("numMessages = " + numMessages); + log.Debug("numReceivers = " + numReceivers); + log.Debug("sendKey = " + sendKey); + log.Debug("role = " + role); + + switch (role) + { + // Check if the sender role is being assigned, and set up a single message producer if so. + case Roles.SENDER: + // Create a new connection to pass the test messages on. + connection = new IConnection[1]; + channel = new IChannel[1]; + + connection[0] = + TestClient.CreateConnection(TestClient.brokerUrl, TestClient.virtualHost); + channel[0] = connection[0].CreateChannel(false, AcknowledgeMode.AutoAcknowledge); + + // Extract and retain the test parameters. + publisher = channel[0].CreatePublisherBuilder() + .WithExchangeName(ExchangeNameDefaults.TOPIC) + .WithRoutingKey(sendDestination) + .Create(); + break; + + // Otherwise the receiver role is being assigned, so set this up to listen for messages on the required number + // of receiver connections. + case Roles.RECEIVER: + // Create the required number of receiver connections. + connection = new IConnection[numReceivers]; + channel = new IChannel[numReceivers]; + + for (int i = 0; i < numReceivers; i++) + { + connection[i] = + TestClient.CreateConnection(TestClient.brokerUrl, TestClient.virtualHost); + channel[i] = connection[i].CreateChannel(false, AcknowledgeMode.AutoAcknowledge); + + IMessageConsumer consumer = channel[i].CreateConsumerBuilder(sendDestination).Create(); + consumer.OnMessage += new MessageReceivedDelegate(OnMessage); + } + + break; + } + + // Start all the connection dispatcher threads running. + foreach (IConnection con in connection) + { + con.Start(); + } + } + + /// + /// Performs the test case actions. + /// + public void Start() + { + log.Debug("public void Start(): called"); + + // Check that the sender role is being performed. + if (role == Roles.SENDER) + { + IMessage testMessage = channel[0].CreateTextMessage("test"); + + for (int i = 0; i < numMessages; i++) + { + publisher.Send(testMessage); + + // Increment the message count. + messageCount++; + } + } + } + + /// + /// Gets a report on the actions performed by the test case in its assigned role. + /// + /// + /// The session to create the report message in. + /// + /// The report message. + public IMessage GetReport(IChannel channel) + { + log.Debug("public IMessage getReport(IChannel channel): called"); + + // Close the test connections. + foreach (IConnection con in connection) + { + con.Stop(); + } + + // Generate a report message containing the count of the number of messages passed. + IMessage report = channel.CreateMessage(); + report.Headers.SetString("CONTROL_TYPE", "REPORT"); + report.Headers.SetInt("MESSAGE_COUNT", messageCount); + + return report; + } + + /// + /// Counts incoming test messages. + /// + /// + /// The incoming test message. + public void OnMessage(IMessage message) + { + log.Debug("public void onMessage(IMessage message = " + message + "): called"); + + // Increment the message count. + messageCount++; + } + } +} diff --git a/dotnet/Qpid.Client.Tests/interop/TestClient.cs b/dotnet/Qpid.Client.Tests/interop/TestClient.cs new file mode 100644 index 0000000000..b502129325 --- /dev/null +++ b/dotnet/Qpid.Client.Tests/interop/TestClient.cs @@ -0,0 +1,341 @@ +using System; +using System.Collections.Generic; +using System.Collections; +using System.Text; +using Qpid.Messaging; +using Qpid.Client.Qms; +using log4net; +using Qpid.Client.Tests.interop.TestCases; + +namespace Qpid.Client.Tests.interop +{ + /// + /// Implements a test client as described in the interop testing spec + /// (http://cwiki.apache.org/confluence/display/qpid/Interop+Testing+Specification). A test client is an agent that + /// reacts to control message sequences send by the test coordinator. + /// + ///

+ ///
Messages Handled by TestClient
Message Action + ///
Invite(compulsory) Reply with Enlist. + ///
Invite(test case) Reply with Enlist if test case available. + ///
AssignRole(test case) Reply with Accept Role if matches an enlisted test. Keep test parameters. + ///
Start Send test messages defined by test parameters. Send report on messages sent. + ///
Status Request Send report on messages received. + ///
+ /// + ///

+ ///
CRC Card
Responsibilities Collaborations + ///
Handle all incoming control messages. {@link InteropClientTestCase} + ///
Configure and look up test cases by name. {@link InteropClientTestCase} + ///
+ ///

+ class TestClient + { + private static ILog log = LogManager.GetLogger(typeof(TestClient)); + + /// Defines the default broker for the tests, localhost, default port. + public static string DEFAULT_BROKER_URL = "amqp://guest:guest@clientid/?brokerlist='tcp://localhost:5672'"; + + /// Defines the default virtual host to use for the tests, none. + public static string DEFAULT_VIRTUAL_HOST = ""; + + /// Defines the default identifying name of this test client. + public static string DEFAULT_CLIENT_NAME = ".net"; + + /// Holds the URL of the broker to run the tests on. + public static string brokerUrl; + + /// Holds the virtual host to run the tests on. If null, then the default virtual host is used. + public static string virtualHost; + + /// The clients identifying name to print in test results and to distinguish from other clients. + private string clientName; + + /// Holds all the test cases. + private IDictionary testCases = new Hashtable(); + + InteropClientTestCase currentTestCase; + + private MessagePublisherBuilder publisherBuilder; + + private IChannel channel; + + + /// + /// Creates a new interop test client, listenting to the specified broker and virtual host, with the specified + /// client identifying name. + /// + /// + /// The url of the broker to connect to. + /// The virtual host to conect to. + /// The client name to use. + public TestClient(string brokerUrl, string virtualHost, string clientName) + { + log.Debug("public TestClient(string brokerUrl = " + brokerUrl + ", string virtualHost = " + virtualHost + + ", string clientName = " + clientName + "): called"); + + // Retain the connection parameters. + TestClient.brokerUrl = brokerUrl; + TestClient.virtualHost = virtualHost; + this.clientName = clientName; + } + + + /// + /// The entry point for the interop test coordinator. This client accepts the following command line arguments: + /// + /// + ///

+ ///
-b The broker URL. Optional. + ///
-h The virtual host. Optional. + ///
-n The test client name. Optional. + ///
name=value Trailing argument define name/value pairs. Added to system properties. Optional. + ///
+ /// + /// The command line arguments. + public static void Main(string[] args) + { + // Extract the command line options (Not exactly Posix but it will do for now...). + string brokerUrl = DEFAULT_BROKER_URL; + string virtualHost = DEFAULT_VIRTUAL_HOST; + string clientName = DEFAULT_CLIENT_NAME; + + foreach (string nextArg in args) + { + if (nextArg.StartsWith("-b")) + { + brokerUrl = nextArg.Substring(2); + } + else if (nextArg.StartsWith("-h")) + { + virtualHost = nextArg.Substring(2); + } + else if (nextArg.StartsWith("-n")) + { + clientName = nextArg.Substring(2); + } + } + + // Create a test client and start it running. + TestClient client = new TestClient(brokerUrl, virtualHost, clientName); + + try + { + client.Start(); + } + catch (Exception e) + { + log.Error("The test client was unable to start.", e); + System.Environment.Exit(1); + } + } + + ///

+ /// Starts the interop test client running. This causes it to start listening for incoming test invites. + /// + private void Start() + { + log.Debug("private void Start(): called"); + + // Use a class path scanner to find all the interop test case implementations. + ArrayList testCaseClasses = new ArrayList(); + + // ClasspathScanner.getMatches(InteropClientTestCase.class, "^TestCase.*", true); + // Hard code the test classes till the classpath scanner is fixed. + testCaseClasses.Add(typeof(TestCase1DummyRun)); + testCaseClasses.Add(typeof(TestCase2BasicP2P)); + testCaseClasses.Add(typeof(TestCase3BasicPubSub)); + + // Create all the test case implementations and index them by the test names. + foreach (Type testClass in testCaseClasses) + { + InteropClientTestCase testCase = (InteropClientTestCase)Activator.CreateInstance(testClass); + testCases.Add(testCase.GetName(), testCase); + } + + // Open a connection to communicate with the coordinator on. + IConnection connection = CreateConnection(brokerUrl, virtualHost); + + channel = connection.CreateChannel(false, AcknowledgeMode.AutoAcknowledge); + + // Set this up to listen for control messages. + string responseQueueName = channel.GenerateUniqueName(); + channel.DeclareQueue(responseQueueName, false, true, true); + + channel.Bind(responseQueueName, ExchangeNameDefaults.DIRECT, "iop.control." + clientName); + channel.Bind(responseQueueName, ExchangeNameDefaults.DIRECT, "iop.control"); + + IMessageConsumer consumer = channel.CreateConsumerBuilder(responseQueueName) + .Create(); + consumer.OnMessage += new MessageReceivedDelegate(OnMessage); + + // Create a publisher to send replies with. + publisherBuilder = channel.CreatePublisherBuilder() + .WithExchangeName(ExchangeNameDefaults.DIRECT); + + + // Start listening for incoming control messages. + connection.Start(); + Console.WriteLine("Test client " + clientName + " ready to receive test control messages..."); + } + + /// + /// Establishes an AMQ connection. This is a simple convenience method for code that does not anticipate handling connection failures. + /// All exceptions that indicate that the connection has failed, are allowed to fall through. + /// + /// + /// The broker url to connect to, null to use the default from the properties. + /// The virtual host to connectio to, null to use the default. + /// + /// A JMS conneciton. + public static IConnection CreateConnection(string brokerUrl, string virtualHost) + { + log.Debug("public static Connection createConnection(string brokerUrl = " + brokerUrl + ", string virtualHost = " + + virtualHost + "): called"); + + // Create a connection to the broker. + IConnectionInfo connectionInfo = QpidConnectionInfo.FromUrl(brokerUrl); + connectionInfo.VirtualHost = virtualHost; + IConnection connection = new AMQConnection(connectionInfo); + + return connection; + } + + /// + /// Handles all incoming control messages. + /// + /// + /// The incoming message. + public void OnMessage(IMessage message) + { + log.Debug("public void OnMessage(IMessage message = " + message + "): called"); + + try + { + string controlType = message.Headers.GetString("CONTROL_TYPE"); + string testName = message.Headers.GetString("TEST_NAME"); + + // Check if the message is a test invite. + if ("INVITE" == controlType) + { + string testCaseName = message.Headers.GetString("TEST_NAME"); + + // Flag used to indicate that an enlist should be sent. Only enlist to compulsory invites or invites + // for which test cases exist. + bool enlist = false; + + if (testCaseName != null) + { + log.Debug("Got an invite to test: " + testCaseName); + + // Check if the requested test case is available. + InteropClientTestCase testCase = (InteropClientTestCase)testCases[testCaseName]; + + if (testCase != null) + { + // Make the requested test case the current test case. + currentTestCase = testCase; + enlist = true; + } + } + else + { + log.Debug("Got a compulsory invite."); + + enlist = true; + } + + if (enlist) + { + // Reply with the client name in an Enlist message. + IMessage enlistMessage = channel.CreateMessage(); + enlistMessage.Headers.SetString("CONTROL_TYPE", "ENLIST"); + enlistMessage.Headers.SetString("CLIENT_NAME", clientName); + enlistMessage.Headers.SetString("CLIENT_PRIVATE_CONTROL_KEY", "iop.control." + clientName); + enlistMessage.CorrelationId = message.CorrelationId; + + Send(enlistMessage, message.ReplyToRoutingKey); + } + } + else if ("ASSIGN_ROLE" == controlType) + { + // Assign the role to the current test case. + string roleName = message.Headers.GetString("ROLE"); + + log.Debug("Got a role assignment to role: " + roleName); + + Roles role; + + if (roleName == "SENDER") + { + role = Roles.SENDER; + } + else + { + role = Roles.RECEIVER; + } + + currentTestCase.AssignRole(role, message); + + // Reply by accepting the role in an Accept Role message. + IMessage acceptRoleMessage = channel.CreateMessage(); + acceptRoleMessage.Headers.SetString("CONTROL_TYPE", "ACCEPT_ROLE"); + acceptRoleMessage.CorrelationId = message.CorrelationId; + + Send(acceptRoleMessage, message.ReplyToRoutingKey); + } + else if ("START" == controlType || "STATUS_REQUEST" == controlType) + { + if ("START" == controlType) + { + log.Debug("Got a start notification."); + + // Start the current test case. + currentTestCase.Start(); + } + else + { + log.Debug("Got a status request."); + } + + // Generate the report from the test case and reply with it as a Report message. + IMessage reportMessage = currentTestCase.GetReport(channel); + reportMessage.Headers.SetString("CONTROL_TYPE", "REPORT"); + reportMessage.CorrelationId = message.CorrelationId; + + Send(reportMessage, message.ReplyToRoutingKey); + } + else if ("TERMINATE" == controlType) + { + Console.WriteLine("Received termination instruction from coordinator."); + + // Is a cleaner shutdown needed? + System.Environment.Exit(1); + } + else + { + // Log a warning about this but otherwise ignore it. + log.Warn("Got an unknown control message, controlType = " + controlType + ", message = " + message); + } + } + catch (QpidException e) + { + // Log a warning about this, but otherwise ignore it. + log.Warn("A QpidException occurred whilst handling a message."); + log.Debug("Got QpidException whilst handling message: " + message, e); + } + } + + /// + /// Send the specified message using the specified routing key on the direct exchange. + /// + /// + /// The message to send. + /// The routing key to send the message with. + public void Send(IMessage message, string routingKey) + { + IMessagePublisher publisher = publisherBuilder.WithRoutingKey(routingKey).Create(); + publisher.Send(message); + } + } +} -- cgit v1.2.1