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) { 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(); }