本文整理汇总了C#中Messenger.GetMessage方法的典型用法代码示例。如果您正苦于以下问题:C# Messenger.GetMessage方法的具体用法?C# Messenger.GetMessage怎么用?C# Messenger.GetMessage使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Messenger
的用法示例。
在下文中一共展示了Messenger.GetMessage方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RunAcceptor
private static void RunAcceptor(string uri, string acceptorName)
{
var messenger = new Messenger(uri);
var promisedToAcceptNoEarlierThan = 0;
var lastAccepted = -1;
var lastAcceptedValue = "";
while (true)
{
var currMessage = messenger.GetMessage<AcceptorReceived>();
if (currMessage.TimePeriod <= lastAccepted) continue;
switch (currMessage.Type)
{
case "prepare":
if (currMessage.TimePeriod > promisedToAcceptNoEarlierThan)
promisedToAcceptNoEarlierThan = currMessage.TimePeriod;
messenger.PostMessage(new Promised
{
By = acceptorName,
TimePeriod = currMessage.TimePeriod,
HaveAccepted = lastAccepted > 0,
LastAcceptedTimePeriod = lastAccepted,
LastAcceptedValue = lastAcceptedValue
});
break;
case "proposed":
if (currMessage.TimePeriod < promisedToAcceptNoEarlierThan)
continue;
lastAccepted = currMessage.TimePeriod;
lastAcceptedValue = currMessage.Value;
messenger.PostMessage(new Accepted
{
By = acceptorName,
TimePeriod = currMessage.TimePeriod,
Value = currMessage.Value,
});
break;
}
}
}
示例2: RunNode
private static void RunNode(string uri)
{
var messenger = new Messenger(uri);
while (true)
{
// Receive messages like this
var currMessage = messenger.GetMessage<Accepted>();
Console.WriteLine("Received 'accepted' message by {0} for value '{1}' in time period {2}", currMessage.By, currMessage.Value, currMessage.TimePeriod);
// Send messages back like this
messenger.PostMessage(new Promised
{
By = "foo",
TimePeriod = currMessage.TimePeriod,
});
}
}
示例3: RunLearner
private static void RunLearner(string uri)
{
var messenger = new Messenger(uri);
var received = new List<Accepted>();
while (true)
{
var currMessage = messenger.GetMessage<Accepted>();
var matchingMessage = received.FirstOrDefault(
prevMessage => currMessage.TimePeriod == prevMessage.TimePeriod && currMessage.By != prevMessage.By);
received.Add(currMessage);
if (matchingMessage == null) continue;
Console.WriteLine("Learned value '{0}' in time period {1}", currMessage.Value, currMessage.TimePeriod);
}
}
示例4: RunProposer
private static void RunProposer(string uri, string freeValue)
{
var messenger = new Messenger(uri);
var received = new List<Promised>();
var latestProposedTimePeriod = 0;
while (true)
{
var currMessage = messenger.GetMessage<Promised>();
if (currMessage.TimePeriod <= latestProposedTimePeriod) continue;
var matchingMessage =
received.FirstOrDefault(
prevMessage =>
currMessage.TimePeriod == prevMessage.TimePeriod && currMessage.By != prevMessage.By);
received.Add(currMessage);
if (matchingMessage == null) continue;
var value = freeValue;
if (currMessage.HaveAccepted)
{
value = currMessage.LastAcceptedValue;
}
if (matchingMessage.HaveAccepted && (!currMessage.HaveAccepted
||
matchingMessage.LastAcceptedTimePeriod >
currMessage.LastAcceptedTimePeriod))
value = matchingMessage.LastAcceptedValue;
messenger.PostMessage(new Proposed
{
TimePeriod = currMessage.TimePeriod,
Value = value,
});
Console.WriteLine("Proposed value '{0}' in time period {1}", value, currMessage.TimePeriod);
latestProposedTimePeriod = currMessage.TimePeriod;
}
}