void ConnectToScribbler(DateTime dt) { Uri driveUri = null; // Look for any active generic drive contract service running Arbiter.Activate(DssEnvironment.TaskQueue, Arbiter.Choice(DssEnvironment.DirectoryQuery(drive.Contract.Identifier), delegate(ServiceInfoType success) { driveUri = new Uri(success.Service); Console.WriteLine("================\n" + driveUri); // Initialize the port and subscribe to the service motorsOn = false; drivePort = DssEnvironment.ServiceForwarder <drive.DriveOperations>(driveUri); drive.DriveOperations driveNotificationPort = new drive.DriveOperations(); drivePort.Subscribe(driveNotificationPort); // Set up notifications Arbiter.Activate(DssEnvironment.TaskQueue, Arbiter.Receive <drive.Update>(true, driveNotificationPort, NotifyDriveUpdate) ); autolock.Set(); }, delegate(W3C.Soap.Fault failure) { // Request failed. Sleep for 1 sec and look for 30 times. if (++attemptCount >= 30) { DssEnvironment.LogError("Unable to find Drive Service, aborting - Press <Enter>"); Console.ReadLine(); DssEnvironment.Shutdown(); } else { // Post a timer message that expires in 30 seconds TimeSpan timeout = new TimeSpan(0, 0, 30); DssEnvironment.TaskQueue.EnqueueTimer(timeout, _timerPort); } } )); }
private T tryCreateAdapter() { lock (this) { try { // Query the service initially PortSet <LookupResponse, Fault> responsePort = new PortSet <LookupResponse, Fault>(); DssEnvironment.ServiceForwarderUnknownType(new Uri("dssp.tcp://localhost/" + Name)).PostUnknownType( new DsspDefaultLookup() { Body = new LookupRequestType(), ResponsePort = responsePort }); ServiceInfoType responseRecord = RSUtils.ReceiveSync(responsePort, Myro.Utilities.Params.DefaultRecieveTimeout); // Try to find a working contract for each adapter foreach (var factory in adapterFactories) { try { ServiceInfoType serviceRecord = RSUtils.FindCompatibleContract(responseRecord, factory.SupportedContracts); T ret = (T)factory.Create(serviceRecord); Console.WriteLine("Attached to " + serviceRecord.Service + " as \"" + Name + "\""); return(ret); } catch (NoContractFoundException) { } } // If we haven't returned already in the loop, we didn't find // an adapter that works. throw new NoAdapterFoundException(responseRecord); } catch (NoAdapterFoundException) { throw; } catch (Exception e) { DssEnvironment.LogError("Error querying or attaching to " + "dssp.tcp://localhost/" + Name + ": " + e.ToString()); throw; } } }
/// <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)); })); }