singrdk/base/Services/NetStack/Channels.Private/DNSExpManager.sg

105 lines
3.7 KiB
Plaintext
Raw Permalink Normal View History

2008-03-05 09:52:00 -05:00
///////////////////////////////////////////////////////////////////////////////
//
// Microsoft Research Singularity
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Note: Provider-side helper for the IP Channel Contract
//
using System.Threading;
using System.Net.IP;
using Microsoft.SingSharp;
using Microsoft.Singularity;
using Microsoft.Singularity.Channels;
using Microsoft.Singularity.Directory;
using NetStack.Contracts;
using NetStack.Runtime;
using System.Collections;
using System;
namespace NetStack.Channels.Private
{
2008-11-17 18:29:00 -05:00
public class DNSExpManager : StoppableThread
2008-03-05 09:52:00 -05:00
{
2008-11-17 18:29:00 -05:00
override protected void Run(StopThreadContract.Exp:Ready! terminator)
2008-03-05 09:52:00 -05:00
{
// Here is the channel we use to communicate with
// the NameServer
ServiceProviderContract.Imp! nsImp;
ServiceProviderContract.Exp! nsExp;
ServiceProviderContract.NewChannel(out nsImp, out nsExp);
// Here is our NameServer connection over which we
// receive new client channels. When we become a
// process, this will be present automatically,
// somehow.
DirectoryServiceContract.Imp epNS = DirectoryService.NewClientEndpoint();
try {
epNS.SendRegister(Bitter.FromString2(DNSContract.ModuleName), nsImp);
switch receive {
case epNS.AckRegister() :
// All is well.
break;
case epNS.NakRegister(ServiceProviderContract.Imp:Start rejectedEP, error) :
// All is very much not well; abort.
DebugStub.Print("Failed to register the DNS module.\n");
delete rejectedEP;
delete nsExp;
return;
break;
}
}
finally {
delete epNS;
}
2008-11-17 18:29:00 -05:00
bool run = true;
while (run) {
2008-03-05 09:52:00 -05:00
switch receive {
case nsExp.Connect(ServiceContract.Exp:Start! newEp) :
{
// We expect people to give us DNSContract.Exp instances
DNSContract.Exp newDnsEp = newEp as DNSContract.Exp;
if (newDnsEp == null) {
// Invalid contract type. Fail.
nsExp.SendNackConnect(newEp);
}
else {
// Signal ready and start servicing this contract
nsExp.SendAckConnect();
newDnsEp.SendReady();
// Spin up a new servicing thread
DNSExpConnection newConn = new DNSExpConnection(newDnsEp);
newConn.Start();
}
}
break;
case nsExp.ChannelClosed():
// Exit this thread
2008-11-17 18:29:00 -05:00
run = false;
break;
case terminator.Terminate():
terminator.SendAckTerminate();
run = false;
break;
case terminator.ChannelClosed():
run = false;
break;
2008-03-05 09:52:00 -05:00
}
}
2008-11-17 18:29:00 -05:00
delete nsExp;
2008-03-05 09:52:00 -05:00
}
}
}