/*********************************************************************/
/* */
/* 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.IO;
using System.Reflection;
using AgentService.Interop;
using AgentService.Platform;
using AgentService.Platform.AMS;
using AgentService.Platform.MTS;
using AgentService.Platform.Modules;
using AgentService.Agent.Runtime;
using AgentService.Agent;
using AgentService.Agent.Behaviour;
namespace AgentService.Platform
{
///
/// Class ServiceController. Manages the services exposed by the
/// AgentService platform.
///
[Serializable]
public class ServiceController : IServiceController
{
///
/// Reference to the bound platform
///
private Platform platform;
///
/// Constructs a module controller object with the boud platform
///
/// Reference to the platform object to be bound
public ServiceController(Platform platform)
{
this.platform = platform;
}
#region AMS Commands
///
/// Returns all the agents registered with the platform
///
///
public AID[] GetRegisteredAgents()
{
AgentDescriptionCollection adc = this.platform.AMS.RegisteredAgents;
AID[] aids = new AID[adc.Count];
int i = 0;
foreach(AgentDescription ad in adc)
{
aids[i] = ad.AID;
i = i + 1;//demartini
}
return aids;
}
///
/// Creates an agent with the given name and configuration file by using the specified template with
///
/// name of the agent
/// full name of the AgentTemplate derived class that defines the agent
/// configuration file
/// true, if the agent need to be created as persistent
/// AID of the newly created agent
public AID CreateAgent(string type, string name, string config, bool bPersist)
{
return this.platform.CreateAgent(type, name, config, bPersist);
}
//MOD: 19/04/2006 : Marco Garr added overloaded CreateAgent to support AppDomain
///
/// Creates an agent in domain with the given name and configuration file by using the specified template with
///
/// name of the agent
/// full name of the AgentTemplate derived class that defines the agent
/// configuration file
/// true, if the agent need to be created as persistent
/// true, if the agent will be runned in an AppDomain
/// AID of the newly created agent
public AID CreateAgent(string type, string name, string config, bool bPersist, bool bDomain)
{
return this.platform.CreateAgent(type, name, config, bPersist, false, bDomain);
}
///
/// Creates an agent in given domain name with the given name and configuration file by using the specified template with
///
/// name of the agent
/// full name of the AgentTemplate derived class that defines the agent
/// configuration file
/// true, if the agent need to be created as persistent
/// the name of the AppDomain in which the Agent is created
/// AID of the newly created agent
public AID CreateAgent(string type, string name, string config, bool bPersist, string domainName)
{
if ((domainName=="") || (domainName==null))
{
return this.CreateAgent(type, name, config, bPersist, true);
}
else
{
return this.platform.CreateAgent(type, name, config, bPersist, false, true, domainName);
}
}
///
/// Activates the given list of agents
///
/// list of aids identifying the agents that need to be activated
public void ActivateAgent(params AID[] aids)
{
foreach(AID aid in aids)
{
this.platform.AMS.Activate(aid);
}
}
///
/// Activates the given list of agents
///
/// list of aids identifying the agents that need to be activated
public void ActivateAllAgents()
{
this.platform.AMS.ActivateAll();
}
///
/// Wait for all the termination of all the active agent
///
public void WaitForAllAgents()
{
if (this.platform.State == PlatformState.Running)
{
object handle = this.platform.activeHandle;
try
{
System.Threading.Monitor.Enter(handle);
if (this.platform.activeAgents > 0)
System.Threading.Monitor.Wait(handle);
}
finally
{
System.Threading.Monitor.Exit(handle);
}
}
}
///
/// Terminate the given list of agents
///
/// agent that will be terminated
/// True if the agent kill process succeds
public bool KillAgent(params AID[] aids)
{
// TODO: Checks error
/*foreach(AID aid in aids)
{
this.platform.AMS.Destroy(aid);
}*/
foreach(AID aid in aids)
{
try
{
int count = this.platform.AMS.agents.Count;
for(int i=0;i
/// Terminate all the agents running into AgentService platform
///
public void KillEmAll()
{
this.platform.AMS.DestroyAll();
}
///
/// Suspends the given list of agents
///
/// list of agents that will be suspended
/// true if operation succeds
public bool SuspendAgent(params AID[] aids)
{
bool bOk = false;
foreach(AID aid in aids)
{
bOk = this.platform.AMS.Suspend(aid);
}
return bOk;
}
///
/// Suspends all the agents
///
public void SuspendAllAgents()
{
this.platform.AMS.SuspendAll();
}
///
/// Wakes up the given list of agents
///
/// list of agents that will be suspended
public bool WakeUpAgent(params AID[] aids)
{
bool bOk = true;
foreach(AID aid in aids)
{
bOk = bOk && this.platform.AMS.WakeUp(aid);
}
return bOk;
}
///
/// Wakes up all the suspended agents
///
public void WakeUpAllAgents()
{
this.platform.AMS.WakeUpAll();
}
public AgentDescription GetAgentDescription(AID aid)
{
foreach(AgentDescription description in this.platform.AMS.RegisteredAgents)
{
if(description.AID.Equals(aid))
{
return description;
}
}
return null;
}
public AgentDescription[] SearchAgent(string pattern)
{
return this.platform.WhitePageSvc.Search(pattern);
}
public AgentDescription SearchExactAgent(string name)
{
return this.platform.WhitePageSvc.SearchExact(name);
}
#endregion
#region DF Commands
public AgentService.Platform.DF.DFAgentDescription[] SearchAgent(AgentService.Platform.DF.ServiceDescription sd)
{
return this.platform.YellowPageSvc.SearchAgent(sd);
}
public AgentService.Platform.DF.DFAgentDescription[] SearchAgent(AgentService.Platform.DF.ServiceDescription sd, AgentService.Platform.DF.SearchConstraints constraint)
{
return this.platform.YellowPageSvc.SearchAgent(sd,constraint);
}
public void Register(AgentService.Platform.DF.DFAgentDescription description)
{
this.platform.YellowPageSvc.Register(description);
}
public void Edit(AgentService.Platform.DF.DFAgentDescription description)
{
this.platform.YellowPageSvc.Edit(description);
}
public void Deregister(AID aid)
{
this.platform.YellowPageSvc.Deregister(aid);
}
#endregion
#region Messaging
///
/// Gets a client for managing a message queue associated to the batch process
///
/// Name of the batch the message queue belongs to
///
public IMsgService GetMsgClient(string clientId)
{
AID clientAid = new AID(clientId, this.platform.Description.Name);
if (this.platform.AMS.RegisteredAgents.Contains(clientAid) == true)
{
return null;
}
return new MsgBatchService(clientAid, this.platform.MTS);
}
///
/// Gets a collection of filtered messages
///
/// aid of the agent
/// filter criteria
/// The filtered messages collection. If null exception occurs
public AgentService.Platform.MTS.AgentMessageCollection FilterMessage(AID aid, AgentService.Platform.MTS.MessageFilter filter)
{
AgentMessageCollection amc = this.platform.Messaging.GetMessages(aid, filter, 0, false);
return amc;
}
///
/// Clears all the messages in all the agent queues.
///
public void ClearAllMessages()
{
this.platform.Messaging.ClearAllMessages();
}
///
/// Clears all the messages in the queue of the given agent.
///
/// agent identifier
/// true if successful
public bool ClearAllMessages(AID aid)
{
return this.platform.Messaging.ClearAllMessages(aid);
}
#endregion
}
}