using System;
using AgentService.Agent;
using AgentService.Agent.Runtime;
using AgentService.Agent.Behaviour;
using AgentService.Agent.Knowledge;
using AgentService.Platform.AMS;
using AgentService.Platform.MTS;
using AgentService.Platform.DF;
namespace TestMessaging
{
///
/// Class StartConv. This behaviour is used
/// to simply open a conversations and send
/// messages through it.
///
[Knowledge(typeof(ConvData))]
public class StartConv : Behaviour
{
///
/// knowledge conversation data.
///
protected ConvData convData;
///
/// Constracts a StartConv behaviour.
///
/// behaviour name
/// conversation data knowledge
public StartConv(string name, ConvData convData) : base(name, convData)
{
this.convData = convData;
}
///
///
///
public override void Body()
{
int repetitions = 0;
string peer = string.Empty;
int maxMess = 0;
try
{
this.LockKnowledge(this.convData);
repetitions = this.convData.GetRepetitions(this);
peer = this.convData.GetPeerName(this);
maxMess = this.convData.GetMaxMessages(this);
}
finally
{
this.ReleaseKnowledge(true);
}
AID aid = null;
int atIndex = peer.IndexOf('@');
if (atIndex >= 0)
{
string name = peer.Substring(0, atIndex);
string platform = peer.Substring(atIndex + 1);
aid = new AID(name, platform);
}
else
{
aid = new AID(peer, this.Owner.HapName);
}
while (repetitions > 0)
{
IConversation conv = this.Runtime.MessageSvc.OpenConversation(aid);
if (conv.WaitForAck(TimeSpan.Zero) == false)
{
this.Runtime.Console.AppendMessage("Attempt #{0} with {1} failed", repetitions, aid.GlobalName);
}
else
{
this.Runtime.Console.AppendMessage("Attempt #{0} with {1} succeeded", repetitions, aid.GlobalName);
}
for(int seqMess = 0; seqMess < maxMess; seqMess++)
{
MessageBody body = new MessageBody();
body.Add("text", "hello");
body.Add("sequence", seqMess);
bool bOk = conv.SendMessage(body);
if (bOk == true)
{
this.Runtime.Console.AppendMessage("SUCCESS - Message #{0} sent", seqMess);
}
else
{
this.Runtime.Console.AppendMessage("FAILED - Message #{0} not sent", seqMess);
}
}
conv.Close();
repetitions--;
}
}
}
}