singrdk/base/Services/Tests/Counter/CounterService.sg

99 lines
3.1 KiB
Plaintext

///////////////////////////////////////////////////////////////////////////////
//
// Microsoft Research Singularity
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// File: Service\Test\Counter\Counter.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.Counter
{
[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 CounterService : WorkerServiceExec
{
private Counter! counter;
internal CounterService([Claims]DirectoryServiceContract.Imp:Ready! dep)
: base(dep, CounterContract.ModuleName)
{
counter = new Counter();
}
protected override bool Accept(ServiceContract.Exp:Start! ep)
{
CounterContract.Exp:Start newEp = ep as CounterContract.Exp:Start;
return (newEp != null);
}
protected override IRunnable NewWorker([Claims]ServiceContract.Exp:Start! ep)
{
CounterContract.Exp:Start newEp;
newEp = ep as CounterContract.Exp:Start;
if (newEp == null) {
delete ep;
return null;
}
return new CounterWorker(newEp, counter);
}
internal static int AppMain(Parameters! config)
{
ManagedServiceContract.Exp:Start ep;
DirectoryServiceContract.Imp:Ready dep;
IRunnable counter;
IRunnable manageExec;
Thread thread;
if (config.serviceRef == null || config.directoryRef == null) {
// Wrong contract type!
return -1;
}
ep = config.serviceRef.Acquire();
dep = config.directoryRef.Acquire();
counter = new CounterService(dep);
manageExec = new ManagementExec(ep, counter);
thread = new Thread(new ThreadStart(manageExec.Run));
thread.Start();
thread.Join();
DebugStub.Print("Counter: Quit\n");
return 0;
}
}
}