Skip to content
Snippets Groups Projects
Commit fa57cc3b authored by Tom Wiesing's avatar Tom Wiesing
Browse files

Clean rewrite

parent 8f0b7f60
No related branches found
No related tags found
No related merge requests found
...@@ -15,42 +15,86 @@ namespace ConnectToSally ...@@ -15,42 +15,86 @@ namespace ConnectToSally
/// </summary> /// </summary>
class DotNetSallyClient class DotNetSallyClient
{ {
protected static TimeSpan receiveTimeout = TimeSpan.FromSeconds(10);
#region Class Variables
/*
* Connection Properties
*/
/// <summary> /// <summary>
/// A logger so that we can write things to the console. /// Address this DotNetSallyClient connects to.
/// </summary> /// </summary>
private static readonly ILog logger = LogManager.GetLogger(typeof(DotNetSallyClient)); public string address { get; private set; }
/// <summary> /// <summary>
/// Username for the connection. /// Username used to connect to Sally.
/// </summary> /// </summary>
public string user { get; private set; } public string user { get; private set; }
/// <summary> /// <summary>
/// Password for the connection. /// Password used to connect to Sally.
/// </summary> /// </summary>
public string password { get; private set; } public string password { get; private set; }
/// <summary> /*
/// Address for the connection. * Connection State
/// </summary> */
public string address { get; private set; }
/// <summary> /// <summary>
/// Connection session variable /// Connection session variable
/// </summary> /// </summary>
public ISession session { get; private set; } private ISession session { get; set; }
/// <summary> /// <summary>
/// Connection variable /// Connection variable
/// </summary> /// </summary>
public IConnection connection { get; private set; } 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> /// <summary>
/// Starts the connection to Sally. /// Starts the connection to Sally.
/// </summary> /// </summary>
protected bool startConnection() /// <returns>If the connection was a success</returns>
protected bool connect()
{ {
// set up a URI and get ready for connecting // set up a URI and get ready for connecting
Uri connecturi = new Uri(this.address); Uri connecturi = new Uri(this.address);
...@@ -84,60 +128,64 @@ namespace ConnectToSally ...@@ -84,60 +128,64 @@ namespace ConnectToSally
//nope something went wrong. //nope something went wrong.
return false; return false;
} }
#endregion
#region Connection Getters
/// <summary> /// <summary>
/// Starts a DotNetSallyClient object with a session and connection with Sally. /// returns the session of this SallyClient.
/// </summary> /// </summary>
/// <param name="address">Adress to connect to. </param> /// <returns></returns>
/// <param name="user">Username to connect with. </param> public ISession getSallySession(){
/// <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;
//set up the logger. // if there is no connection, we can not return a session
BasicConfigurator.Configure(); if (this.connection == null || !this.connection.IsStarted) {
if (!startConnection()) throw new Exception(logFatal("No connection started yet. Start a connection and try again."));
{
// if we could not start the connection, we should complain.
Console.WriteLine("DotNetSallyClient <" + address + ">: Could not start connection");
logger.Fatal("DotNetSallyClient <" + address + ">: Could not start connection");
return;
} }
// we're done for now. // we can now return it.
Console.WriteLine("DotNetSallyClient <"+ address + ">: Started connection"); return this.session;
} }
#endregion
#region Logging
/// <summary> /// <summary>
/// returns the session of this SallyClient. /// Generates a log message
/// </summary> /// </summary>
/// <param name="message">Message content</param>
/// <returns></returns> /// <returns></returns>
public ISession getSallySession(){ private string logMessage(string message)
{
// if there is no connection, we can not return it. return "DotNetSallyClient <" + address + ">: " + message;
if (!this.connection.IsStarted) {
throw new Exception("DotNetSallyClient <" + address + ">: No connection started yet. Start a connection and try again.");
} }
//and return it. /// <summary>
return this.session; /// 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> /// <summary>
/// Registers a document with this Sally Client. /// Logs a fatal message to console and to the log.
/// </summary> /// </summary>
/// <param name="doc"></param> /// <param name="message"></param>
public void registerDocument(SallyDocument doc) private string logFatal(string message)
{ {
//just call the document method to do this. string msg = logMessage(message);
doc.registerWith(this);
logger.Fatal(msg);
return msg;
} }
#endregion
} }
} }
This diff is collapsed.
...@@ -16,20 +16,10 @@ namespace SallyConnect ...@@ -16,20 +16,10 @@ namespace SallyConnect
// we first create the sally client. // we first create the sally client.
DotNetSallyClient dns = new DotNetSallyClient("activemq:tcp://localhost:61616");//neptune.eecs.jacobs-university.de:61616 DotNetSallyClient dns = new DotNetSallyClient("activemq:tcp://localhost:61616");//neptune.eecs.jacobs-university.de:61616
SallyDocument doc = new SallyDocument(dns, docName, interfaces);
//and then register a document to it. doc.register();
SallyDocument sD = new SallyDocument(docName, interfaces);
dns.registerDocument(sD); // register the document.
//now just wait
Console.Read();
/*
System.Threading.Thread.Sleep(1000);
if (sD.isRegistered()){
sD.sendToSally("some message");
}
Console.Read(); Console.Read();
*/
} }
} }
} }
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment