singrdk/base/Applications/NameSpace/NakService/NakService.sg

196 lines
6.6 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.
//
2008-11-17 18:29:00 -05:00
// Note: Used for testing the namespace service.
2008-03-05 09:52:00 -05:00
// register /dev/NakConnect in the namespace.
// will always Nak any Connect requests.
//
2008-11-17 18:29:00 -05:00
2008-03-05 09:52:00 -05:00
using System;
using System.Runtime.CompilerServices;
//using System.Threading;
using Microsoft.Singularity;
using Microsoft.Singularity.Directory;
using Microsoft.Singularity.V1.Services;
using Microsoft.Singularity.Channels;
using Microsoft.Singularity.Security;
using Microsoft.SingSharp;
using Microsoft.SingSharp.Runtime;
[assembly: ApplicationPublisherAttribute("singularity.microsoft.com")]
[assembly: AssertPrivilegeAttribute("$register-privilege.localhost")]
namespace Microsoft.Singularity.Applications
{
public static class NakConnect
{
2008-11-17 18:29:00 -05:00
static string hang = "/service/channelhang";
static string nakConn = "/service/nakconnect";
static string nakRunning = "/service/nakrunning";
static string closed = "/service/channelclosed";
2008-03-05 09:52:00 -05:00
private static bool Register(DirectoryServiceContract.Imp! ds,
string! name,
[Claims]ServiceProviderContract.Imp! imp)
{
// acquire namespace endpoint
ErrorCode errorOut;
ServiceProviderContract.Imp:Start service;
bool ok = SdsUtils.Register(name, ds, imp, out errorOut);
2008-11-17 18:29:00 -05:00
if (!ok) {
2008-03-05 09:52:00 -05:00
Console.WriteLine(" Registration of {0} failed. Reason: {1}",
name,
SdsUtils.ErrorCodeToString(errorOut)
);
}
return ok;
}
private static bool DoDeregister(DirectoryServiceContract.Imp! ds, string! path)
{
ErrorCode errorOut;
ServiceProviderContract.Imp:Start service;
bool ok = SdsUtils.Deregister(path, ds, out service, out errorOut);
if (!ok) DebugStub.Break();
delete service;
return ok;
}
static void Finalize()
{
Console.WriteLine("NakService Finalizer called.");
DebugStub.Break();
DirectoryServiceContract.Imp ds = DirectoryService.NewClientEndpoint();
try {
DoDeregister(ds, hang);
DoDeregister(ds, nakConn);
DoDeregister(ds, closed);
}
finally {
delete ds;
}
}
static void CleanUp()
{
Console.WriteLine("NakService Finalizer called.");
DirectoryServiceContract.Imp ds = DirectoryService.NewClientEndpoint();
try {
DoDeregister(ds, hang);
DoDeregister(ds, nakConn);
DoDeregister(ds, closed);
}
finally {
delete ds;
}
}
public static int Main(String[]! args)
{
DirectoryServiceContract.Imp ds = DirectoryService.NewClientEndpoint();
2008-11-17 18:29:00 -05:00
// register "/service/nakconnect" in NS
2008-03-05 09:52:00 -05:00
ServiceProviderContract.Imp! imp;
ServiceProviderContract.Exp! s;
ServiceProviderContract.NewChannel(out imp, out s);
bool ok = NakConnect.Register(ds, nakConn, imp);
if (!ok) {
delete s;
delete ds;
return -1;
}
2008-11-17 18:29:00 -05:00
// register "/service/channelhang" in NS
2008-03-05 09:52:00 -05:00
// we keep the exp alive but we never listen
ServiceProviderContract.Imp! imp3;
ServiceProviderContract.Exp! s3;
ServiceProviderContract.NewChannel(out imp3, out s3);
ok = NakConnect.Register(ds, hang, imp3);
if (!ok) {
delete s;
delete s3;
delete ds;
return -1;
}
2008-11-17 18:29:00 -05:00
// register "/service/nakrunning" in NS
2008-03-05 09:52:00 -05:00
// we keep the exp alive but we never listen
ServiceProviderContract.Imp! imp4;
ServiceProviderContract.Exp! s4;
ServiceProviderContract.NewChannel(out imp4, out s4);
ok = NakConnect.Register(ds, nakRunning, imp4);
if (!ok) {
delete s;
delete s3;
delete ds;
delete s4;
return -1;
}
2008-11-17 18:29:00 -05:00
// register "/service/channelclosed" in NS
2008-03-05 09:52:00 -05:00
// we pass the imp on to the NS but close down the exp end
ServiceProviderContract.Imp! imp2;
ServiceProviderContract.Exp! s2;
ServiceProviderContract.NewChannel(out imp2, out s2);
ok = NakConnect.Register(ds, closed, imp2);
delete s2; // ensure channel closed on other end
if (!ok) {
delete s;
delete s3;
delete s4;
delete ds;
return -1;
}
// enter loop to receive Connect messages and always return NACK
try {
for (bool run = true; run;) {
switch receive {
// Listen for new connections
case s.Connect(candidate):
Console.WriteLine("Nak Service NACKing....");
s.SendNackConnect(candidate);
break;
case s4.Connect(candidate):
Console.WriteLine("Nak Service NACKing....");
s.SendAckConnect();
delete candidate;
break;
case s.ChannelClosed() :
Console.WriteLine("NakConnect channel closed");
run = false;
break;
case s4.ChannelClosed() : //IsRunning has closed
Console.WriteLine("NakRunning channel closed");
run = false;
break;
}
}
}
finally {
CleanUp();
}
Console.WriteLine("terminating NakService cleanly");
delete s;
delete s3;
delete s4;
delete ds;
SdsUtils.FlushCache();
return 0;
}
} // class NakConnect
}//namespace