Select Git revision
DotNetSallyClient.cs
-
Tom Wiesing authoredTom Wiesing authored
DotNetSallyClient.cs 4.97 KiB
using System;
using log4net;
using log4net.Config;
using Apache.NMS;
using SallyConnect;
namespace ConnectToSally
{
/// <summary>
/// Represent a sally client
/// </summary>
class DotNetSallyClient
{
#region Class Variables
/*
* Connection Properties
*/
/// <summary>
/// Address this DotNetSallyClient connects to.
/// </summary>
public string address { get; private set; }
/// <summary>
/// Username used to connect to Sally.
/// </summary>
public string user { get; private set; }
/// <summary>
/// Password used to connect to Sally.
/// </summary>
public string password { get; private set; }
/*
* Connection State
*/
/// <summary>
/// Connection session variable
/// </summary>
private ISession session { get; set; }
/// <summary>
/// Connection variable
/// </summary>
private IConnection connection { get; set; }
/*
* Logger
*/
/// <summary>
/// A logger so that we can write things to the console.
/// </summary>
private static readonly ILog logger = LogManager.GetLogger(typeof(DotNetSallyClient));
#endregion
#region Connection Start
/// <summary>
/// Constructs a new DotNetSallyClient and establishes a connection.
/// </summary>
/// <param name="address">Adress to connect to. </param>
/// <param name="user">Username to connect with. </param>
/// <param name="password">Password to connect with. </param>
public DotNetSallyClient(string address= "activemq:tcp://localhost:61616", string user="karaf", string password="karaf")
{
// store connection params
this.address = address;
this.user = user;
this.password = password;
BasicConfigurator.Configure(); // ???
// try to connect
if (!connect())
{
logFatal("Could not start connection");
return;
}
logInfo("Started connection");
}
/// <summary>
/// Starts the connection to Sally.
/// </summary>
/// <returns>If the connection was a success</returns>
protected bool connect()
{
// set up a URI and get ready for connecting
Uri connecturi = new Uri(this.address);
IConnectionFactory connectFactory = new NMSConnectionFactory(connecturi);
try
{
//create a connection
this.connection = connectFactory.CreateConnection(this.user, this.password);
//create a session
this.session = this.connection.CreateSession();
//and get everything running
this.connection.Start();
// we have started the connection.
return this.connection.IsStarted;
}
// catch exceptions so that we do not crash
catch (TypeLoadException e)
{
Console.WriteLine(e.ToString());
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
//nope something went wrong.
return false;
}
#endregion
#region Connection Getters
/// <summary>
/// returns the session of this SallyClient.
/// </summary>
/// <returns></returns>
public ISession getSallySession(){
// if there is no connection, we can not return a session
if (this.connection == null || !this.connection.IsStarted) {
throw new Exception(logFatal("No connection started yet. Start a connection and try again."));
}
// we can now return it.
return this.session;
}
#endregion
#region Logging
/// <summary>
/// Generates a log message
/// </summary>
/// <param name="message">Message content</param>
/// <returns></returns>
private string logMessage(string message)
{
return "DotNetSallyClient <" + address + ">: " + message;
}
/// <summary>
/// Logs an info message to console and to the log.
/// </summary>
/// <param name="message"></param>
private string logInfo(string message)
{
string msg = logMessage(message);
logger.Info(msg);
return msg;
}
/// <summary>
/// Logs a fatal message to console and to the log.
/// </summary>
/// <param name="message"></param>
private string logFatal(string message)
{
string msg = logMessage(message);
logger.Fatal(msg);
return msg;
}
#endregion
}
}