/*********************************************************************/
/* */
/* 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 AgentService.Platform.AMS;
using System.ServiceModel;
using AgentService.Platform.MTS;
using System.Threading;
using AgentService.Platform.Modules;
using System.Xml.Linq;
using System.Collections;
namespace AgentService.Agent.Runtime
{
///
/// RemoteRegisterClient: manages registrations/deregistrations of mobile
/// platforms and agents
///
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, AddressFilterMode = AddressFilterMode.Any)]
class RemoteRegisterClient : IRemoteRegisterClient
{
private AgentService.Platform.AMS.AMS AMS;
private AgentService.Platform.MTS.MTS MTS;
private RemoteMsgClient rmsgc;
private RemoteEventsDispatcher rmevd;
///
/// The checking Time for the presence of remote platforms (in seconds)
///
private int checkTime;
private Thread pollingThread;
///
/// If true does the polling to check the presence of the remote platforms
///
public bool doPolling { get; set; }
///
/// An hashtable containing all the registered agent for a remote platform
///
private Hashtable RegisteredAID = new Hashtable();
public RemoteRegisterClient(AMS ams, MTS mts, RemoteMsgClient msgc, RemoteEventsDispatcher rmevd, int checkTime)
{
doPolling = true;
this.AMS = ams;
this.MTS = mts;
this.rmsgc = msgc;
this.rmevd = rmevd;
this.checkTime = checkTime;
pollingThread = new Thread(new ThreadStart(this.CheckTimeout));
pollingThread.Start();
}
#region IRemoteRegister Members
///
/// Registers a Mobile platform
///
/// The platform ID
/// The userid
/// The password
///
public bool RegisterPlatform(string platID, string user, string pwd)
{
XElement tree = XElement.Load("users.xml");
XElement el = tree.Element("Users");
foreach (XElement el1 in tree.Elements("User"))
{
string s = el1.Attribute("userid").Value;
string s1 = el1.Attribute("password").Value;
if (user.Equals(s) && pwd.Equals(s1))
{
RegisteredAID.Add(platID, new List());
return this.rmevd.AddPlatform(platID);
}
}
return false;
}
///
/// Deregisters a mobile platform
///
/// the platform id
///
public bool DeregisterPlatform(string platID)
{
if(RegisteredAID.Contains(platID)) RegisteredAID.Remove(platID);
return this.rmevd.RemovePlatform(platID);
}
///
/// Registers a mobile agent
///
/// the agent aid
/// the id of the owner platform
///
public bool RegisterMobile(AgentService.Platform.AMS.AID aid, string platID)
{
((List)RegisteredAID[platID]).Add(aid);
bool b0 = AMS.RegisterMobile(aid);
bool b1 = MTS.Register(aid);
bool b2 = this.rmsgc.RegisterAgent(aid);
bool b3 = this.rmevd.RegisterAgent(aid, platID);
return (b0 && b1 && b2 && b3);
}
///
/// Deregisters a mobile agent
///
/// the agent aid
/// the id of the owner platform
///
public bool DeregisterMobile(AgentService.Platform.AMS.AID aid, string platID)
{
((List)RegisteredAID[platID]).Remove(aid);
bool b0 = this.rmevd.DeRegisterAgent(aid, platID);
bool b1 = AMS.DeregisterMobile(aid);
bool b2 = MTS.Deregister(aid);
bool b3 = this.rmsgc.DeRegisterAgent(aid);
return (b0 && b1 && b2 && b3);
}
///
/// Checks if the registered remote platforms are connected in the
/// last two minutes, otherwise deregisters them and their agents
///
public void CheckTimeout()
{
try
{
while (this.doPolling == true)
{
List platToRemove = new List();
DateTime time = System.DateTime.Now;
foreach (string platID in this.rmevd.Timings.Keys)
{
DateTime lasttime = (DateTime)this.rmevd.Timings[platID];
if ((time.Ticks - lasttime.Ticks) / 1000000 > (this.checkTime*10))
{
var list = ((List)this.RegisteredAID[platID]);
for (int i = 0; i < list.Count; i++)
{
this.DeregisterMobile(list[i], platID);
i--;
}
platToRemove.Add(platID);
}
}
foreach (string platID in platToRemove)
{
this.RegisteredAID.Remove(platID);
this.DeregisterPlatform(platID);
this.rmevd.Timings.Remove(platID);
}
platToRemove.Clear();
//two minutes
Thread.Sleep(this.checkTime*1000);
}
}
catch (ThreadAbortException abortException)
{
}
}
public void KillPollingThread()
{
this.doPolling = false;
this.pollingThread.Abort();
}
#endregion
}
}