protected void subscribeDirectory() { var dirPort = DssEnvironment.ServiceForwarder <dirProxy.DirectoryPort>(new Uri("http://localhost:50000/directory")); var resPort = new dirProxy.DirectoryPort(); Arbiter.ExecuteToCompletion(DssEnvironment.TaskQueue, Arbiter.Choice <SubscribeResponseType, Fault>( dirPort.Subscribe(resPort, null), delegate(SubscribeResponseType success) { Console.WriteLine("AdapterBank subscribed to directory service"); }, delegate(Fault failure) { throw new Exception("Could not subscribe to directory service: " + failure.Reason); })); Arbiter.Activate(DssEnvironment.TaskQueue, Arbiter.Receive <dirProxy.Insert>(true, resPort, directoryInsertHandler)); }
/// <summary> /// Helper method that waits for a service to start and runs a handler when it does. /// This subscribes to and queries the DSS directory. /// </summary> /// <param name="serviceInfo"></param> /// <param name="handler"></param> private static void waitForService(ServiceInfoType serviceInfo, Handler <ServiceInfoType> handler) { var dirPort = DssEnvironment.ServiceForwarder <directory.DirectoryPort>(new Uri("dssp.tcp://localhost/directory")); var notPort = new directory.DirectoryPort(); // Keep this flag to only run the handler once bool ranHandler = false; Object lockObj = new Object(); // Subscribe first, then query, to make sure we don't miss the service registering itself DssEnvironment.LogInfo("Subscribing to directory"); Arbiter.Activate(DssEnvironment.TaskQueue, Arbiter.Choice( dirPort.Subscribe( new directory.SubscribeRequest() { QueryRecordList = new List <ServiceInfoType>() { serviceInfo }, NotificationCount = 1 }, notPort, typeof(directory.Insert)), delegate(SubscribeResponseType r) { DssEnvironment.LogInfo("Subscribed to directory!"); // Run handler if the service starts and we get the subscription notification DssEnvironment.LogInfo("Activating subscription receive"); Arbiter.Activate(DssEnvironment.TaskQueue, Arbiter.Receive <directory.Insert>(false, notPort, delegate(directory.Insert ins) { DssEnvironment.LogInfo("Got subscription notification!"); lock (lockObj) { if (ranHandler == false) { new Thread(new ThreadStart(delegate() { handler.Invoke(ins.Body.Record); })).Start(); ranHandler = true; } } })); }, delegate(Fault f) { lock (lockObj) { if (ranHandler == false) { new Thread(new ThreadStart(delegate() { handler.Invoke(null); })).Start(); ranHandler = true; } } DssEnvironment.LogError("Fault received while subscribing to directory: " + Strings.FromFault(f)); })); // Query directory, run handler if the service is already started DssEnvironment.LogInfo("Querying directory"); Arbiter.Activate(DssEnvironment.TaskQueue, Arbiter.Choice( dirPort.Query(new directory.QueryRequest() { QueryRecord = serviceInfo }), delegate(directory.QueryResponse r) { DssEnvironment.LogInfo("Queried directory!"); lock (lockObj) { if (ranHandler == false && r.RecordList.Length > 0) { new Thread(new ThreadStart(delegate() { handler.Invoke(r.RecordList[0]); })).Start(); ranHandler = true; } } }, delegate(Fault f) { //lock (lockObj) //{ // if (ranHandler == false) // { // new Thread(new ThreadStart(delegate() { handler.Invoke(null); })).Start(); // ranHandler = true; // } //} DssEnvironment.LogError("Fault received while querying directory: " + Strings.FromFault(f)); })); }
/// <summary> /// Helper method that waits for a service to start and runs a handler when it does. /// This subscribes to and queries the DSS directory. /// </summary> /// <param name="serviceInfo"></param> /// <param name="handler"></param> private static void waitForService(ServiceInfoType serviceInfo, Handler<ServiceInfoType> handler) { var dirPort = DssEnvironment.ServiceForwarder<directory.DirectoryPort>(new Uri("dssp.tcp://localhost/directory")); var notPort = new directory.DirectoryPort(); // Keep this flag to only run the handler once bool ranHandler = false; Object lockObj = new Object(); // Subscribe first, then query, to make sure we don't miss the service registering itself DssEnvironment.LogInfo("Subscribing to directory"); Arbiter.Activate(DssEnvironment.TaskQueue, Arbiter.Choice( dirPort.Subscribe( new directory.SubscribeRequest() { QueryRecordList = new List<ServiceInfoType>() { serviceInfo }, NotificationCount = 1 }, notPort, typeof(directory.Insert)), delegate(SubscribeResponseType r) { DssEnvironment.LogInfo("Subscribed to directory!"); // Run handler if the service starts and we get the subscription notification DssEnvironment.LogInfo("Activating subscription receive"); Arbiter.Activate(DssEnvironment.TaskQueue, Arbiter.Receive<directory.Insert>(false, notPort, delegate(directory.Insert ins) { DssEnvironment.LogInfo("Got subscription notification!"); lock (lockObj) { if (ranHandler == false) { new Thread(new ThreadStart(delegate() { handler.Invoke(ins.Body.Record); })).Start(); ranHandler = true; } } })); }, delegate(Fault f) { lock (lockObj) { if (ranHandler == false) { new Thread(new ThreadStart(delegate() { handler.Invoke(null); })).Start(); ranHandler = true; } } DssEnvironment.LogError("Fault received while subscribing to directory: " + Strings.FromFault(f)); })); // Query directory, run handler if the service is already started DssEnvironment.LogInfo("Querying directory"); Arbiter.Activate(DssEnvironment.TaskQueue, Arbiter.Choice( dirPort.Query(new directory.QueryRequest() { QueryRecord = serviceInfo }), delegate(directory.QueryResponse r) { DssEnvironment.LogInfo("Queried directory!"); lock (lockObj) { if (ranHandler == false && r.RecordList.Length > 0) { new Thread(new ThreadStart(delegate() { handler.Invoke(r.RecordList[0]); })).Start(); ranHandler = true; } } }, delegate(Fault f) { //lock (lockObj) //{ // if (ranHandler == false) // { // new Thread(new ThreadStart(delegate() { handler.Invoke(null); })).Start(); // ranHandler = true; // } //} DssEnvironment.LogError("Fault received while querying directory: " + Strings.FromFault(f)); })); }