singrdk/base/Services/Tests/Replace/ReplaceServer.sg

140 lines
4.5 KiB
Plaintext

///////////////////////////////////////////////////////////////////////////////
//
// Microsoft Research Singularity
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// File: Service\Test\Replace\ReplaceServer.sg
//
// Note:
//
using System;
using System.Threading;
using Microsoft.SingSharp;
using Microsoft.SingSharp.Reflection;
using Microsoft.Singularity;
using Microsoft.Singularity.Channels;
using Microsoft.Singularity.Configuration;
using Microsoft.Singularity.Directory;
using Microsoft.Singularity.Resiliency;
using Microsoft.Singularity.Security;
using Microsoft.Singularity.ServiceManager;
using Microsoft.Singularity.Services;
[assembly: Transform(typeof(ServiceResourceTransform))]
[assembly: ApplicationPublisherAttribute("singularity.microsoft.com")]
[assembly: AssertPrivilegeAttribute("$register-privilege.localhost")]
namespace Microsoft.Singularity.Services.Replace
{
[Category("Service")]
internal sealed class Parameters
{
[Endpoint]
public readonly TRef<ManagedServiceContract.Exp:Start> serviceRef;
[Endpoint]
public readonly TRef<DirectoryServiceContract.Imp:Ready> directoryRef;
[Endpoint]
public readonly TRef<ServiceProxyContract.Imp:Start> proxyRef;
reflective private Parameters();
}
internal sealed class ReplaceServer : WorkerServiceExec
{
private GameTable table;
private TRef<ServiceProxyContract.Imp:Start>! proxyRef;
internal ReplaceServer([Claims]DirectoryServiceContract.Imp:Ready! dep,
[Claims]ServiceProxyContract.Imp:Start! sep)
: base(dep, GamePlayerContract.ModuleName)
{
proxyRef = new TRef<ServiceProxyContract.Imp:Start>(sep);
}
public override void Run()
{
ServiceProviderContract.Exp! provider;
ThreadTerminationContract.Exp:Start! signal;
if (signalRef == null) {
throw new Exception("Signal is empty.");
}
DebugStub.Print("-- Replace Game Server Start!\n");
if (!RegisterName(out provider)) {
delete provider;
return;
}
DebugStub.Print("-- name registered\n");
table = new GameTable(proxyRef.Acquire());
table.Initialize();
signal = signalRef.Acquire();
while (Listen(provider, signal));
signalRef.Release(signal);
DeregisterName(provider);
DebugStub.Print("-- Final Score --\n");
table.Print();
}
protected override bool Accept(ServiceContract.Exp:Start! ep)
{
GamePlayerContract.Exp:Start newEp = ep as GamePlayerContract.Exp:Start;
return (newEp != null);
}
protected override IRunnable NewWorker([Claims]ServiceContract.Exp:Start! ep)
{
GamePlayerContract.Exp:Start newEp;
newEp = ep as GamePlayerContract.Exp:Start;
if (newEp == null) {
delete ep;
return null;
}
if (table == null) {
delete newEp;
return null;
}
return new ReplaceWorker(newEp, table);
}
internal static int AppMain(Parameters! config)
{
ManagedServiceContract.Exp:Start mep;
DirectoryServiceContract.Imp:Ready dep;
ServiceProxyContract.Imp:Start sep;
IRunnable service;
IRunnable manageExec;
Thread thread;
GameTable table;
if (config.serviceRef == null ||
config.directoryRef == null ||
config.proxyRef == null)
{
return -1;
}
mep = config.serviceRef.Acquire();
dep = config.directoryRef.Acquire();
sep = config.proxyRef.Acquire();
DebugStub.Print("-- server process enter\n");
service = new ReplaceServer(dep, sep);
manageExec = new ManagementExec(mep, service);
thread = new Thread(new ThreadStart(manageExec.Run));
thread.Start();
thread.Join();
return 0;
}
}
}