private void Receiving()
{
/*
* the first command the EndpointManager receives.
* instructs the EndpointManager to fire off its "Listens" command, which starts
* up all inbound transports and binds them to specific addresses via configuration.
* those results will then be piped back to Remoting, who waits for the results of
* listen.AddressPromise.
* */
Receive<Listen>(listen =>
{
Listens.ContinueWith<INoSerializationVerificationNeeded>(listens =>
{
if (listens.IsFaulted)
{
return new ListensFailure(listen.AddressesPromise, listens.Exception);
}
else
{
return new ListensResult(listen.AddressesPromise, listens.Result);
}
}, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default)
.PipeTo(Self);
});
Receive<ListensResult>(listens =>
{
_transportMapping = (from mapping in listens.Results
group mapping by mapping.Item1.Address
into g
select new { address = g.Key, transports = g.ToList() }).Select(x =>
{
if (x.transports.Count > 1)
{
throw new RemoteTransportException(
string.Format("There are more than one transports listening on local address {0}",
x.address));
}
return new KeyValuePair<Address, AkkaProtocolTransport>(x.address,
x.transports.Head().Item1.ProtocolTransport);
}).ToDictionary(x => x.Key, v => v.Value);
//Register a listener to each transport and collect mapping to addresses
var transportsAndAddresses = listens.Results.Select(x =>
{
x.Item2.SetResult(new ActorAssociationEventListener(Self));
return x.Item1;
}).ToList();
listens.AddressesPromise.SetResult(transportsAndAddresses);
});
Receive<ListensFailure>(failure => failure.AddressesPromise.SetException(failure.Cause));
// defer the inbound association until we can enter "Accepting" behavior
Receive<InboundAssociation>(
ia => Context.System.Scheduler.ScheduleTellOnce(TimeSpan.FromMilliseconds(10), Self, ia, Self));
Receive<ManagementCommand>(mc => Sender.Tell(new ManagementCommandAck(status: false)));
Receive<StartupFinished>(sf => Become(Accepting));
Receive<ShutdownAndFlush>(sf =>
{
Sender.Tell(true);
Context.Stop(Self);
});
}