Skip to content

Instantly share code, notes, and snippets.

@mikewiegand
Last active August 29, 2015 14:02
Show Gist options
  • Save mikewiegand/8197bdd193984f24f03f to your computer and use it in GitHub Desktop.
Save mikewiegand/8197bdd193984f24f03f to your computer and use it in GitHub Desktop.
Grain callback POC
using Callback.GrainInterfaces;
using Orleans;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Callback.GrainCollection
{
[Reentrant]
public class Grain1 : Orleans.GrainBase, IGrain1
{
private string _name = "Some sort of name";
private List<IGrain2> _subscribers = new List<IGrain2>();
public Task<string> Name
{
get
{
return Task.FromResult(_name);
}
}
public Task Subscribe(IGrain2 igrain2)
{
_subscribers.Add(igrain2);
return TaskDone.Done;
}
public async Task NotifyAll()
{
var tasks = new List<Task>(_subscribers.Count);
foreach (var subscriber in _subscribers)
{
tasks.Add(subscriber.Cb(Grain1Factory.Cast(this.AsReference())));
}
await Task.WhenAll(tasks);
}
}
}
using Callback.GrainInterfaces;
using Orleans;
using System.Threading.Tasks;
namespace Callback.GrainCollection
{
public class Grain2 : Orleans.GrainBase, IGrain2
{
public async Task Cb(IGrain1 grain1)
{
var name = await grain1.Name; // Gets here, but goes into oblivion.
System.Console.Write("****************" + name);
var x = name;
}
}
}
using System.Threading.Tasks;
namespace Callback.GrainInterfaces
{
public interface IGrain1 : Orleans.IGrain
{
Task<string> Name { get; }
Task Subscribe(IGrain2 igrain2);
Task NotifyAll();
}
}
using System.Threading.Tasks;
namespace Callback.GrainInterfaces
{
public interface IGrain2 : Orleans.IGrain
{
Task Cb(IGrain1 grain1);
}
}
using System;
namespace Callback.SiloHost
{
/// <summary>
/// Orleans test silo host
/// </summary>
public class Program
{
static void Main(string[] args)
{
// The Orleans silo environment is initialized in its own app domain in order to more
// closely emulate the distributed situation, when the client and the server cannot
// pass data via shared memory.
AppDomain hostDomain = AppDomain.CreateDomain("OrleansHost", null, new AppDomainSetup
{
AppDomainInitializer = InitSilo,
AppDomainInitializerArguments = args,
});
Orleans.OrleansClient.Initialize("DevTestClientConfiguration.xml");
var grain1 = GrainInterfaces.Grain1Factory.GetGrain(0);
var grain2 = GrainInterfaces.Grain2Factory.GetGrain(0);
grain1.Subscribe(grain2).Wait();
grain1.NotifyAll().Wait();
Console.WriteLine("Orleans Silo is running.\nPress Enter to terminate...");
Console.ReadLine();
hostDomain.DoCallBack(ShutdownSilo);
}
static void InitSilo(string[] args)
{
hostWrapper = new OrleansHostWrapper(args);
if (!hostWrapper.Run())
{
Console.Error.WriteLine("Failed to initialize Orleans silo");
}
}
static void ShutdownSilo()
{
if (hostWrapper != null)
{
hostWrapper.Dispose();
GC.SuppressFinalize(hostWrapper);
}
}
private static OrleansHostWrapper hostWrapper;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment