/*********************************************************************/
/* */
/* AgentService Framework */
/* www.agentservice.it */
/* */
/* Copyright (C) 2007 l.i.d.o. - DIST, University of Genoa, Italy */
/* GNU Lesser General Public License */
/* */
/* This library is free software; you can redistribute it and/or */
/* modify it under the terms of the GNU Lesser General Public */
/* License as published by the Free Software Foundation, */
/* version 2.1 of the License. */
/* */
/* This library is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
/* Lesser General Public License for more details. */
/* */
/* You should have received a copy of the GNU Lesser General Public */
/* License along with this library; if not, write to the */
/* Free Software Foundation, Inc., 59 Temple Place - Suite 330, */
/* Boston, MA 02111-1307, USA. */
/* */
/*********************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Collections;
using AgentService.Platform.AMS;
using AgentService.Platform.Modules;
using AgentService.Platform.Modules.Default;
namespace AgentService.Agent.Runtime
{
///
/// RemoteDispatcher service: allows remote platforms and agents to receive events
///
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, AddressFilterMode = AddressFilterMode.Any)]
class RemoteEventsDispatcher : IRemoteEventsDispatcher
{
private RemoteMsgClient msgClient;
private IImprovedMessagingModule msgmod;
///
/// An hashtable containing the last timestamp a platform requested its events
///
private Hashtable timings = new Hashtable();
public Hashtable Timings
{
get
{
return this.timings;
}
}
///
/// An hashtable containing all the agents events for a given remote platform
///
private Hashtable Events = new Hashtable();
///
/// An hashtable containing the remote platform that ownes a given agent
///
private Hashtable Identifiers = new Hashtable();
public RemoteEventsDispatcher(RemoteMsgClient msgClient, IImprovedMessagingModule msg)
{
this.msgClient = msgClient;
this.msgmod = msg;
msgClient.OnIncomingConversation += new AgentService.Platform.MTS.ConversationDelegate(msgClient_OnIncomingConversation);
//msgClient.OnMessageArrive += new MessageArrivedDelegate(msgClient_OnMessageArrive);
}
void OnMessageArrive(object sender, IncomingMessageEventArgs args)
{
this.AddEvent(args.aid, new RemoteEvent(args.aid, AgentService.Platform.PlatformEvent.MessageReceived, null, "SimpleMessage"));
}
void OnConversationMessageArrive(object sender, IncomingConversationMessageEventArgs args)
{
this.AddEvent(args.aid, new RemoteEvent(args.aid, AgentService.Platform.PlatformEvent.MessageReceived, null, args.ConversationId));
}
///
/// The delegate that handles an OnIncomingConversation event
///
///
///
void msgClient_OnIncomingConversation(object sender, AgentService.Platform.MTS.ConversationArgs args)
{
RemoteConversation rmc = (RemoteConversation) args.Conversation;
RemoteConversationDescriptor desc = new RemoteConversationDescriptor(rmc.Peer,rmc.ConvId,rmc.State);
this.AddEvent(rmc.Owner, new RemoteEvent(rmc.Owner, AgentService.Platform.PlatformEvent.ConversationRequested,null,desc));
}
///
/// Registers a new remote platform
///
/// the platform guid
///
public bool AddPlatform(string platID)
{
bool bOk;
lock (this.Events)
{
if (Events.Contains(platID))
{
bOk = false;
}
else
{
Events.Add(platID, new List());
lock (this.timings)
{
timings.Add(platID, System.DateTime.Now);
}
bOk = true;
}
}
return bOk;
}
///
/// Deregisters a previously registered remote platform
///
/// the platform guid
///
public bool RemovePlatform(string platID)
{
bool bOk;
lock (this.Events)
{
if (Events.Contains(platID))
{
lock (this.timings)
{
timings.Remove(platID);
}
Events.Remove(platID);
bOk = true;
}
else
{
bOk = false;
}
}
return bOk;
}
///
/// Gets all the event for the agents of a given remote platform
///
/// the platform guid
///
public List GetAllEvents(string platID)
{
List list = new List();
lock (this.Events)
{
if (Events.Contains(platID))
{
foreach (RemoteEvent ev in ((List)Events[platID]))
{
list.Add(ev);
}
((List)Events[platID]).Clear();
lock (this.timings)
{
this.timings[platID] = System.DateTime.Now;
}
}
}
return list;
}
///
/// Registers a new remote agent
///
/// the aid of the agent
/// the id of the remote platform
///
public bool RegisterAgent(AID aid, string platID)
{
bool bOk;
lock (this.Events)
{
if (!Events.Contains(platID))
{
Events.Add(platID, new List());
}
}
lock (this.Identifiers)
{
if (Identifiers.Contains(aid))
{
bOk = false;
}
else
{
Identifiers.Add(aid, platID);
this.msgmod.RegisterToIncomingMessage(this.OnMessageArrive,aid);
this.msgmod.RegisterToIncomingConversationMessage(this.OnConversationMessageArrive, aid);
bOk = true;
}
}
return bOk;
}
///
/// Deregisters a previously registered remote agent
///
/// the aid of the agent
/// the remote platform guid
///
public bool DeRegisterAgent(AID aid, string platID)
{
bool bOk;
lock (this.Identifiers)
{
if (Identifiers.Contains(aid))
{
Identifiers.Remove(aid);
this.msgmod.DeregisterToIncomingMessage(this.OnMessageArrive, aid);
this.msgmod.DeregisterToIncomingConversationMessage(this.OnConversationMessageArrive, aid);
bOk = true;
}
else
{
bOk = false;
}
}
return bOk;
}
///
/// Adds a new remote event to be dispatched
///
/// the agent aid of the recipient agent
/// the remote event to be dispatched
///
public bool AddEvent(AID to, RemoteEvent ev)
{
bool bOk;
string platID;
lock (this.Identifiers)
{
if (Identifiers.Contains(to))
{
platID = (string)Identifiers[to];
}
else
{
platID = null;
}
}
if (platID != null)
{
lock (this.Events)
{
((List)this.Events[platID]).Add(ev);
bOk = true;
}
}
else
{
bOk = false;
}
return bOk;
}
}
}