本文整理汇总了C#中Agent.Close方法的典型用法代码示例。如果您正苦于以下问题:C# Agent.Close方法的具体用法?C# Agent.Close怎么用?C# Agent.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Agent
的用法示例。
在下文中一共展示了Agent.Close方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main(string[] args)
{
if(args.Length != 1)
{
Console.WriteLine(
"expecting one parameter: server destination");
return;
}
string serverAddress = args[0];
try
{
Agent clientAgent = new Agent();
// read lines of text from standard input
// and post each one for transmission
string inputLine = null;
while((inputLine = Console.ReadLine()) != null)
{
Parameters param = new Parameters();
// the "content" field name is arbitrary,
// but needs to be recognized at the server side
param.SetString("content", inputLine);
OutgoingMessage message =
clientAgent.Send(serverAddress,
"printer", "print", param);
message.WaitForTransmission();
message.Close();
}
clientAgent.Close();
}
catch(Exception ex)
{
Console.WriteLine(
"error: {0}", ex.Message);
}
}
示例2: Main
static void Main(string[] args)
{
if(args.Length != 1 && args.Length != 4)
{
Console.WriteLine(
"need 1 or 4 parameters:\n" +
" - server address\n" +
" - outgoing high water mark\n" +
" - outgoing low water mark\n" +
" - number of iterations\n" +
"If only server address is given," +
" the limits will have default values" +
" and the loop will be infinite");
return;
}
string serverAddress = args[0];
int numOfIterations = -1;
Parameters options = new Parameters();
if(args.Length == 4)
{
int outgoingHighWaterMark;
int outgoingLowWaterMark;
try
{
outgoingHighWaterMark = int.Parse(args[1]);
outgoingLowWaterMark = int.Parse(args[2]);
numOfIterations = int.Parse(args[3]);
}
catch(FormatException)
{
Console.WriteLine("invalid arguments");
return;
}
options.SetInteger("outgoing_high_water_mark",
outgoingHighWaterMark);
options.SetInteger("outgoing_low_water_mark",
outgoingLowWaterMark);
}
try
{
Agent clientAgent = new Agent(options);
Parameters param = new Parameters();
int index = 1;
while(true)
{
param.SetInteger("index", index);
clientAgent.SendOneWay(serverAddress,
"object", "message", param);
Console.WriteLine(
"posted message " + index);
if(numOfIterations > 0)
{
if(index == numOfIterations)
{
break;
}
}
++index;
}
clientAgent.Close();
}
catch(Exception ex)
{
Console.WriteLine(
"error: {0}", ex.Message);
}
}
示例3: testSubscribeUnsubscribe
public void testSubscribeUnsubscribe()
{
Agent publisherAgent = new Agent();
string publisherAddress =
publisherAgent.AddListener(localAddress);
ValuePublisher value = new ValuePublisher();
publisherAgent.RegisterValuePublisher("my_value", value);
// no subscribers yet
Assert.AreEqual(0, value.NumberOfSubscribers);
Assert.AreEqual(0, value.Subscribers.Count);
// set up the subscriber side
bool gotUpdate = false;
Agent subscriberAgent = new Agent();
subscriberAgent.RegisterObject("my_update_handler",
delegate(object sender, IncomingMessageArgs args)
{
Assert.AreEqual("subscription_update",
args.Message.MessageName);
gotUpdate = true;
});
// subscribe
Parameters parameters = new Parameters();
parameters.SetString("destination_object", "my_update_handler");
OutgoingMessage subscribeMsg = subscriberAgent.Send(
publisherAddress, "my_value", "subscribe", parameters);
subscribeMsg.WaitForCompletion();
// there should be one subscriber, as seen at the publisher side
Assert.AreEqual(1, value.NumberOfSubscribers);
Assert.AreEqual(1, value.Subscribers.Count);
Assert.AreEqual("my_update_handler",
value.Subscribers[0].DestinationObject);
// publish some value
Parameters dummy = new Parameters();
value.Publish(dummy);
// check if the listener got it
pause();
Assert.IsTrue(gotUpdate);
// unsubscribe
OutgoingMessage unsubscribeMsg = subscriberAgent.Send(
publisherAddress, "my_value", "unsubscribe", null);
unsubscribeMsg.WaitForCompletion();
// there should be no subscribers
Assert.AreEqual(0, value.NumberOfSubscribers);
Assert.AreEqual(0, value.Subscribers.Count);
// check that the updates do not arrive any longer
gotUpdate = false;
value.Publish(dummy);
pause();
Assert.IsFalse(gotUpdate);
value.Close();
subscriberAgent.Close();
publisherAgent.Close();
}
示例4: testUnknownCommands
public void testUnknownCommands()
{
// set up the publisher side
Agent publisherAgent = new Agent();
string publisherAddress =
publisherAgent.AddListener(localAddress);
bool gotUnknown = false;
ValuePublisher value = new ValuePublisher(
delegate(object sender, IncomingMessageArgs args)
{
Assert.AreEqual("unknown", args.Message.MessageName);
gotUnknown = true;
args.Message.Reply(null);
});
publisherAgent.RegisterValuePublisher("my_value", value);
// set up the subscriber side
Agent subscriberAgent = new Agent();
// send unknown command
OutgoingMessage unknownMsg = subscriberAgent.Send(
publisherAddress, "my_value", "unknown", null);
unknownMsg.WaitForCompletion();
Assert.IsTrue(gotUnknown);
subscriberAgent.Close();
publisherAgent.Close();
}
示例5: Main
static void Main(string[] args)
{
if(args.Length != 3)
{
Console.WriteLine(
"expecting three parameters: " +
"name server address and two integers");
return;
}
String nameServerAddress = args[0];
int a;
int b;
try
{
a = int.Parse(args[1]);
b = int.Parse(args[2]);
}
catch(Exception)
{
Console.WriteLine("cannot parse the parameters");
return;
}
try
{
Agent clientAgent = new Agent();
// obtain the address of calculator server
Parameters resolveParams = new Parameters();
resolveParams.SetString("object", "calculator");
OutgoingMessage nsQuery =
clientAgent.Send(nameServerAddress,
"names", "resolve", resolveParams);
nsQuery.WaitForCompletion();
if (nsQuery.State !=
OutgoingMessage.MessageState.REPLIED) {
Console.WriteLine("error: {0}",
nsQuery.ExceptionMsg);
return;
}
Parameters resolveReply = nsQuery.Reply;
String calculatorAddress =
resolveReply.GetString("location");
nsQuery.Close();
// send message to the calculator object
Parameters param = new Parameters();
param.SetInteger("a", a);
param.SetInteger("b", b);
OutgoingMessage message =
clientAgent.Send(calculatorAddress,
"calculator", "calculate", param);
message.WaitForCompletion();
OutgoingMessage.MessageState state =
message.State;
if(state == OutgoingMessage.MessageState.REPLIED)
{
Parameters reply = message.Reply;
int sum = reply.GetInteger("sum");
int difference = reply.GetInteger("difference");
int product = reply.GetInteger("product");
int ratio = 0;
Parameters.Entry ratioEntry =
reply.Find("ratio");
bool ratioDefined = ratioEntry != null;
if(ratioDefined)
{
ratio = ratioEntry.GetInteger();
}
Console.WriteLine("sum = {0}", sum);
Console.WriteLine("difference = {0}",
difference);
Console.WriteLine("product = {0}", product);
Console.Write("ratio = ");
if(ratioDefined)
{
Console.WriteLine(ratio);
}
else
{
Console.WriteLine("<undefined>");
}
}
else if(state ==
OutgoingMessage.MessageState.REJECTED)
{
//.........这里部分代码省略.........
示例6: testRawBinary
public void testRawBinary()
{
bool gotMessage = false;
Encoding encoding = Encoding.UTF8;
byte[] messageContent =
encoding.GetBytes("Hello this is raw binary message");
byte[] replyContent =
encoding.GetBytes("Hi and this is raw binary response !");
Parameters options = new Parameters();
options.SetBoolean(OptionNames.DELIVER_AS_RAW_BINARY, true);
Agent rawServerAgent = new Agent(options);
string rawServerAddress =
rawServerAgent.AddListener("tcp://*:*");
Agent rawClientAgent = new Agent(options);
rawServerAgent.RegisterObject(objectName,
delegate(object sender, IncomingMessageArgs args)
{
gotMessage = true;
byte[] content = args.Message.RawContent;
Assert.AreEqual(messageContent, content);
args.Message.Reply(
new RawBinaryDataSource(replyContent));
});
OutgoingMessage message = rawClientAgent.Send(
rawServerAddress, objectName, messageName,
new RawBinaryDataSource(messageContent));
message.WaitForCompletion();
Assert.IsTrue(gotMessage);
Assert.AreEqual(
OutgoingMessage.MessageState.REPLIED, message.State);
byte[] reply = message.RawReply;
Assert.AreEqual(replyContent, reply);
message.Close();
rawClientAgent.Close();
rawServerAgent.Close();
}
示例7: testLoadBalancing
public void testLoadBalancing()
{
Agent serverAgent1 = new Agent();
string serverAddress1 = serverAgent1.AddListener("tcp://*:*");
Agent serverAgent2 = new Agent();
string serverAddress2 = serverAgent2.AddListener("tcp://*:*");
string loadBalancedTarget = string.Format(
"failover:({0}|{1})", serverAddress1, serverAddress2);
bool gotMessage1 = false;
bool gotMessage2 = false;
serverAgent1.RegisterObject(objectName,
delegate(object sender, IncomingMessageArgs args)
{
gotMessage1 = true;
args.Message.Reply(null);
});
serverAgent2.RegisterObject(objectName,
delegate(object sender, IncomingMessageArgs args)
{
gotMessage2 = true;
args.Message.Reply(null);
});
OutgoingMessage message = clientAgent.Send(
loadBalancedTarget, objectName, messageName, null);
// since this is a load-balanced (and failover) target,
// the message is implicitly waited for completion
Assert.AreEqual(
OutgoingMessage.MessageState.REPLIED, message.State);
// exactly one of two servers got the message
// (exclusive-or of 1st or 2nd receiver gotMessage)
Assert.IsTrue(gotMessage1 ^ gotMessage2);
message.Close();
serverAgent1.Close();
serverAgent2.Close();
}