2013-01-18 15:55:50 +00:00
|
|
|
using System.Threading;
|
2014-01-08 14:16:39 +00:00
|
|
|
using AsterNET.Manager.Action;
|
|
|
|
using AsterNET.Manager.Event;
|
|
|
|
using AsterNET.Manager.Response;
|
2013-01-18 15:55:50 +00:00
|
|
|
|
2014-01-08 14:16:39 +00:00
|
|
|
namespace AsterNET.Manager
|
2013-01-18 15:55:50 +00:00
|
|
|
{
|
2015-01-03 15:37:29 +00:00
|
|
|
/// <summary>
|
|
|
|
/// A combinded event and response handler that adds received events and the response to a ResponseEvents object.
|
|
|
|
/// </summary>
|
|
|
|
public class ResponseEventHandler : IResponseHandler
|
|
|
|
{
|
|
|
|
private ManagerActionEvent action;
|
|
|
|
private AutoResetEvent autoEvent;
|
|
|
|
private ManagerConnection connection;
|
|
|
|
private ResponseEvents events;
|
|
|
|
private int hash;
|
2013-01-18 15:55:50 +00:00
|
|
|
|
2015-01-03 15:37:29 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Creates a new instance.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="events">the ResponseEvents to store the events in</param>
|
|
|
|
/// <param name="actionCompleteEventClass">the type of event that indicates that all events have been received</param>
|
|
|
|
/// <param name="thread">the thread to interrupt when the actionCompleteEventClass has been received</param>
|
|
|
|
public ResponseEventHandler(ManagerConnection connection, ManagerActionEvent action, AutoResetEvent autoEvent)
|
|
|
|
{
|
|
|
|
this.connection = connection;
|
|
|
|
this.events = new ResponseEvents();
|
|
|
|
this.action = action;
|
|
|
|
this.autoEvent = autoEvent;
|
|
|
|
}
|
2013-01-18 15:55:50 +00:00
|
|
|
|
2015-01-03 15:37:29 +00:00
|
|
|
public ResponseEvents ResponseEvents
|
|
|
|
{
|
|
|
|
get { return events; }
|
|
|
|
}
|
2013-01-18 15:55:50 +00:00
|
|
|
|
2015-01-03 15:37:29 +00:00
|
|
|
public ManagerAction Action
|
|
|
|
{
|
|
|
|
get { return action; }
|
|
|
|
}
|
2013-01-18 15:55:50 +00:00
|
|
|
|
2015-01-03 15:37:29 +00:00
|
|
|
public int Hash
|
|
|
|
{
|
|
|
|
get { return hash; }
|
|
|
|
set { hash = value; }
|
|
|
|
}
|
2013-01-18 15:55:50 +00:00
|
|
|
|
2015-01-03 15:37:29 +00:00
|
|
|
public void Free()
|
|
|
|
{
|
|
|
|
connection = null;
|
|
|
|
autoEvent = null;
|
|
|
|
action = null;
|
|
|
|
events.Events.Clear();
|
|
|
|
events.Response = null;
|
|
|
|
events = null;
|
|
|
|
}
|
2013-01-18 15:55:50 +00:00
|
|
|
|
2015-01-03 15:37:29 +00:00
|
|
|
public void HandleResponse(ManagerResponse response)
|
|
|
|
{
|
|
|
|
events.Response = response;
|
|
|
|
if (response is ManagerError)
|
|
|
|
events.Complete = true;
|
2013-01-18 15:55:50 +00:00
|
|
|
|
2015-01-03 15:37:29 +00:00
|
|
|
if (events.Complete && autoEvent != null)
|
|
|
|
autoEvent.Set();
|
|
|
|
}
|
2013-01-18 15:55:50 +00:00
|
|
|
|
2015-01-03 15:37:29 +00:00
|
|
|
public void HandleEvent(ManagerEvent e)
|
|
|
|
{
|
|
|
|
// should always be a ResponseEvent, anyway...
|
|
|
|
if (e is ResponseEvent)
|
|
|
|
{
|
|
|
|
var responseEvent = (ResponseEvent) e;
|
|
|
|
events.AddEvent(responseEvent);
|
|
|
|
}
|
2013-01-18 15:55:50 +00:00
|
|
|
|
2015-01-03 15:37:29 +00:00
|
|
|
// finished?
|
|
|
|
if (action.ActionCompleteEventClass().IsAssignableFrom(e.GetType()))
|
|
|
|
{
|
|
|
|
lock (events)
|
|
|
|
events.Complete = true;
|
|
|
|
if (events.Response != null)
|
|
|
|
autoEvent.Set();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|