public void OnDataAvailable(IDataReader the_reader) { previous = -1; /* Take all messages. */ ReturnCode status = chatMessageDR.Take( ref messages, ref infoSeq1, SampleStateKind.Any, ViewStateKind.Any, InstanceStateKind.Any); ErrorHandler.checkStatus( status, "Chat.ChatMessageDataReader.take"); /* For each message, extract the key-field and find * the corresponding name. */ for (int i = 0; i < messages.Length; i++) { if (infoSeq1[i].ValidData) { /* Find the corresponding named message. */ if (messages[i].userID != previous) { previous = messages[i].userID; nameFinderParams[0] = previous.ToString(); status = nameFinder.SetQueryParameters(nameFinderParams); ErrorHandler.checkStatus( status, "DDS.QueryCondition.SetQueryParameters"); status = nameServiceDR.ReadWithCondition( ref names, ref infoSeq2, nameFinder); ErrorHandler.checkStatus( status, "Chat.NameServiceDataReader.ReadWithCondition"); /* Extract Name (there should only be one result). */ if (status == ReturnCode.NoData) { userName = "******" + previous; } else { userName = names[0].name; } /* Release the name sample again. */ status = nameServiceDR.ReturnLoan(ref names, ref infoSeq2); ErrorHandler.checkStatus( status, "Chat.NameServiceDataReader.return_loan"); } /* Write merged Topic with userName instead of userID. */ joinedSample.userName = userName; joinedSample.userID = messages[i].userID; joinedSample.index = messages[i].index; joinedSample.content = messages[i].content; status = namedMessageDW.Write(joinedSample); ErrorHandler.checkStatus( status, "Chat.NamedMessageDataWriter.write"); } } status = chatMessageDR.ReturnLoan(ref messages, ref infoSeq1); ErrorHandler.checkStatus( status, "Chat.ChatMessageDataReader.return_loan"); }
public void OnDataAvailable(IDataReader the_reader) { int count = 0; previous = -1; /* Take all messages. */ ReturnCode status = chatMessageDR.Take( ref messages, ref infoSeq1, SampleStateKind.Any, ViewStateKind.Any, InstanceStateKind.Any); ErrorHandler.checkStatus( status, "Chat.ChatMessageDataReader.take"); /* For each message, extract the key-field and find * the corresponding name. */ for (int i = 0; i < messages.Length; i++) { if (infoSeq1[i].ValidData) { /* Find the corresponding named message. */ if (messages[i].userID != previous) { previous = messages[i].userID; nameFinderParams[0] = previous.ToString(); status = nameFinder.SetQueryParameters(nameFinderParams); ErrorHandler.checkStatus( status, "DDS.QueryCondition.SetQueryParameters"); /* To ensure the identifier for the sender is presented with the message * content, it may be necessary to try reading again if the NameService * sample is not initially available. This is because the arrival order * of samples representing different topics cannot be guaranteeed. */ status = ReturnCode.NoData; while (status == ReturnCode.NoData && count < 10) { status = nameServiceDR.ReadWithCondition( ref names, ref infoSeq2, nameFinder); ErrorHandler.checkStatus( status, "Chat.NameServiceDataReader.ReadWithCondition"); /* Extract Name (there should only be one result). */ if (status == ReturnCode.NoData) { userName = "******" + previous; /* Sleep for some amount of time, as not to consume too much CPU cycles. */ System.Threading.Thread.Sleep(100); count++; } else { userName = names[0].name; /* Release the name sample again. */ status = nameServiceDR.ReturnLoan(ref names, ref infoSeq2); ErrorHandler.checkStatus( status, "Chat.NameServiceDataReader.return_loan"); } } } /* Write merged Topic with userName instead of userID. */ joinedSample.userName = userName; joinedSample.userID = messages[i].userID; joinedSample.index = messages[i].index; joinedSample.content = messages[i].content; status = namedMessageDW.Write(joinedSample); ErrorHandler.checkStatus( status, "Chat.NamedMessageDataWriter.write"); } } status = chatMessageDR.ReturnLoan(ref messages, ref infoSeq1); ErrorHandler.checkStatus( status, "Chat.ChatMessageDataReader.return_loan"); }