/// <summary> /// return a participant instance to the manager /// </summary> /// <param name="address"></param> /// <param name="role"></param> /// <param name="type"></param> /// <param name="subType"></param> /// <returns></returns> public IClient getClientFor(string address, Role role, ECOMMS_Entity.Type type, SubType subType) { IClient client = null; switch (role) { case Role.Instrument: client = new SimInstrumentClient(address); //new InstrumentClient(address, type); break; } return(client); }
static void Main(string[] args) { #region create a manager Manager enterpriseManager = new Manager(); enterpriseManager.connect(); enterpriseManager.init(); #endregion #region create address service participant and client //start up an address service AddressServiceParticipant asp = new AddressServiceParticipant(); //connect returns false if the server is not running //may change this to allow the connection exception to leak //into the client code //this will necessarily change when we move to cluster servers //or provide server redundancy asp.connect(@"nats://*****:*****@"nats://localhost:4222"); aspClient.init(); //blocking request to get address from service client //to be used to create instrument participant and client below var address = aspClient.request("get", "nextaddress"); #endregion #region create instrument participant and client //instrument participant example - shim SimInstrumentParticipant instrument = new SimInstrumentParticipant(address); instrument.connect(@"nats://*****:*****@"nats://localhost:4222"); client.init(); //observe the client client.addObserver(new ObserverAdapter((observable, hint) => { switch (hint) { case "STATUS_RECEIVED": Console.WriteLine("notfied through observer"); break; } })); #endregion //call through client with bogus get request //using new protocol request ( ack/nak/response ) //this will be wrapped by the doGet() method but is available //in the base class Entity client.protocolRequest("get", "fliplikewilson", new ResponseCompletionAdapter( () => { Console.WriteLine("ResponeCompletionAdapter ack"); }, (error) => { Console.WriteLine("ResponseCompletionAdapter [get][fliplikewilson] : DANGER WILL ROBINSON!"); }, (bytes) => { } )); //call through client with valid get request //ADD NAMED PARAMETERS FOR MORE CLARITY... client.protocolRequest("get", "location", new ResponseCompletionAdapter( ack: () => { Console.WriteLine("ResponeCompletionAdapter ack"); }, nak: (error) => { Console.WriteLine("ResponseCompletionAdapter [get][location] : DANGER WILL ROBINSON!"); }, response: (bytes) => { Console.WriteLine("ResponseCompletionAdapter [get][location] : " + Encoding.UTF8.GetString(bytes, 0, bytes.Length)); } )); //call through client with valid get request //ADD NAMED PARAMETERS FOR MORE CLARITY... //ONLY PROVIDE RESPONSE CALLBACK client.protocolRequest("get", "location.now", new ResponseCompletionAdapter( response: (bytes) => { Console.WriteLine("ResponseCompletionAdapter [get][location.now] : " + Encoding.UTF8.GetString(bytes, 0, bytes.Length)); }, ack: () => { Console.WriteLine("ResponseCompletionAdapter [ack][location.now] : "); } )); //call through Client doGet method client.doGet("location", (s) => { Console.WriteLine("the instrument client location is " + s); }); //call demo address service get aspClient.doGet("nextaddress", (s) => { Console.WriteLine("address service has returned " + s); }); client.doGet("location", (s) => { Console.WriteLine("the instrument client location is " + s); }); client.doSet("location", "somewhere", (s) => { Console.WriteLine("location set was a " + s); }); client.doAction("start", (s) => { Console.WriteLine("sent START action to instrument and it was a " + s); Console.WriteLine("showing instrument client properties:"); Console.WriteLine("location: " + instrument.location); Console.WriteLine("started flag: " + instrument.started); }); //through registered facility - action //client.doAction("control.autosampler.openlid", (s) => //{ //}); //through registered facility - IResponseCompletionAdapter client.doAction("control.autosampler.openlid", new ResponseCompletionAdapter( ack: () => Console.WriteLine("control.autosampler.openlid got ACK"), response: (bytes) => Console.WriteLine("control.autosampler.openlid got RESPONSE " + Encoding.UTF8.GetString(bytes, 0, bytes.Length)) )); //through normal path with completion adapter client.doAction("openlid", new ResponseCompletionAdapter( ack: () => Console.WriteLine("control.autosampler.openlid got ACK"), response: (bytes) => Console.WriteLine("control.autosampler.openlid got RESPONSE " + Encoding.UTF8.GetString(bytes, 0, bytes.Length)) )); Thread.Sleep(1000); client.doAction("end", (s) => { Console.WriteLine("sent END action to instrument and it was a " + s); Console.WriteLine("showing instrument client properties:"); Console.WriteLine("location: " + instrument.location); Console.WriteLine("started flag: " + instrument.started); }); Thread.Sleep(5000); Console.WriteLine(enterpriseManager.clients.Count); Console.WriteLine( "there are " + enterpriseManager.clients.Where((i) => i.role == Role.Instrument).Count() + " instruments online" ); //observe the enterprise manager enterpriseManager.addObserver(new ObserverAdapter((o, h) => { //need to wait to notify until after base class has gotton responses //or have library query first before creating client Thread.Sleep(1000); switch (h) { case "CLIENTS_CHANGED": Console.WriteLine( "there are " + enterpriseManager.clients.Where((i) => i.role == Role.Instrument).Count() + " instruments online" ); break; } })); Console.WriteLine("START ANOTHER CONSOLE APP..."); Console.ReadKey(); }