Newer
Older
using System;
using log4net;
using log4net.Config;
using Apache.NMS;
using SallyConnect;
namespace ConnectToSally
{
class DotNetSallyClient
#region Class Variables
/*
* Connection Properties
*/
public string user { get; private set; }
public string password { get; private set; }
/// <summary>
/// Connection session variable
/// </summary>
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
{
this.connection = connectFactory.CreateConnection(this.user, this.password);
catch (TypeLoadException e)
{
Console.WriteLine(e.ToString());
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
/// <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
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);
/// <param name="message"></param>
private string logFatal(string message)
string msg = logMessage(message);
logger.Fatal(msg);
return msg;