本文整理汇总了C#中Parameters.SetInteger方法的典型用法代码示例。如果您正苦于以下问题:C# Parameters.SetInteger方法的具体用法?C# Parameters.SetInteger怎么用?C# Parameters.SetInteger使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Parameters
的用法示例。
在下文中一共展示了Parameters.SetInteger方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: calculator
private static void calculator(
object sender, IncomingMessageArgs args)
{
// extract the parameters for calculations
Parameters param = args.Message.Parameters;
int a = param.GetInteger("a");
int b = param.GetInteger("b");
// prepare the answer
// with results of four calculations
Parameters replyParams = new Parameters();
replyParams.SetInteger("sum", a + b);
replyParams.SetInteger("difference", a - b);
replyParams.SetInteger("product", a * b);
// if the ratio cannot be computed,
// it is not included in the response
// the client will interpret that fact properly
if(b != 0)
{
replyParams.SetInteger("ratio", a / b);
}
args.Message.Reply(replyParams);
Console.WriteLine(
"got message with parameters {0} and {1}" +
", response has been sent back", a, b);
}
示例2: Main
static void Main(string[] args)
{
if(args.Length != 1)
{
Console.WriteLine(
"expecting one parameter: " +
"publisher destination");
return;
}
string publisherAddress = args[0];
try
{
ValuePublisher randomValue =
new ValuePublisher();
Agent publisherAgent = new Agent();
string resolvedAddress =
publisherAgent.AddListener(publisherAddress);
Console.WriteLine(
"The publisher is listening on {0}",
resolvedAddress);
publisherAgent.RegisterValuePublisher(
"random_number", randomValue);
// publish random values forever
Parameters content = new Parameters();
Random generator = new Random();
while(true)
{
int random = generator.Next(0, 100);
content.SetInteger("value", random);
Console.WriteLine("publishing value {0}",
random);
randomValue.Publish(content);
Thread.Sleep(1000);
}
}
catch(Exception ex)
{
Console.WriteLine(
"error: {0}", ex.Message);
}
}
示例3: testForEstablishedConnection
public void testForEstablishedConnection()
{
try
{
// artificial listening socket for this test
Socket server = new Socket(
AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// bind to system-assigned port
server.Bind(new IPEndPoint(IPAddress.Loopback, 0));
server.Listen(1);
string target = "tcp://127.0.0.1:" +
((IPEndPoint)server.LocalEndPoint).Port.ToString();
try
{
Parameters parameters = new Parameters();
parameters.SetInteger(
OptionNames.TCP_CONNECT_TIMEOUT, 1000);
Channel ch = new Channel(
target, new Options(parameters), null,
null, LogEventArgs.LogLevel.LOW);
Socket accepted = server.Accept();
ch.close();
accepted.Close();
server.Close();
}
catch(Exception)
{
Assert.Fail("should never reach this point");
}
}
catch(Exception)
{
Assert.Fail("should never reach this point");
}
}
示例4: Main
static void Main(string[] args)
{
if(args.Length != 1 && args.Length != 3)
{
Console.WriteLine(
"need 1 or 3 parameters:\n" +
" - server address\n" +
" - incoming high water mark\n" +
" - incoming low water mark\n" +
"If only server address is given," +
" the limits will have default values");
return;
}
string serverAddress = args[0];
Parameters options = new Parameters();
if(args.Length == 3)
{
int incomingHighWaterMark;
int incomingLowWaterMark;
try
{
incomingHighWaterMark = int.Parse(args[1]);
incomingLowWaterMark = int.Parse(args[2]);
}
catch(FormatException)
{
Console.WriteLine("invalid arguments");
return;
}
options.SetInteger("incoming_high_water_mark",
incomingHighWaterMark);
options.SetInteger("incoming_low_water_mark",
incomingLowWaterMark);
}
try
{
Agent serverAgent = new Agent(options);
string resolvedAddress =
serverAgent.AddListener(serverAddress);
Console.WriteLine(
"The server is listening on " +
resolvedAddress);
serverAgent.RegisterObject(
"object", Consumer);
// block
while(true)
{
Thread.Sleep(10000);
}
}
catch(Exception ex)
{
Console.WriteLine(
"error: {0}", ex.Message);
}
}
示例5: 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);
}
}
示例6: 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)
{
//.........这里部分代码省略.........
示例7: deserializeFromBytes
private static void deserializeFromBytes(Parameters parameters,
ByteIterator bytes)
{
int numOfEntries = consumeInt(bytes);
for(int i = 0; i != numOfEntries; ++i)
{
string entryName = consumeString(bytes);
int typeCode = consumeInt(bytes);
Parameters.EntryType type = codeToType(typeCode);
switch(type)
{
case Parameters.EntryType.BOOLEAN:
{
int value = consumeInt(bytes);
parameters.SetBoolean(entryName, value != 0);
}
break;
case Parameters.EntryType.INTEGER:
{
int value = consumeInt(bytes);
parameters.SetInteger(entryName, value);
}
break;
case Parameters.EntryType.LONG:
{
long value = consumeLong(bytes);
parameters.SetLong(entryName, value);
}
break;
case Parameters.EntryType.DOUBLE:
{
double value = consumeDouble(bytes);
parameters.SetDouble(entryName, value);
}
break;
case Parameters.EntryType.STRING:
{
string value = consumeString(bytes);
parameters.SetString(entryName, value);
}
break;
case Parameters.EntryType.BINARY:
{
byte[] value = consumeBinary(bytes);
parameters.SetBinary(entryName, value);
}
break;
case Parameters.EntryType.BOOLEAN_ARRAY:
{
int arrayLength = consumeInt(bytes);
int bytesNeeded =
(arrayLength + BITS_IN_BYTE - 1)
/ BITS_IN_BYTE;
byte[] packedArray =
consumeBytes(bytes, bytesNeeded);
bool[] array = new bool[arrayLength];
for(int j = 0; j != arrayLength; ++j)
{
int bytePosition = j / BITS_IN_BYTE;
int bitPosition = j % BITS_IN_BYTE;
int mask = 1 << bitPosition;
if((packedArray[bytePosition] & mask) != 0)
{
array[j] = true;
}
}
parameters.SetBooleanArray(entryName, array);
}
break;
case Parameters.EntryType.INTEGER_ARRAY:
{
int arrayLength = consumeInt(bytes);
int[] array = new int[arrayLength];
for(int j = 0; j != arrayLength; ++j)
{
array[j] = consumeInt(bytes);
}
parameters.SetIntegerArray(entryName, array);
}
break;
case Parameters.EntryType.LONG_ARRAY:
{
int arrayLength = consumeInt(bytes);
long[] array = new long[arrayLength];
for(int j = 0; j != arrayLength; ++j)
{
array[j] = consumeLong(bytes);
}
parameters.SetLongArray(entryName, array);
}
break;
case Parameters.EntryType.DOUBLE_ARRAY:
{
int arrayLength = consumeInt(bytes);
double[] array = new double[arrayLength];
for(int j = 0; j != arrayLength; ++j)
//.........这里部分代码省略.........
示例8: testForNesting
public void testForNesting()
{
parameters.SetInteger("name", 123);
Assert.IsTrue(parameters.Count == 1);
Parameters nested = new Parameters();
parameters.SetNestedParameters("nested", nested);
Assert.IsTrue(parameters.Count == 2);
nested.SetInteger("internal", 456);
Assert.IsTrue(nested.Count == 1);
nested.SetDouble("internal2", 3.125);
Assert.IsTrue(parameters.Count == 2);
Assert.IsTrue(nested.Count == 2);
Parameters tmp = parameters.GetNestedParameters("nested");
Assert.AreEqual(tmp, nested);
Parameters nested2 = new Parameters();
nested.SetNestedParameters("more nested", nested2);
Assert.IsTrue(parameters.Count == 2);
Assert.IsTrue(nested.Count == 3);
nested2.SetInteger("more internal", 789);
Assert.IsTrue(parameters.Count == 2);
Assert.IsTrue(nested.Count == 3);
Assert.IsTrue(nested2.Count == 1);
Assert.AreEqual(parameters.GetType("name"),
Parameters.EntryType.INTEGER);
Assert.AreEqual(parameters.GetType("nested"),
Parameters.EntryType.NESTED_PARAMETERS);
Assert.AreEqual(nested.GetType("internal"),
Parameters.EntryType.INTEGER);
Assert.AreEqual(nested.GetType("internal2"),
Parameters.EntryType.DOUBLE);
Assert.AreEqual(nested.GetType("more nested"),
Parameters.EntryType.NESTED_PARAMETERS);
Assert.AreEqual(nested2.GetType("more internal"),
Parameters.EntryType.INTEGER);
try
{
nested.GetType("blabla");
Assert.Fail("should never reach this point");
}
catch(NoSuchNameException)
{
// expected, ignore
}
IEnumerator<Parameters.Entry> it = parameters.GetEnumerator();
Assert.IsTrue(it.MoveNext());
Parameters.Entry e = it.Current;
Assert.AreEqual(e.Type, Parameters.EntryType.INTEGER);
Assert.IsTrue(it.MoveNext());
e = it.Current;
Assert.AreEqual(e.Type, Parameters.EntryType.NESTED_PARAMETERS);
Parameters n = e.GetNestedParameters();
Assert.IsTrue(n.Count == 3);
Assert.IsFalse(it.MoveNext());
Assert.AreEqual("entry 0:\n"
+ "name: name\n"
+ "integer: 123\n"
+ "entry 1:\n"
+ "name: nested\n"
+ "nested parameters:\n"
+ " entry 0:\n"
+ " name: internal\n"
+ " integer: 456\n"
+ " entry 1:\n"
+ " name: internal2\n"
+ " double: 3.125\n"
+ " entry 2:\n"
+ " name: more nested\n"
+ " nested parameters:\n"
+ " entry 0:\n"
+ " name: more internal\n"
+ " integer: 789\n",
parameters.ToString());
byte[] expected = {
2, 0, 0, 0, // num of entries
4, 0, 0, 0, // length of "name"
(byte)'n', (byte)'a', (byte)'m', (byte)'e',
2, 0, 0, 0, // type code for integer
123, 0, 0, 0, // value
6, 0, 0, 0, // length of "nested"
(byte)'n', (byte)'e', (byte)'s', (byte)'t',
(byte)'e', (byte)'d', 0, 0,
13, 0, 0, 0, // type code for nested
3, 0, 0, 0, // num of entries in nested
8, 0, 0, 0, // length o "internal"
//.........这里部分代码省略.........
示例9: testForLocking
public void testForLocking()
{
parameters.SetInteger("name", 123);
bool IsLocked = parameters.IsLocked("name");
Assert.IsFalse(IsLocked);
long key = 1234567890;
parameters.Lock("name", key);
IsLocked = parameters.IsLocked("name");
Assert.IsTrue(IsLocked);
try
{
parameters.Lock("name", key);
Assert.Fail("should never reach here");
}
catch(BadStateException e)
{
// already locked
Assert.AreEqual(
"The object 'name' is not in the appropriate state.",
e.Message);
}
try
{
// cannot modify
parameters.SetInteger("name", 456);
Assert.Fail("should never reach here");
}
catch(BadStateException e)
{
// locked, cannot modify
Assert.AreEqual(
"The object 'name' is not in the appropriate state.",
e.Message);
}
try
{
parameters.Unlock("name", key + 1);
Assert.Fail("should never reach here");
}
catch(UnexpectedValueException e)
{
// bad key
Assert.AreEqual("Unexpected value: bad key", e.Message);
}
parameters.Unlock("name", key);
IsLocked = parameters.IsLocked("name");
Assert.IsFalse(IsLocked);
try
{
parameters.Unlock("name", key);
Assert.Fail("should never reach here");
}
catch(BadStateException e)
{
// already unlocked
Assert.AreEqual(
"The object 'name' is not in the appropriate state.",
e.Message);
}
// unlocked, can modify
parameters.SetInteger("name", 456);
int value = parameters.GetInteger("name");
Assert.IsTrue(value == 456);
// nested test
Parameters nested = new Parameters();
parameters.SetNestedParameters("nested", nested);
nested.SetInteger("internal", 789);
Parameters nested2 = new Parameters();
nested.SetNestedParameters("more nested", nested2);
nested2.SetInteger("more internal", 987);
// lock something within the hierarchy
long otherKey = 987654321;
nested.Lock("internal", otherKey);
try
{
// cannot modify
nested.SetInteger("internal", 123);
Assert.Fail("should never reach here");
}
//.........这里部分代码省略.........
示例10: testBigMessages
public void testBigMessages()
{
// Note:
// The messages are sent with different priorities, which means
// that they might complete in the order that is different from the
// order of posting them to the outgoing queue.
// The messages are posted with increasing priorities (first message
// is sent with lowest priority, last message with highest),
// so it is *very likely* that they will be received by server
// in the reversed order, but this cannot be guaranteed as there is
// no relation between the speed of posting and the speed
// of transmission.
int numOfMessages = 10;
bool[] gotMessages = new bool[numOfMessages];
int sizeOfBigString = 1000000;
StringBuilder bigStringBld = new StringBuilder(sizeOfBigString);
for (int i = 0; i != sizeOfBigString; ++i)
{
bigStringBld.Append('x');
}
string bigString = bigStringBld.ToString();
serverAgent.RegisterObject(objectName,
delegate(object sender, IncomingMessageArgs args)
{
Parameters receivedContent = args.Message.Parameters;
int id = receivedContent.GetInteger("id");
// uncomment it to see how the messages get reordered
// verify the big value
//Console.WriteLine("received message {0}", id);
gotMessages[id] = true;
Assert.AreEqual(bigString,
receivedContent.GetString("big"));
args.Message.Reply(null);
});
Parameters content = new Parameters();
content.SetString("big", bigString);
OutgoingMessage[] messages =
new OutgoingMessage[numOfMessages];
// send all messages with different ids and priorities
for (int i = 0; i != numOfMessages; ++i)
{
int id = i;
int priority = i; // increasing priority
content.SetInteger("id", id);
messages[i] = clientAgent.Send(
serverAddress, objectName,
messageName, content, priority);
}
// wait for all messages to complete
for (int i = 0; i != numOfMessages; ++i)
{
messages[i].WaitForCompletion();
messages[i].Close();
}
for (int i = 0; i != numOfMessages; ++i)
{
Assert.IsTrue(gotMessages[i]);
}
}