private void initPublisher(String pub, int strength) { mgr = new DDSEntityManager(); String partitionName = "Ownership example"; // create Domain Participant mgr.createParticipant(partitionName); // create Type StockTypeSupport stkTS = new StockTypeSupport(); mgr.registerType(stkTS); // create Topic mgr.createTopic("StockTrackerExclusive", "Ownership"); // create Publisher mgr.createPublisher(); // create DataWriter mgr.createWriter(); dwriter= mgr.getWriter(); OwnershipDataWriter = dwriter as StockDataWriter; msft = new Stock(); msft.ticker = "MSFT"; msft.publisher = pub; msft.strength = strength; msftHandle = OwnershipDataWriter.RegisterInstance(msft); }
static void Main(string[] args) { DDSEntityManager mgr = new DDSEntityManager("HelloWorld"); String partitionName = "HelloWorld example"; // create Domain Participant mgr.createParticipant(partitionName); // create Type MsgTypeSupport msgTS = new MsgTypeSupport(); mgr.registerType(msgTS); // create Topic mgr.createTopic("HelloWorldData_Msg"); // create Subscriber mgr.createSubscriber(); // create DataReader mgr.createReader(false); IDataReader dreader = mgr.getReader(); MsgDataReader HelloWorldDataReader = dreader as MsgDataReader; Msg[] msgSeq = null; DDS.SampleInfo[] infoSeq = null; Boolean terminate = false; ReturnCode status; Console.WriteLine("=== [Subscriber] Ready ..."); int count = 0; while (!terminate && count < 1500) { status = HelloWorldDataReader.Take(ref msgSeq, ref infoSeq, Length.Unlimited, SampleStateKind.Any, ViewStateKind.Any, InstanceStateKind.Any); ErrorHandler.checkStatus(status, "DataReader.Take"); for (int i = 0; i < msgSeq.Length; i++) { if (infoSeq[i].ValidData) { Console.WriteLine("=== [Subscriber] message received :"); Console.WriteLine(" userID : {0}", msgSeq[i].userID); Console.WriteLine(" Message : \"" + msgSeq[i].message + "\""); terminate = true; } } status = HelloWorldDataReader.ReturnLoan(ref msgSeq, ref infoSeq); ErrorHandler.checkStatus(status, "DataReader.ReturnLoan"); Thread.Sleep(200); ++count; } Thread.Sleep(2); // clean up mgr.getSubscriber().DeleteDataReader(HelloWorldDataReader); mgr.deleteSubscriber(); mgr.deleteTopic(); mgr.deleteParticipant(); }
static void Main(string[] args) { DDSEntityManager mgr = new DDSEntityManager("Listener"); ReturnCode status = ReturnCode.Error; ListenerDataListener myListener; String partitionName = "Listener Example"; int count = 0; Duration wait_timeout = new Duration(0, 200000000); // create Domain Participant mgr.createParticipant(partitionName); // create Type MsgTypeSupport msgTS = new MsgTypeSupport(); mgr.registerType(msgTS); // create Topic mgr.createTopic("ListenerData_Msg"); // create Subscriber mgr.createSubscriber(); // create DataReader mgr.createReader(false); IDataReader dreader = mgr.getReader(); myListener = new ListenerDataListener(); myListener.MsgDR = dreader as MsgDataReader; Console.WriteLine("=== [ListenerDataSubscriber] SetListener"); StatusKind kind = StatusKind.DataAvailable | StatusKind.RequestedDeadlineMissed; status = myListener.MsgDR.SetListener(myListener, kind); ErrorHandler.checkStatus(status, "DataReader.SetListener"); Console.WriteLine("=== [ListenerDataSubscriber] Ready..."); myListener.terminated = false; WaitSet ws = new WaitSet(); ws.AttachCondition(myListener.guardCond); ICondition[] cond = null; while (!myListener.terminated && count < 1500) { Console.WriteLine("=== [SubscriberUsingListener] waiting waitset ..."); ws.Wait(ref cond, wait_timeout); myListener.guardCond.SetTriggerValue(false); ++count; } Console.WriteLine("===[ListenerDataSubscriber] Market Closed."); mgr.getSubscriber().DeleteDataReader(myListener.MsgDR); mgr.deleteSubscriber(); mgr.deleteTopic(); mgr.deleteParticipant(); }
static void Main(string[] args) { DDSEntityManager mgr = new DDSEntityManager(); String partitionName = "HelloWorld example"; // create Domain Participant mgr.createParticipant(partitionName); mgr.setAutoDispose(true); // create Type MsgTypeSupport msgTS = new MsgTypeSupport(); mgr.registerType(msgTS); // create Topic mgr.createTopic("HelloWorldData_Msg", "HelloWorld"); // create Publisher mgr.createPublisher(); // create DataWriter mgr.createWriter(); // Publish Events IDataWriter dwriter = mgr.getWriter(); MsgDataWriter helloWorldWriter = dwriter as MsgDataWriter; Msg msgInstance = new Msg(); msgInstance.userID = 1; msgInstance.message = "Hello World"; InstanceHandle handle = helloWorldWriter.RegisterInstance(msgInstance); ErrorHandler.checkHandle(handle, "MsgDataWriter.RegisterInstance"); Console.WriteLine("=== [Publisher] writing a message containing :"); Console.WriteLine(" userID : {0}", msgInstance.userID); Console.WriteLine(" Message : \" {0} \"", msgInstance.message); ReturnCode status = helloWorldWriter.Write(msgInstance, handle); ErrorHandler.checkStatus(status, "MsgDataWriter.Write"); try { Thread.Sleep(2); } catch (ArgumentOutOfRangeException ex) { Console.WriteLine(ex.ToString()); Console.WriteLine(ex.StackTrace); } status = helloWorldWriter.UnregisterInstance(msgInstance, handle); // Clean up mgr.getPublisher().DeleteDataWriter(helloWorldWriter); mgr.deletePublisher(); mgr.deleteTopic(); mgr.deleteParticipant(); }
static void Main(string[] args) { DDSEntityManager mgr = new DDSEntityManager("WaitSet"); String partitionName = "WaitSet example"; // create Domain Participant mgr.createParticipant(partitionName); // create Type MsgTypeSupport msgTS = new MsgTypeSupport(); mgr.registerType(msgTS); // create Topic mgr.createTopic("WaitSetData_Msg"); // create Publisher mgr.createPublisher(); // create DataWriter mgr.createWriter(); // Publish Events IDataWriter dwriter = mgr.getWriter(); MsgDataWriter WaitSetWriter = dwriter as MsgDataWriter; // Write the first message Msg msgInstance = new Msg(); msgInstance.userID = 1; msgInstance.message = "First hello"; Console.WriteLine("=== [Publisher] writing a message containing :"); Console.WriteLine(" userID : {0}", msgInstance.userID); Console.WriteLine(" Message : \" {0} ", msgInstance.message); ReturnCode status = WaitSetWriter.Write(msgInstance, InstanceHandle.Nil); ErrorHandler.checkStatus(status, "MsgDataWriter.Write"); Thread.Sleep(500); // Write another message msgInstance.message = "Hello again"; status = WaitSetWriter.Write(msgInstance, InstanceHandle.Nil); ErrorHandler.checkStatus(status, "MsgDataWriter.Write"); Console.WriteLine("=== [Publisher] writing a message containing :"); Console.WriteLine(" userID : {0}", msgInstance.userID); Console.WriteLine(" Message : {0}", msgInstance.message); Thread.Sleep(500); // Clean up mgr.getPublisher().DeleteDataWriter(WaitSetWriter); mgr.deletePublisher(); mgr.deleteTopic(); mgr.deleteParticipant(); }
static void Main(string[] args) { DDSEntityManager mgr = new DDSEntityManager("Listener"); String partitionName = "Listener Example"; // create Domain Participant mgr.createParticipant(partitionName); // create Type MsgTypeSupport msgTS = new MsgTypeSupport(); mgr.registerType(msgTS); // create Topic mgr.createTopic("ListenerData_Msg"); // create Publisher mgr.createPublisher(); // create DataWriter mgr.createWriter(); // Publish Events IDataWriter dwriter = mgr.getWriter(); MsgDataWriter listenerWriter = dwriter as MsgDataWriter; ReturnCode status = ReturnCode.Error; Msg msgInstance = new Msg(); msgInstance.userID = 1; msgInstance.message = "Hello World"; Console.WriteLine("=== [ListenerDataPublisher] writing a message containing :"); Console.WriteLine(" userID : {0}", msgInstance.userID); Console.WriteLine(" Message : \"" + msgInstance.message + "\""); InstanceHandle msgHandle = listenerWriter.RegisterInstance(msgInstance); ErrorHandler.checkHandle(msgHandle, "DataWriter.RegisterInstance"); status = listenerWriter.Write(msgInstance, InstanceHandle.Nil); ErrorHandler.checkStatus(status, "DataWriter.Write"); Thread.Sleep(2); // clean up status = listenerWriter.Dispose(msgInstance, msgHandle); ErrorHandler.checkStatus(status, "DataWriter.Dispose"); status = listenerWriter.UnregisterInstance(msgInstance, msgHandle); ErrorHandler.checkStatus(status, "DataWriter.UnregisterInstance"); mgr.getPublisher().DeleteDataWriter(listenerWriter); mgr.deletePublisher(); mgr.deleteTopic(); mgr.deleteParticipant(); }
static void Main(string[] args) { DDSEntityManager mgr = new DDSEntityManager("WaitSet"); String partitionName = "WaitSet example"; // create Domain Participant mgr.createParticipant(partitionName); // create Type MsgTypeSupport msgTS = new MsgTypeSupport(); mgr.registerType(msgTS); // create Topic mgr.createTopic("WaitSetData_Msg"); // create Subscriber mgr.createSubscriber(); // create DataReader mgr.createReader(false); // Read Events IDataReader dreader = mgr.getReader(); MsgDataReader WaitSetReader = dreader as MsgDataReader; // Create a WaitSet WaitSet w = new WaitSet(); // Create a ReadCondition IReadCondition readCond = WaitSetReader.CreateReadCondition(SampleStateKind.NotRead, ViewStateKind.New, InstanceStateKind.Alive); ErrorHandler.checkHandle(readCond, "DataReader.CreateReadCondition"); // Create a QueryCondition String[] queryStr = { "Hello again" }; Console.WriteLine("=== [WaitSetDataSubscriber] Query : message = \"Hello again"); IQueryCondition queryCond = WaitSetReader.CreateQueryCondition("message=%0", queryStr); ErrorHandler.checkHandle(queryCond, "DataReader.CreateQueryCondition"); // Obtain a StatusCondition IStatusCondition statusCond; statusCond = dreader.StatusCondition; statusCond.SetEnabledStatuses(StatusKind.LivelinessChanged); ErrorHandler.checkHandle(statusCond, "DataReader.StatusCondition"); GuardCondition escape; escape = new GuardCondition(); ReturnCode result; result = w.AttachCondition(statusCond); ErrorHandler.checkStatus(result, "WaitSet.AttachCondition (status)"); result = w.AttachCondition(readCond); ErrorHandler.checkStatus(result, "WaitSet.AttachCondition (read)"); result = w.AttachCondition(queryCond); ErrorHandler.checkStatus(result, "WaitSet.AttachCondition (query)"); result = w.AttachCondition(escape); ErrorHandler.checkStatus(result, "WaitSet.AttachCondition (guard)"); /* Initialize and pre-allocate the GuardList used to obtain the triggered Conditions. */ ICondition[] guardList = new ICondition[4]; DDS.SampleInfo[] infoSeq = null; Msg[] msgSeq = null; bool escaped = false; bool writerLeft = false; int prevCount = 0; int count = 0; Console.WriteLine("=== [WaitSetDataSubscriber] Ready ..."); while (!escaped && count < 20 ) { /** * Wait until data will be available */ Duration wait_timeout = new Duration(Duration.InfiniteSec, Duration.InfiniteNanoSec); result = w.Wait(ref guardList, wait_timeout); ErrorHandler.checkStatus(result, "WaitSet.Wait"); /* Walk over all guards to display information */ foreach (ICondition guard in guardList) { if (guard == readCond) { result = WaitSetReader.ReadWithCondition(ref msgSeq, ref infoSeq, Length.Unlimited, readCond); ErrorHandler.checkStatus(result, "WaitSetReader.ReadWithCondition"); foreach (Msg msg in msgSeq) { Console.WriteLine(" --- New message received --- "); Console.WriteLine(" userID: {0}", msg.userID); Console.WriteLine(" Message : \"{0}", msg.message); } result = WaitSetReader.ReturnLoan(ref msgSeq, ref infoSeq); ErrorHandler.checkStatus(result, "WaitSet.ReturnLoan"); } else if (guard == queryCond) { result = WaitSetReader.TakeWithCondition(ref msgSeq, ref infoSeq, Length.Unlimited, queryCond); ErrorHandler.checkStatus(result, "WaitSetReader.TakeWithCondition"); foreach (Msg msg in msgSeq) { Console.WriteLine(" --- New message received with QueryCondition --- "); Console.WriteLine(" userID: {0}", msg.userID); Console.WriteLine(" Message : \" {0}", msg.message); } result = WaitSetReader.ReturnLoan(ref msgSeq, ref infoSeq); ErrorHandler.checkStatus(result, "WaitSet.ReturnLoan"); } else if (guard == statusCond) { LivelinessChangedStatus livelinessStatus = new LivelinessChangedStatus(); result = WaitSetReader.GetLivelinessChangedStatus(ref livelinessStatus); ErrorHandler.checkStatus(result, "DataReader.getLivelinessChangedStatus"); if (livelinessStatus.AliveCount < prevCount) { Console.WriteLine("!!! A WaitSetDataWriter lost its liveliness"); writerLeft = true; Console.WriteLine("Triggering escape condition."); ReturnCode status = escape.SetTriggerValue(true); } else { Console.WriteLine("!!! A WaitSetDataWriter joined"); } prevCount = livelinessStatus.AliveCount; } else if (guard == escape) { Console.WriteLine("WaitSetSubscriber has terminated."); escaped = true; ReturnCode status = escape.SetTriggerValue(false); } else { // Unknown Condition } } ++count; } // Detach all Conditions from the WaitSet result = w.DetachCondition(escape); ErrorHandler.checkStatus(result, "WaitSet.DetachCondition (escape)"); result = w.DetachCondition(readCond); ErrorHandler.checkStatus(result, "WaitSet.DetachCondition (readCond)"); result = w.DetachCondition(queryCond); ErrorHandler.checkStatus(result, "WaitSet.DetachCondition (queryCond)"); result = w.DetachCondition(statusCond); ErrorHandler.checkStatus(result, "WaitSet.DetachCondition (statusCond)"); Console.WriteLine("=== [Subscriber] Closed"); // clean up WaitSetReader.DeleteReadCondition(readCond); WaitSetReader.DeleteReadCondition(queryCond); mgr.getSubscriber().DeleteDataReader(WaitSetReader); mgr.deleteSubscriber(); mgr.deleteTopic(); mgr.deleteParticipant(); }
static void Main(string[] args) { if (args.Length != 1) { Console.WriteLine("*** [ContentFilteredTopicDataSubscriber] Subscription string not specified"); Console.WriteLine("*** usage : ContentFilteredTopicDataSubscriber <subscription_string>"); Environment.Exit(-1); } ITopic topic; DDSEntityManager mgr = new DDSEntityManager(); String partitionName = "ContentFilteredTopic example"; // Create DomainParticipant mgr.createParticipant(partitionName); // Create Type StockTypeSupport msgTS = new StockTypeSupport(); mgr.registerType(msgTS); // Create Topic topic = mgr.createTopic("StockTrackerExclusive", "ContentFilteredTopic"); // ContentFilteredTopic mgr.createContentFilter("MyStockTopic", topic, args[0]); Console.WriteLine("=== [ContentFilteredTopicDataSubscriber] Subscription filter : ticker = '{0}'",args[0]); // create Subscriber mgr.createSubscriber(); // create DataReader mgr.createReader("ContentFilteredTopic", true); // Read Events IDataReader dreader = mgr.getReader(); StockDataReader WaitSetReader = dreader as StockDataReader; // Create WaitSet IWaitSet w = new WaitSet(); // Status Condition IStatusCondition status; status = dreader.StatusCondition; status.SetEnabledStatuses(StatusKind.DataAvailable); ReturnCode result = ReturnCode.Error; result = w.AttachCondition(status); ErrorHandler.checkHandle(status, "DataReader.StatusCondition"); ErrorHandler.checkStatus(result, "WaitSet.AttachCondition"); DDS.ICondition[] conditionList; DDS.SampleInfo[] infoSeq; Stock[] stockSeq; Console.WriteLine("=== [ContentFilteredTopicDataSubscriber] Ready ..."); bool terminate = false; int count = 0; while (!terminate && count < 1500) { /** * Wait until data will be available */ Duration wait_timeout = new Duration(Duration.InfiniteSec, Duration.InfiniteNanoSec); conditionList = null; result = w.Wait(ref conditionList, wait_timeout); ErrorHandler.checkStatus(result, "WaitSet.Wait"); stockSeq = null; infoSeq = null; /** * Read Data */ result = WaitSetReader.Take(ref stockSeq, ref infoSeq, Length.Unlimited, SampleStateKind.Any, ViewStateKind.Any, InstanceStateKind.Any); ErrorHandler.checkStatus(result, "StockDataReader.Take"); /** * Display Data */ for (int i=0; i < stockSeq.Length; i++) { if (infoSeq[i].ValidData) { if (stockSeq[i].price == -1) { // We read the last expected sample => exit terminate = true; break; } Console.WriteLine("=== [ContentFilteredTopicDataSubscriber] receives stockQuote : ( {0} , {1} )", stockSeq[i].ticker, stockSeq[i].price); } } result = WaitSetReader.ReturnLoan(ref stockSeq, ref infoSeq); ErrorHandler.checkStatus(result, "StockDataReader.ReturnLoan"); Thread.Sleep(200); ++count; } Console.WriteLine("=== [ContentFilteredTopicDataSubscriber] Market Closed"); // clean up mgr.getSubscriber().DeleteDataReader(WaitSetReader); mgr.deleteSubscriber(); mgr.deleteContentFilteredTopic(); mgr.deleteTopic(); mgr.deleteParticipant(); }
static void Main(string[] args) { if (args.Length != 1) { Console.WriteLine("Insufficient number of arguments."); usage(); Environment.Exit(-1); } else { DDSEntityManager mgr = new DDSEntityManager(); String partitionName = "Durability Example"; String durabilityKind = args[0]; // create Domain Participant mgr.createParticipant(partitionName); // set the durability kind mgr.setDurabilityKind(durabilityKind); // create Type MsgTypeSupport msgTS = new MsgTypeSupport(); mgr.registerType(msgTS); // create Topic mgr.createTopic("DurabilityData_Msg", "Durability"); // create Subscriber mgr.createSubscriber(); mgr.createReader("Durability", false); IDataReader dreader = mgr.getReader(); MsgDataReader DurabilityDataReader = dreader as MsgDataReader; Msg[] msgSeq = null; DDS.SampleInfo[] infoSeq = null; Boolean terminate = false; ReturnCode status; Console.WriteLine("=== [Subscriber] Ready ..."); while (!terminate) { status = DurabilityDataReader.Take(ref msgSeq, ref infoSeq, Length.Unlimited, SampleStateKind.Any, ViewStateKind.Any, InstanceStateKind.Any); ErrorHandler.checkStatus(status, "DataReader.Take"); for (int i = 0; i < msgSeq.Length; i++) { if (infoSeq[i].ValidData) { Console.WriteLine("{0} : {1}", msgSeq[i].id, msgSeq[i].content); if (msgSeq[i].content.Equals("9")) { terminate = true; break; } } } status = DurabilityDataReader.ReturnLoan(ref msgSeq, ref infoSeq); ErrorHandler.checkStatus(status, "MsgDataReader.ReturnLoan"); } // clean up mgr.getSubscriber().DeleteDataReader(DurabilityDataReader); mgr.deleteSubscriber(); mgr.deleteTopic(); mgr.deleteParticipant(); } }
static void Main(string[] args) { DDSEntityManager mgr = new DDSEntityManager("ContentFilteredTopic"); String partitionName = "ContentFilteredTopic example"; // create Domain Participant mgr.createParticipant(partitionName); mgr.setAutoDispose(true); // create Type StockTypeSupport msgTS = new StockTypeSupport(); mgr.registerType(msgTS); // create Topic mgr.createTopic("StockTrackerExclusive"); // create Publisher mgr.createPublisher(); // create DataWriter mgr.createWriter(); // Publish Events IDataWriter dwriter = mgr.getWriter(); StockDataWriter ContentFilteredTopicDataWriter = dwriter as StockDataWriter; Stock geStock = new Stock(); Stock msftStock = new Stock(); ReturnCode writeStatus; geStock.ticker = "GE"; geStock.price = 12.00f; msftStock.ticker = "MSFT"; msftStock.price = 25.00f; InstanceHandle geHandle = ContentFilteredTopicDataWriter.RegisterInstance(geStock); InstanceHandle msHandle = ContentFilteredTopicDataWriter.RegisterInstance(msftStock); for (int i = 0; i < 20; i++) { geStock.price += 0.5f ; msftStock.price += 1.5f; Console.WriteLine("=== [ContentFilteredTopicDataPublisher] sends 2 stockQuotes : (GE, {0}) (MSFT, {1})", geStock.price, msftStock.price); writeStatus = ContentFilteredTopicDataWriter.Write(geStock, geHandle); ErrorHandler.checkStatus(writeStatus, "StockDataWriter.Write"); writeStatus = ContentFilteredTopicDataWriter.Write(msftStock, msHandle); ErrorHandler.checkStatus(writeStatus, "StockDataWriter.Write"); Thread.Sleep(100); } geStock.price = -1.0f; msftStock.price = -1.0f; writeStatus = ContentFilteredTopicDataWriter.Write(geStock, geHandle); ErrorHandler.checkStatus(writeStatus, "StockDataWriter.Write GE"); writeStatus = ContentFilteredTopicDataWriter.Write(msftStock, msHandle); ErrorHandler.checkStatus(writeStatus, "StockDataWriter.Write MSFT"); Console.WriteLine("Market Closed"); ContentFilteredTopicDataWriter.UnregisterInstance(geStock, geHandle); ContentFilteredTopicDataWriter.UnregisterInstance(msftStock, msHandle); mgr.getPublisher().DeleteDataWriter(ContentFilteredTopicDataWriter); mgr.deletePublisher(); mgr.deleteTopic(); mgr.deleteParticipant(); }
static void Main(string[] args) { DDSEntityManager mgr = new DDSEntityManager(); String partitionName = "Ownership example"; // create Domain Participant mgr.createParticipant(partitionName); // create Type StockTypeSupport msgTS = new StockTypeSupport(); mgr.registerType(msgTS); // create Topic mgr.createTopic("StockTrackerExclusive", "Ownership"); // create Subscriber mgr.createSubscriber(); // create DataReader mgr.createReader("Ownership", false); // Read Events IDataReader dreader = mgr.getReader(); StockDataReader OwnershipDataReader = dreader as StockDataReader; Stock[] msgSeq = null; DDS.SampleInfo[] infoSeq = null; Console.WriteLine("===[Subscriber] Ready ..."); Console.WriteLine(" Ticker Price Publisher ownership strength"); Boolean terminate = false; ReturnCode status = ReturnCode.Error; int count = 0; while (!terminate && count < 1500) { OwnershipDataReader.Take(ref msgSeq, ref infoSeq, Length.Unlimited, SampleStateKind.Any, ViewStateKind.Any, InstanceStateKind.Any); for (int i = 0; i < msgSeq.Length; i++) { if (infoSeq[i].ValidData) { if (msgSeq[i].price < -0.0f) { terminate = true; break; } Console.WriteLine(" {0} {1} {2} {3}", msgSeq[i].ticker, msgSeq[i].price, msgSeq[i].publisher, msgSeq[i].strength); } } status = OwnershipDataReader.ReturnLoan(ref msgSeq, ref infoSeq); ErrorHandler.checkStatus(status, "StockDataReader.ReturnLoan"); Thread.Sleep(2); ++count; Thread.Sleep(200); } Console.WriteLine("===[Subscriber] Market Closed"); // clean up mgr.getSubscriber().DeleteDataReader(OwnershipDataReader); mgr.deleteSubscriber(); mgr.deleteTopic(); mgr.deleteParticipant(); }
static void Main(string[] args) { if (args.Length != 1) { Console.WriteLine("Bad args number"); Console.WriteLine("*** [QueryConditionDataQuerySubscriber] Query string not specified"); Console.WriteLine("*** usage : QueryConditionDataQuerySubscriber <query_string>"); Environment.Exit(-1); } ITopic topic; DDSEntityManager mgr = new DDSEntityManager(); String partitionName = "QueryCondition example"; String QueryConditionDataToSubscribe = args[0]; // Create DomainParticipant mgr.createParticipant(partitionName); // Create Type StockTypeSupport msgTS = new StockTypeSupport(); mgr.registerType(msgTS); // Create Topic topic = mgr.createTopic("StockTrackerExclusive", "QueryCondition"); // create Subscriber mgr.createSubscriber(); // create DataReader mgr.createReader("QueryCondition", false); // Read Events IDataReader dreader = mgr.getReader(); StockDataReader QueryConditionDataReader = dreader as StockDataReader; String[] queryStr = { QueryConditionDataToSubscribe }; Console.WriteLine("=== [QueryConditionDataQuerySubscriber] Query : ticker = {0}", QueryConditionDataToSubscribe); IQueryCondition qc = QueryConditionDataReader.CreateQueryCondition( SampleStateKind.Any, ViewStateKind.Any, InstanceStateKind.Any, "ticker=%0", queryStr); Console.WriteLine("=== [QueryConditionDataQuerySubscriber] Ready ..."); DDS.SampleInfo[] infoSeq = null; Stock[] stockSeq = null; ReturnCode status = ReturnCode.Error; bool terminate = false; int count = 0; Console.WriteLine("=== [QueryConditionDataQuerySubscriber] Ready ..."); while (!terminate && count < 1500) { // Take Sample with Condition status = QueryConditionDataReader.TakeWithCondition(ref stockSeq, ref infoSeq, Length.Unlimited, qc); ErrorHandler.checkStatus(status, "DataReader.TakeWithCondition"); /** * Display Data */ for (int i=0; i<stockSeq.Length; i++) { if (infoSeq[i].ValidData) { if (stockSeq[i].price == -1.0f) { terminate = true; break; } Console.WriteLine("{0} : {1}", stockSeq[i].ticker, String.Format("{0:0.#}", stockSeq[i].price)); } } status = QueryConditionDataReader.ReturnLoan(ref stockSeq, ref infoSeq); ErrorHandler.checkStatus(status, "DataReader.ReturnLoan"); Thread.Sleep(200); ++count; } Console.WriteLine("=== [QueryConditionDataQuerySubscriber] Market Closed"); // clean up QueryConditionDataReader.DeleteReadCondition(qc); mgr.getSubscriber().DeleteDataReader(QueryConditionDataReader); mgr.deleteSubscriber(); mgr.deleteTopic(); mgr.deleteParticipant(); }
static void Main(string[] args) { bool autodispose_flag = false; String writer_action; ReturnCode status = ReturnCode.Error; if (args.Length < 2) { usage(); } else if ((!args[0].Equals("true")) && (!args[0].Equals("false")) && (!args[1].Equals("dispose")) && (!args[1].Equals("unregister")) && (!args[1].Equals("stoppub"))) { usage(); } else { autodispose_flag = Boolean.Parse(args[0]); writer_action = args[1]; // DDS Helper class instance for Msg Topic DDSEntityManager mgr = new DDSEntityManager("Lifecycle"); // Create domain participant String partitionName = "Lifecycle example"; mgr.createParticipant(partitionName); // Set AutoDispose Flag mgr.setAutoDispose(autodispose_flag); // Create type MsgTypeSupport mt = new MsgTypeSupport(); mgr.registerType(mt); // Create Topic String topicName = "Lifecycle_Msg"; mgr.createTopic(topicName); // Create Publisher mgr.createPublisher(); // Create DataWriter(s) mgr.createWriter(); // Publish Samples IDataWriter dwriter = mgr.getWriter(); MsgDataWriter LifecycleWriter = dwriter as MsgDataWriter; IDataWriter dwriter_stopper = mgr.getWriterStopper(); MsgDataWriter LifecycleWriter_stopper = dwriter_stopper as MsgDataWriter; if (writer_action.Equals("dispose")) { // Send Msg (topic to monitor) Msg msgInstance = new Msg(); msgInstance.userID = 1; msgInstance.message = "Lifecycle_1"; msgInstance.writerStates = "SAMPLE_SENT -> INSTANCE_DISPOSED -> DATAWRITER_DELETED"; Console.WriteLine("=== [Publisher] :"); Console.WriteLine(" userID : {0}", msgInstance.userID); Console.WriteLine(" Message : \"{0}\"", msgInstance.message); Console.WriteLine(" writerStates : \"{0}\"", msgInstance.writerStates); status = LifecycleWriter.Write(msgInstance, InstanceHandle.Nil); ErrorHandler.checkStatus(status, "MsDataWriter.Write"); Thread.Sleep(500); Console.WriteLine("=== [Publisher] : SAMPLE_SENT"); // Dispose instance status = LifecycleWriter.Dispose(msgInstance, InstanceHandle.Nil); ErrorHandler.checkStatus(status, "MsDataWriter.Dispose"); Console.WriteLine("=== [Publisher] : INSTANCE_DISPOSED"); } else if (writer_action.Equals("unregister")) { // Send Msg (topic to monitor) Msg msgInstance = new Msg(); msgInstance.userID = 2; msgInstance.message = "Lifecycle_2"; msgInstance.writerStates = "SAMPLE_SENT -> INSTANCE_UNREGISTERED -> DATAWRITER_DELETED"; Console.WriteLine("=== [Publisher] :"); Console.WriteLine(" userID : {0}", msgInstance.userID); Console.WriteLine(" Message : \"{0}\"", msgInstance.message); Console.WriteLine(" writerStates : \"{0}\"", msgInstance.writerStates); status = LifecycleWriter.Write(msgInstance, InstanceHandle.Nil); ErrorHandler.checkStatus(status, "MsDataWriter.Write"); Thread.Sleep(500); Console.WriteLine("=== [Publisher] : SAMPLE_SENT"); // Unregister instance : the auto_dispose_unregistered_instances flag // is currently ignored and the instance is never disposed automatically status = LifecycleWriter.UnregisterInstance(msgInstance, InstanceHandle.Nil); ErrorHandler.checkStatus(status, "MsDataWriter.UnregisterInstance"); Console.WriteLine("=== [Publisher] : INSTANCE_UNREGISTERED"); } else if (writer_action.Equals("stoppub")) { Msg msgInstance = new Msg(); msgInstance.userID = 3; msgInstance.message = "Lifecycle_3"; msgInstance.writerStates = "SAMPLE_SENT -> DATAWRITER_DELETED"; Console.WriteLine("=== [Publisher] :"); Console.WriteLine(" userID : {0}", msgInstance.userID); Console.WriteLine(" Message : \"{0}\"", msgInstance.message); Console.WriteLine(" writerStates : \"{0}\"", msgInstance.writerStates); status = LifecycleWriter.Write(msgInstance, InstanceHandle.Nil); ErrorHandler.checkStatus(status, "MsDataWriter.Write"); Thread.Sleep(500); Console.WriteLine("=== [Publisher] : SAMPLE_SENT"); } // Let the subscriber treat the previous writer state !!! Console.WriteLine("=== [Publisher] waiting 500ms to let the subscriber treat the previous write state ..."); Thread.Sleep(500); // Remove the DataWriter mgr.deleteWriter(LifecycleWriter); Thread.Sleep(500); Console.WriteLine("=== [Publisher] : DATAWRITER_DELETED"); // Stop the subscriber Msg stopMsg = new Msg(); stopMsg.userID = 4; stopMsg.message = "Lifecycle_4"; stopMsg.writerStates = "STOPPING_SUBSCRIBER"; Console.WriteLine("=== [Publisher] :"); Console.WriteLine(" userID : {0}", stopMsg.userID); Console.WriteLine(" Message : \"{0}\"", stopMsg.message); Console.WriteLine(" writerStates : \"{0}\"", stopMsg.writerStates); status = LifecycleWriter_stopper.Write(stopMsg, InstanceHandle.Nil); ErrorHandler.checkStatus(status, "MsDataWriter.Write"); Thread.Sleep(500); // Remove the DataWriter stopper mgr.deleteWriter(LifecycleWriter_stopper); // Remove the Publisher mgr.deletePublisher(); // Remove the Topic mgr.deleteTopic(); // Remove the participant mgr.deleteParticipant(); } }
static void Main(string[] args) { String[] sSampleState = { "READ_SAMPLE_STATE", "NOT_READ_SAMPLE_STATE" }; String[] sViewState = { "NEW_VIEW_STATE", "NOT_NEW_VIEW_STATE" }; String[] sInstanceState = { "ALIVE_INSTANCE_STATE", "NOT_ALIVE_DISPOSED_INSTANCE_STATE", "NOT_ALIVE_NO_WRITERS_INSTANCE_STATE" }; WriterState[] WriterStateList = null; SampleInfo[] ws_infoSeq = null; Msg[] msgList = null; SampleInfo[] infoSeq = null; //------------------ WriterState topic --------------------// DDSEntityManager mgrWs = new DDSEntityManager(); // Create domain participant String partitionName = "Lifecycle"; mgrWs.createParticipant(partitionName); // Create Type WriterStateTypeSupport wst = new WriterStateTypeSupport(); mgrWs.registerType(wst); // Create Topic String wsTopicName = "WriterState_Msg"; mgrWs.createTopic(wsTopicName, "Lifecycle"); // Create Subscriber mgrWs.createSubscriber(); // Create DataReader mgrWs.createReader("Lifecycle", false); IDataReader dwsReader = mgrWs.getReader(); WriterStateDataReader WriterStateReader = dwsReader as WriterStateDataReader; ErrorHandler.checkHandle(WriterStateReader, "WriterStateDataReader narrow"); //------------------ Msg topic --------------------// DDSEntityManager mgr = new DDSEntityManager(); // Create domain participant mgr.createParticipant(partitionName); // Create type MsgTypeSupport mt = new MsgTypeSupport(); mgr.registerType(mt); // Create Topic String topicName = "Lifecycle_Msg"; mgr.createTopic(topicName, "Lifecycle"); // Create Subscriber mgr.createSubscriber(); mgr.createReader("Lifecycle", false); IDataReader dreader = mgr.getReader(); MsgDataReader LifecycleReader = dreader as MsgDataReader; ErrorHandler.checkHandle(WriterStateReader, "MsgDataReader narrow"); Console.WriteLine("=== [Subscriber] Ready ..."); bool closed = false; bool writerStateChg = true; String sWriterState = ""; ReturnCode status = ReturnCode.Error; while (!closed) { // WriterState topic status = WriterStateReader.Take(ref WriterStateList, ref ws_infoSeq, Length.Unlimited, SampleStateKind.Any, ViewStateKind.Any, InstanceStateKind.Any); ErrorHandler.checkStatus(status, "WriterStateReader.Take"); for (int j = 0; j < WriterStateList.Length; j++) { if (ws_infoSeq[j].ValidData) { Console.WriteLine("--- WriterState : {0}", WriterStateList[j].state); closed = WriterStateList[j].state.Equals("STOPPING SUBSCRIBER"); writerStateChg = sWriterState.Equals(WriterStateList[j].state); sWriterState = WriterStateList[j].state; Thread.Sleep(200); } } status = WriterStateReader.ReturnLoan(ref WriterStateList, ref ws_infoSeq); ErrorHandler.checkStatus(status, "WriterStateDataReader.ReturnLoan"); Thread.Sleep(2); // Lifecycle Topic if (writerStateChg) { status = LifecycleReader.Read(ref msgList, ref infoSeq, Length.Unlimited, SampleStateKind.Any, ViewStateKind.Any, InstanceStateKind.Any); ErrorHandler.checkStatus(status, "MsgDataReader.Read"); for (int j = 0; j < msgList.Length; j++) { Console.WriteLine(" Message : {0}", msgList[j].message); Console.WriteLine(" valida data : {0}", infoSeq[j].ValidData); Console.WriteLine(" sample_state : {0}", sSampleState[index((int)infoSeq[j].SampleState)]); Console.WriteLine(" view_state : {0}", sViewState[index((int)infoSeq[j].ViewState)]); Console.WriteLine(" instance_state : {0}", sInstanceState[index((int)infoSeq[j].InstanceState)]); Thread.Sleep(200); } status = LifecycleReader.ReturnLoan(ref msgList, ref infoSeq); ErrorHandler.checkStatus(status, "MsgDataReader.ReturnLoan"); Thread.Sleep (2); writerStateChg = false; } } // Clean-up Msg Entities mgr.getSubscriber().DeleteDataReader(LifecycleReader); mgr.deleteSubscriber(); mgr.deleteTopic(); mgr.deleteParticipant(); // Clean-up WriterState Entities mgrWs.getSubscriber().DeleteDataReader(WriterStateReader); mgrWs.deleteSubscriber(); mgrWs.deleteTopic(); mgrWs.deleteParticipant(); }
static void Main(string[] args) { DDSEntityManager mgr = new DDSEntityManager(); String partitionName = "QueryCondition example"; // create Domain Participant mgr.createParticipant(partitionName); // create Type StockTypeSupport msgTS = new StockTypeSupport(); mgr.registerType(msgTS); // create Topic mgr.createTopic("StockTrackerExclusive", "QueryCondition"); // create Publisher mgr.createPublisher(); // create DataWriter mgr.createWriter(); // Publish Events IDataWriter dwriter = mgr.getWriter(); StockDataWriter QueryConditionDataWriter = dwriter as StockDataWriter; Stock geStock = new Stock(); Stock msftStock = new Stock(); ReturnCode writeStatus; geStock.ticker = "GE"; geStock.price = 12.00f; msftStock.ticker = "MSFT"; msftStock.price = 25.00f; // Register Instances InstanceHandle geHandle = QueryConditionDataWriter.RegisterInstance(geStock); ErrorHandler.checkHandle(geHandle, "DataWriter.RegisterInstance (GE)"); InstanceHandle msHandle = QueryConditionDataWriter.RegisterInstance(msftStock); ErrorHandler.checkHandle(msHandle, "DataWriter.RegisterInstance (MSFT)"); for (int i = 0; i < 20; i++) { geStock.price += 0.5f; msftStock.ticker += 1.5f; writeStatus = QueryConditionDataWriter.Write(geStock, InstanceHandle.Nil); ErrorHandler.checkStatus(writeStatus, "StockDataWriter.Write"); writeStatus = QueryConditionDataWriter.Write(msftStock, InstanceHandle.Nil); ErrorHandler.checkStatus(writeStatus, "StockDataWriter.Write"); Thread.Sleep(100); Console.WriteLine("GE : {0} MSFT {1}",String.Format("{0:0.#}", geStock.price), String.Format("{0:0.#}", msftStock.price)); } geStock.price = -1.0f; msftStock.price = -1.0f; // Write samples writeStatus = QueryConditionDataWriter.Write(geStock, InstanceHandle.Nil); ErrorHandler.checkStatus(writeStatus, "StockDataWriter.Write (GE)"); writeStatus = QueryConditionDataWriter.Write(msftStock, InstanceHandle.Nil); ErrorHandler.checkStatus(writeStatus, "StockDataWriter.Write (MS)"); // Dispose Instances writeStatus = QueryConditionDataWriter.Dispose(geStock, geHandle); ErrorHandler.checkStatus(writeStatus, "StockDataWriter.Dispose (GE)"); writeStatus = QueryConditionDataWriter.Dispose(msftStock, msHandle); ErrorHandler.checkStatus(writeStatus, "StockDataWriter.Dispose (MS)"); // Unregister Instances writeStatus = QueryConditionDataWriter.UnregisterInstance(geStock, geHandle); ErrorHandler.checkStatus(writeStatus, "StockDataWriter.UnregisterInstance (GE)"); writeStatus = QueryConditionDataWriter.UnregisterInstance(msftStock, msHandle); ErrorHandler.checkStatus(writeStatus, "StockDataWriter.UnregisterInstance(MS)"); // Clean up mgr.getPublisher().DeleteDataWriter(QueryConditionDataWriter); mgr.deletePublisher(); mgr.deleteTopic(); mgr.deleteParticipant(); }
static void Main(string[] args) { char choice = ' '; bool interactive = true; bool autodispose_flag = false; if (args.Length < 1) usage(); autodispose_flag = Boolean.Parse(args[0]); if (args.Length > 1) { if (!(args[1].Equals("-action"))) usage(); if (args.Length < 2) usage(); choice = Char.Parse(args[2]); if ((!choice.Equals('1')) && (!choice.Equals('2')) && (!choice.Equals('3')) && (!choice.Equals('4'))) usage(); interactive = false; Console.WriteLine("Choice : {0}", choice); } // DDS Helper class instances for WriterState & Msg Topic DDSEntityManager mgrWs = new DDSEntityManager(); DDSEntityManager mgr = new DDSEntityManager(); // Create domain participant String partitionName = "Lifecycle"; mgrWs.createParticipant(partitionName); // Create Type WriterStateTypeSupport wst = new WriterStateTypeSupport(); mgrWs.registerType(wst); // Create Topic String wsTopicName = "WriterState_Msg"; mgrWs.createTopic(wsTopicName, "Lifecycle"); // Create Publisher mgrWs.createPublisher(); // Create DataWriter mgrWs.createWriter(); // Publish Samples IDataWriter dws_writer = mgrWs.getWriter(); WriterStateDataWriter WriterStateWriter = dws_writer as WriterStateDataWriter; //-------------------Msg Topic-------------------------// // Set AutoDispose Flag mgr.setAutoDispose(autodispose_flag); // Create Participant mgr.createParticipant(partitionName); // Create type MsgTypeSupport mt = new MsgTypeSupport(); mgr.registerType(mt); // Create Topic String topicName = "Lifecycle_Msg"; mgr.createTopic(topicName, "Lifecycle"); // Create Publisher mgr.createPublisher(); // Create DataWriter mgr.createWriter(); // Publish Samples IDataWriter dwriter = mgr.getWriter(); MsgDataWriter LifecycleWriter = dwriter as MsgDataWriter; // Send MsgDataWriter state WriterState wsInstance = new WriterState(); wsInstance.state = "SENDING SAMPLE"; Console.WriteLine("===[Publisher] : "); Console.WriteLine(" Writer state: \" {0}", wsInstance.state, "\""); ReturnCode status = WriterStateWriter.Write(wsInstance, InstanceHandle.Nil); ErrorHandler.checkStatus(status, "WriterStateDataWriter.Write"); // Send Msg (topic to monitor) Msg msgInstance = new Msg(); msgInstance.userID = 1; msgInstance.message = "Hello"; Console.WriteLine("=== [Publisher] :"); Console.WriteLine(" userID : {0}", msgInstance.userID); Console.WriteLine(" Message : \" {0}", msgInstance.message); status = LifecycleWriter.Write(msgInstance, InstanceHandle.Nil); ErrorHandler.checkStatus(status, "MsDataWriter.Write"); Thread.Sleep(2000); // Send MsgDataWriter state wsInstance.state = "SAMPLE SENT"; Console.WriteLine("===[Publisher] : "); Console.WriteLine(" Writer state: \" {0}", wsInstance.state, "\""); status = WriterStateWriter.Write(wsInstance, InstanceHandle.Nil); ErrorHandler.checkStatus(status, "WriterStateDataWriter.Write"); if (interactive) { while ((choice != '1') && (choice != '2') && (choice != '3') && (choice != '4')) { Console.WriteLine("=== Choose a character to continue : "); Console.WriteLine(" 1 Dispose the instance"); Console.WriteLine(" 2 Unregister the instance"); Console.WriteLine(" 3 Stop the Publisher"); Console.WriteLine(" 4 Stop the Subscriber"); choice = Char.Parse(Console.In.ReadLine()); } } switch (choice) { case '1' : { // Dispose instance status = LifecycleWriter.Dispose(msgInstance, InstanceHandle.Nil); ErrorHandler.checkStatus(status, "MsDataWriter.Dispose"); // Send MsgDataWriter state wsInstance.state = "INSTANCE DISPOSED"; Console.WriteLine("===[Publisher] : "); Console.WriteLine(" Writer state: \" {0}", wsInstance.state, "\""); status = WriterStateWriter.Write(wsInstance, InstanceHandle.Nil); ErrorHandler.checkStatus(status, "WriterStateDataWriter.Write"); if (interactive) { Console.Write("\n=== enter a character to continue : "); choice = Char.Parse(Console.In.ReadLine()); } break; } case '2': { // Unregister instance : the autoDispose_flag is currently // ignored and the instance is never disposed automatically status = LifecycleWriter.UnregisterInstance(msgInstance, InstanceHandle.Nil); ErrorHandler.checkStatus(status, "MsgDataWriter.UnregisterInstance"); // send MsgDataWriter state wsInstance.state = "INSTANCE UNREGISTERED"; Console.WriteLine("===[Publisher] : "); Console.WriteLine(" Writer state: \" {0}", wsInstance.state, "\""); status = WriterStateWriter.Write(wsInstance, InstanceHandle.Nil); ErrorHandler.checkStatus(status, "WriterStateDataWriter.Write"); if (interactive) { Console.Write("\n=== enter a character to continue : "); choice = Char.Parse(Console.In.ReadLine()); } break; } case '3': { break; } case '4': { // send MsgDataWriter state wsInstance.state = "STOPPING SUBSCRIBER"; Console.WriteLine("===[Publisher] : "); Console.WriteLine(" Writer state: \" {0}", wsInstance.state, "\""); status = WriterStateWriter.Write(wsInstance, InstanceHandle.Nil); ErrorHandler.checkStatus(status, "WriterStateDataWriter.Write"); break; } } // Clean-up entities for Msg topic // Remove the DataWriters mgr.getPublisher().DeleteDataWriter(LifecycleWriter); // send MsgDataWriter state wsInstance.state = "DATAWRITER DELETED"; Console.WriteLine("===[Publisher] : "); Console.WriteLine(" Writer state: \" {0}", wsInstance.state, "\""); status = WriterStateWriter.Write(wsInstance, InstanceHandle.Nil); ErrorHandler.checkStatus(status, "WriterStateDataWriter.Write"); // Remove the Publisher mgr.deletePublisher(); // Remove the Topic mgr.deleteTopic(); // Remove the Participant mgr.deleteParticipant(); // Clean-up entities fpr WriterState topic // Dispose the instance status = WriterStateWriter.Dispose(wsInstance, InstanceHandle.Nil); ErrorHandler.checkStatus(status, "WriterStateWriter.Dispose"); // Remove the DataWriter mgrWs.getPublisher().DeleteDataWriter(WriterStateWriter); // Remove the Publisher mgrWs.deletePublisher(); // Remove the Topic mgrWs.deleteTopic(); // Remove Participant mgrWs.deleteParticipant(); }
static void Main(string[] args) { String[] sSampleState = { "READ_SAMPLE_STATE", "NOT_READ_SAMPLE_STATE" }; String[] sViewState = { "NEW_VIEW_STATE", "NOT_NEW_VIEW_STATE" }; String[] sInstanceState = { "ALIVE_INSTANCE_STATE", "NOT_ALIVE_DISPOSED_INSTANCE_STATE", "NOT_ALIVE_NO_WRITERS_INSTANCE_STATE" }; bool closed = false; int nbIter = 1; int nbIterMax = 100; ReturnCode status = ReturnCode.Error; Msg[] msgList = null; SampleInfo[] infoSeq = null; DDSEntityManager mgr = new DDSEntityManager("Lifecycle"); // Create domain participant String partitionName = "Lifecycle example"; mgr.createParticipant(partitionName); // Create type MsgTypeSupport mt = new MsgTypeSupport(); mgr.registerType(mt); // Create Topic String topicName = "Lifecycle_Msg"; mgr.createTopic(topicName); // Create Subscriber mgr.createSubscriber(); mgr.createReader(false); IDataReader dreader = mgr.getReader(); MsgDataReader LifecycleReader = dreader as MsgDataReader; ErrorHandler.checkHandle(LifecycleReader, "MsgDataReader narrow"); Console.WriteLine("=== [Subscriber] Ready ..."); while (!closed && (nbIter < nbIterMax)) { status = LifecycleReader.Read(ref msgList, ref infoSeq, Length.Unlimited, SampleStateKind.Any, ViewStateKind.Any, InstanceStateKind.Any); ErrorHandler.checkStatus(status, "MsgDataReader.Read"); for (int j = 0; j < msgList.Length; j++) { Console.WriteLine(" Message : {0}", msgList[j].message); Console.WriteLine(" writerStates : {0}", msgList[j].writerStates); Console.WriteLine(" valida data : {0}", infoSeq[j].ValidData); string str = "sample_state:" + sSampleState[index((int)infoSeq[j].SampleState)] + "-view_state:" + sViewState[index((int)infoSeq[j].ViewState)] + "-instance_state:" + sInstanceState[index((int)infoSeq[j].InstanceState)]; Console.WriteLine(str); Thread.Sleep(200); closed = msgList[j].writerStates.Equals("STOPPING_SUBSCRIBER"); } status = LifecycleReader.ReturnLoan(ref msgList, ref infoSeq); ErrorHandler.checkStatus(status, "MsgDataReader.ReturnLoan"); Thread.Sleep(20); nbIter++; } Console.WriteLine("=== [Subscriber] stopping after {0} iterations - closed={1}", nbIter, closed.ToString()); if (nbIter == nbIterMax) Console.WriteLine("*** Error : max {0} iterations reached", nbIterMax); // Clean-up mgr.getSubscriber().DeleteDataReader(LifecycleReader); mgr.deleteSubscriber(); mgr.deleteTopic(); mgr.deleteParticipant(); }
static void Main(string[] args) { bool automatic = true; if (args.Length > 1) { automatic = args[0].Equals("true"); } string hostName; DDSEntityManager mgr = new DDSEntityManager(); // create domain participant mgr.createParticipant("BuiltInTopics"); // Resolve the built-in Subscriber ISubscriber builtInSubscriber = mgr.getParticipant().BuiltInSubscriber; // Lookup the DataReader for the DCPSParticipant built-in Topic IDataReader reader = builtInSubscriber.LookupDataReader("DCPSParticipant"); // Cast the DataReader to a ParticipantBuiltInTopicDataReader ParticipantBuiltinTopicDataDataReader participantReader = reader as ParticipantBuiltinTopicDataDataReader; ErrorHandler.checkHandle(participantReader, "ParticipantBuiltInTopicDataReader narrow"); // Allocate a new typed seq for data samples ParticipantBuiltinTopicData[] data = null; // Allocate a new seq for sample infos SampleInfo[] info = null; Console.WriteLine("=== [BuiltInTopicsDataSubscriber] : Waiting for historical data ... "); participantReader.WaitForHistoricalData(Duration.Infinite); Console.WriteLine("=== [BuiltInTopicsDataSubscriber] : done"); // Create a new ReadCondition for the reader that matches all samples IReadCondition readCond = participantReader.CreateReadCondition(SampleStateKind.Any, ViewStateKind.Any, InstanceStateKind.Any); ErrorHandler.checkHandle(readCond, "DataReader.CreateReadCondition"); // Create a waitset and add the ReadCondition created above WaitSet aWaitSet = new WaitSet(); ReturnCode status = aWaitSet.AttachCondition(readCond); ErrorHandler.checkStatus(status, "WaitSet.AttachCondition"); // Initialize and pre-allocate the seq used to obtain the triggered Conditions. ICondition[] condSeq = new ICondition[0]; // Allocate a map store node information later on // The key of map is the id of the node and the value is the // number of active participants on that node. A Dictionary is used // as an UnorderedMap. Dictionary<int, int> nodes = new Dictionary<int, int>(); // Allocate a map to store node information later on. // The key of the map is the id of the node and the value is the // name of the node. A Dictionary is used as an UnorderedMap. Dictionary<int, string> nodeNames = new Dictionary<int, string>(); Console.WriteLine("=== [BuiltInTopicsDataSubscriber] Ready ..."); // Block the current thread until the attached condition becomes true // or the user interrupts. status = aWaitSet.Wait(ref condSeq, Duration.Infinite); ErrorHandler.checkStatus(status, "WaitSet.Wait"); bool done = false; // Continue processing until interrupted. while (!done) { // Take all the available data from the reader status = participantReader.Take(ref data, ref info, Length.Unlimited, SampleStateKind.Any, ViewStateKind.Any, InstanceStateKind.Any); ErrorHandler.checkStatus(status, "ParticipantBuiltinTopicDataDataReader.Take"); // Verify the data has been taken if (status == ReturnCode.Ok) { // Iterate the list of taken samples. for (int i = 0; i < data.Length ; i++) { // Resolve the node identification int nodeId = data[i].Key[0]; // Initialise the number of participants for a node int participantCount = 0; // Check if we saw a participant for the node before. if (nodes.ContainsKey(nodeId)) { participantCount = nodes[nodeId]; } // Check sample info to see whether the instance is ALIVE. if (info[i].InstanceState == InstanceStateKind.Alive) { // The splicedaemon publishes the host-name in the // user_data field if (data[i].UserData.Value.Length != 0) { hostName = Encoding.UTF8.GetString(data[i].UserData.Value); nodeNames[nodeId] = hostName; } else { hostName = null; } // Increase the number of participants. participantCount++; // Update the number of participants for the node. nodes[nodeId] = participantCount; // If it's the first participant, report the node is up. if (participantCount == 1) { Console.WriteLine("=== [BuiltInTopicsDataSubscriber] Node ' {0} ' started (Total nodes running: {1} ) participantCount = {2}", nodeId, nodes.Count, participantCount); } if (hostName != null) { Console.WriteLine("=== [BuiltInTopicsDataSubscriber] Hostname for node ' {0} ' is hostname: {1}", nodeId, hostName); } } else { // Decrease the number of participants. participantCount--; // If no more participants exist, report the node is down. if (participantCount == 0) { hostName = nodeNames[nodeId]; nodeNames.Remove(nodeId); nodes.Remove(nodeId); if (hostName != null) { Console.WriteLine("=== [BuiltInTopicsDataSubscriber] Node {0} ({1}) stopped (Total nodes running: ({2})", nodeId, hostName, nodes.Count); } else { Console.WriteLine("=== [BuiltInTopicsDataSubscriber] Node {0} stopped (Total nodes running : {1})", nodeId, nodes.Count); } } else if (participantCount > 0) { nodes[nodeId] = participantCount; } } } } status = participantReader.ReturnLoan(ref data, ref info); ErrorHandler.checkStatus(status, "ParticipantReader.ReturnLoan"); if (!automatic) { // Block the current thread until the attached condition becomes // true or the user interrupts. Console.WriteLine("=== [BuiltInTopicsDataSubscriber] Waiting ... "); status = aWaitSet.Wait(ref condSeq, Duration.Infinite); done = (status != ReturnCode.Ok); } else { done = true; } } // Recursively delete all entities in the DomainParticipant. mgr.getParticipant().DeleteContainedEntities(); // Delete DomainParticipant mgr.getDomainParticipantFactory().DeleteParticipant(mgr.getParticipant()); }
static void Main(string[] args) { if(args.Length != 3) { Console.WriteLine("Insufficient number of arguments."); usage(); Environment.Exit(-1); } else { String durabilityKind = args[0]; Boolean autodisposeFlag = Boolean.Parse(args[1].ToString()); Boolean automaticFlag = Boolean.Parse(args[2].ToString()); DDSEntityManager mgr = new DDSEntityManager(); String partitionName = "Durability example"; // Set the Durability Kind mgr.setDurabilityKind(durabilityKind); // Set the auto dispose flag mgr.setAutoDispose(autodisposeFlag); Thread.Sleep(1000); // create Domain Participant mgr.createParticipant(partitionName); // create Type MsgTypeSupport stkTS = new MsgTypeSupport(); mgr.registerType(stkTS); // create Topic mgr.createTopic("DurabilityData_Msg", "Durability"); // create Publisher mgr.createPublisher(); // create DataWriter mgr.createWriter(); // Publish Events IDataWriter dwriter = mgr.getWriter(); MsgDataWriter msgWriter = dwriter as MsgDataWriter; Msg [] DurabilityDataMsg = new Msg[10]; InstanceHandle [] handle = new InstanceHandle [10]; ReturnCode status = ReturnCode.Error; for (int x = 0; x < 10; x++) { DurabilityDataMsg[x] = new Msg(); DurabilityDataMsg[x].id = x; DurabilityDataMsg[x].content = x.ToString(); Console.WriteLine("Storing {0}", DurabilityDataMsg[x].content); handle[x] = msgWriter.RegisterInstance(DurabilityDataMsg[x]); ErrorHandler.checkHandle(handle[x], "DataWriter.RegisterInstance"); status = msgWriter.Write(DurabilityDataMsg[x], handle[x]); ErrorHandler.checkStatus(status, "DataWriter.Write"); } if (!automaticFlag) { char c = (char)0; Console.WriteLine("Enter E to exit"); while (c != 'E') { c = (char)Console.Read(); } } else { Console.WriteLine("=== sleeping 10s..."); Thread.Sleep(10000); } // Clean up status = mgr.getPublisher().DeleteDataWriter(msgWriter); ErrorHandler.checkStatus(status, "Publisher.DeleteDatWriter"); mgr.deletePublisher(); mgr.deleteTopic(); mgr.deleteParticipant(); } }