public class TopicPublisher : PUBSUB
{
public string VPNName { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public const int DefaultReconnectRetries = 3;
public void Run(IContext context, string host)
{
// Validate parameters
if (context == null)
{
throw new ArgumentException(“Solace Systems API context Router must be not null.”, “context”);
}
if (string.IsNullOrWhiteSpace(host))
{
throw new ArgumentException(“Solace Messaging Router host name must be non-empty.”, “host”);
}
if (string.IsNullOrWhiteSpace(VPNName))
{
throw new InvalidOperationException(“VPN name must be non-empty.”);
}
if (string.IsNullOrWhiteSpace(UserName))
{
throw new InvalidOperationException(“Client username must be non-empty.”);
}
// Create session properties
SessionProperties sessionProps = new SessionProperties()
{
Host = host,
VPNName = VPNName,
UserName = UserName,
Password = Password,
ReconnectRetries = DefaultReconnectRetries
};
Console.WriteLine(“Connecting as {0}@{1} on {2}…”, UserName, VPNName, host);
using (ISession session = context.CreateSession(sessionProps, null, null))
{
ReturnCode returnCode = session.Connect();
if (returnCode == ReturnCode.SOLCLIENT_OK)
{
Console.WriteLine(“Session successfully connected.”);
PublishMessage(session);
}
else
{
Console.WriteLine(“Error connecting, return code: {0}”, returnCode);
}
}
}
public void PublishMessage(ISession session)
{
using (IMessage message = ContextFactory.Instance.CreateMessage())
{
message.Destination = ContextFactory.Instance.CreateTopic(“tutorial/topic”);
message.BinaryAttachment = Encoding.ASCII.GetBytes(RedStatus);
Console.WriteLine(“Publishing message…”);
ReturnCode returnCode = session.Send(message);
if (returnCode == ReturnCode.SOLCLIENT_OK)
{
Console.WriteLine(“Done.”);
}
else
{
Console.WriteLine(“Publishing failed, return code: {0}”, returnCode);
}
}
}
}
public class TopicSubscriber : PUBSUB, IDisposable
{
public string VPNName { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public const int DefaultReconnectRetries = 3;
private ISession Session = null;
private EventWaitHandle WaitEventWaitHandle = new AutoResetEvent(false);
public void RunSUB(IContext context1, string host)
{
// Validate parameters
if (context1 == null)
{
throw new ArgumentException(“Solace Systems API context Router must be not null.”, “context1”);
}
if (string.IsNullOrWhiteSpace(host))
{
throw new ArgumentException(“Solace Messaging Router host name must be non-empty.”, “host”);
}
if (string.IsNullOrWhiteSpace(VPNName))
{
throw new InvalidOperationException(“VPN name must be non-empty.”);
}
if (string.IsNullOrWhiteSpace(UserName))
{
throw new InvalidOperationException(“Client username must be non-empty.”);
}
SessionProperties sessionProps = new SessionProperties()
{
Host = host,
VPNName = VPNName,
UserName = UserName,
Password = Password,
ReconnectRetries = DefaultReconnectRetries
};
Console.WriteLine(“Connecting as {0}@{1} on {2}…”, UserName, VPNName, host);
Session = context1.CreateSession(sessionProps, HandleMessage, null);
ReturnCode returnCode = Session.Connect();
if (returnCode == ReturnCode.SOLCLIENT_OK)
{
Console.WriteLine(“Session successfully connected.”);
Session.Subscribe(ContextFactory.Instance.CreateTopic(“tutorial/topic”), true);
Console.WriteLine(“Waiting for a message to be published…”);
WaitEventWaitHandle.WaitOne();
}
else
{
Console.WriteLine(“Error connecting, return code: {0}”, returnCode);
}
}
public void HandleMessage(object source, MessageEventArgs args)
{
Console.WriteLine(“Received published message.”);
using (IMessage message = args.Message)
{
Console.WriteLine(“Message content: {0}”, Encoding.ASCII.GetString(message.BinaryAttachment));
String mess = Encoding.ASCII.GetString(message.BinaryAttachment);
Debug.Log(mess);
WaitEventWaitHandle.Set();
}
}
#region IDisposable Support
private bool disposedValue = false;
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
if (Session != null)
{
Session.Dispose();
}
}
disposedValue = true;
}
}
public void Dispose()
{
Dispose(true);
}
#endregion
}
}