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 HandleConv. This behaviour is used
/// to simply receive a conversation request
/// and handle it according to the information
/// mantained in the knowledge.
///
[Knowledge(typeof(ConvData))]
public class HandleConv : Behaviour
{
///
/// knowledge conversation data.
///
protected ConvData convData;
///
/// Constracts a HandleConv behaviour.
///
/// behaviour name
/// conversation data knowledge
public HandleConv(string name, ConvData convData) : base(name, convData)
{
this.convData = convData;
}
///
///
///
public override void Body()
{
bool bRefuse = false;
int repetitions = 0;
int maxMess = 0;
try
{
this.LockKnowledge(this.convData);
bRefuse = this.convData.GetRefuse(this);
repetitions = this.convData.GetRepetitions(this);
maxMess = this.convData.GetMaxMessages(this);
}
finally
{
this.ReleaseKnowledge(true);
}
int sequence = 0;
do
{
while (this.IncomingConvs.Count == 0)
{
System.Threading.Thread.Sleep(50);
}
IConversation conv = this.IncomingConvs[0];
if (bRefuse == true)
{
conv.Reject();
this.Runtime.Console.AppendMessage("Attempt #{0}, from {1}: Rejected", sequence, conv.Peer.GlobalName);
}
else
{
conv.Accept();
this.Runtime.Console.AppendMessage("Attempt #{0}, from {1}: Accepted", sequence, conv.Peer.GlobalName);
for (int i = 0; i < maxMess; i++)
{
AgentMessage mess = conv.WaitForMessage();
if (mess != null)
{
// mess = conv.ReceiveMessage();
int seq = (int) mess.Body["sequence"];
string text = mess.Body["text"] as string;
this.Runtime.Console.AppendMessage("Received message #{0}: {1}", seq, text);
}
}
}
this.IncomingConvs.Remove(conv);
sequence++;
bRefuse = !bRefuse;
} while (sequence < repetitions);
}
}
}