int SimpleDataWriter::run(SimplePublisher* publisher, unsigned num_messages) { this->num_messages_delivered_ = 0; this->num_messages_sent_ = num_messages; // Set up the DataSampleList TAO::DCPS::DataSampleList samples; samples.head_ = 0; samples.tail_ = 0; samples.size_ = num_messages; // This is what goes in the "Data Block". std::string data = "Hello World!"; // Now we can create the DataSampleHeader struct and set its fields. TAO::DCPS::DataSampleHeader header; header.message_id_ = 1; header.publication_id_ = this->pub_id_; TAO::DCPS::DataSampleListElement* prev_element = 0; TAO::DCPS::DataSampleListElementAllocator allocator(num_messages); TAO::DCPS::TransportSendElementAllocator trans_allocator(num_messages, sizeof (TAO::DCPS::TransportSendElement)); for (unsigned i = 1; i <= num_messages; ++i) { // This is what goes in the "Data Block". std::ostringstream ostr; ostr << data << " [" << i << "]"; std::string data_str = ostr.str(); ssize_t num_data_bytes = data_str.length() + 1; header.message_length_ = num_data_bytes; header.sequence_ = i; // The DataSampleHeader is what goes in the "Header Block". ACE_Message_Block* header_block = new ACE_Message_Block(header.max_marshaled_size()); header_block << header; ACE_Message_Block* data_block = new ACE_Message_Block(num_data_bytes); data_block->copy(data_str.c_str()); // Chain the "Data Block" to the "Header Block" header_block->cont(data_block); // Create the DataSampleListElement now. TAO::DCPS::DataSampleListElement* element; ACE_NEW_MALLOC_RETURN(element, static_cast<TAO::DCPS::DataSampleListElement*> (allocator.malloc(sizeof (TAO::DCPS::DataSampleListElement))), TAO::DCPS::DataSampleListElement(this->pub_id_, this, 0, &trans_allocator), 1); // The Sample Element will hold the chain of blocks (header + data). element->sample_ = header_block; if (prev_element == 0) { samples.head_ = element; } else { prev_element->next_send_sample_ = element; } prev_element = element; samples.tail_ = element; } publisher->send_samples(samples); return 0; }
int DDS_TEST::run(int num_messages, int msg_size) { VDBG((LM_DEBUG, "(%P|%t) DBG: " "Build the DataSampleElementList\n")); this->num_messages_delivered_ = 0; this->num_messages_sent_ = num_messages; // Set up the SendStateDataSampleList OpenDDS::DCPS::SendStateDataSampleList samples; samples.size_ = num_messages; // Now we can create the DataSampleHeader struct and set its fields. OpenDDS::DCPS::DataSampleHeader header; // The +1 makes the null terminator ('\0') get placed into the block. header.message_id_ = 1; header.publication_id_ = this->pub_id_; OpenDDS::DCPS::DataSampleElement* prev_element = 0; OpenDDS::DCPS::DataSampleElementAllocator allocator(num_messages); Cleanup cleanup(allocator, samples); OpenDDS::DCPS::TransportSendElementAllocator trans_allocator(num_messages, sizeof (OpenDDS::DCPS::TransportSendElement)); std::string data = "Hello World!"; if (msg_size) { data.clear(); for (int j = 1; j <= msg_size; ++j) { data += char(1 + (j % 255)); } } for (int i = 1; i <= num_messages; ++i) { // This is what goes in the "Data Block". std::ostringstream ostr; ostr << data << " [" << i << "]"; std::string data_str = msg_size ? data : ostr.str(); ssize_t num_data_bytes = data_str.length() + 1; header.message_length_ = static_cast<ACE_UINT32>(num_data_bytes); header.sequence_ = i; // The DataSampleHeader is what goes in the "Header Block". ACE_Message_Block* header_block = new ACE_Message_Block(header.max_marshaled_size()); *header_block << header; ACE_Message_Block* data_block = new ACE_Message_Block(num_data_bytes); data_block->copy(data_str.c_str()); // Chain the "Data Block" to the "Header Block" header_block->cont(data_block); // Create the DataSampleElement now. OpenDDS::DCPS::DataSampleElement* element; ACE_NEW_MALLOC_RETURN(element, static_cast<OpenDDS::DCPS::DataSampleElement*>(allocator.malloc(sizeof (OpenDDS::DCPS::DataSampleElement))), OpenDDS::DCPS::DataSampleElement(this->pub_id_, this, 0, &trans_allocator, 0), 1); // The Sample Element will hold on to the chain of blocks (header + data). element->sample_ = header_block; if (prev_element == 0) { samples.head_ = element; } else { prev_element->set_next_send_sample(element); } prev_element = element; samples.tail_ = element; } VDBG((LM_DEBUG, "(%P|%t) DBG: " "Send the SendStateDataSampleList (samples).\n")); ACE_Time_Value start = ACE_OS::gettimeofday(); this->send(samples); ACE_Time_Value finished = ACE_OS::gettimeofday(); ACE_Time_Value total = finished - start; ACE_DEBUG((LM_INFO, "(%P|%t) Publisher total time required was %d.%d seconds.\n", total.sec(), total.usec() % 1000000)); VDBG((LM_DEBUG, "(%P|%t) DBG: " "The Publisher has finished sending the samples.\n")); if (msg_size) { ACE_OS::sleep(15); } return 0; }
int SimpleDataWriter::run(SimplePublisher* publisher) { DBG_ENTRY("SimpleDataWriter","run"); VDBG((LM_DEBUG, "(%P|%t) DBG: " "Build the DataSampleElementList to contain one element - " "our 'Hello World' string.\n")); // We just send one message. // This is what goes in the "Data Block". ACE_TString data = "Hello World!"; // Now we can create the DataSampleHeader struct and set its fields. TAO::DCPS::DataSampleHeader header; // The +1 makes the null terminator ('/0') get placed into the block. header.message_length_ = data.length() + 1; header.message_id_ = 1; header.sequence_ = 0; // TMB - Compiler no longer likes the next line... source_timestamp_ is gone. //header.source_timestamp_ = ACE_OS::gettimeofday().msec(); header.publication_id_ = this->pub_id_; // The DataSampleHeader is what goes in the "Header Block". ACE_Message_Block* header_block = new ACE_Message_Block (header.max_marshaled_size()); header_block << header; // The +1 makes the null terminator ('/0') get placed into the block. ACE_Message_Block* data_block = new ACE_Message_Block(data.length() + 1); data_block->copy(data.c_str()); // Chain the "Data Block" to the "Header Block" header_block->cont(data_block); // Create the DataSampleListElement now. TAO::DCPS::DataSampleListElementAllocator allocator(3); TAO::DCPS::TransportSendElementAllocator trans_allocator(3, sizeof (TAO::DCPS::TransportSendElement)); TAO::DCPS::DataSampleListElement* element; ACE_NEW_MALLOC_RETURN(element, static_cast<TAO::DCPS::DataSampleListElement*> (allocator.malloc(sizeof (TAO::DCPS::DataSampleListElement))), TAO::DCPS::DataSampleListElement(this->pub_id_, this, 0, &trans_allocator), 1); // The Sample Element will hold on to the chain of blocks (header + data). element->sample_ = header_block; // Set up the DataSampleList TAO::DCPS::DataSampleList samples; samples.head_ = element; samples.tail_ = element; samples.size_ = 1; VDBG((LM_DEBUG, "(%P|%t) DBG: " "Ask the publisher to send the DataSampleList (samples).\n")); publisher->send_samples(samples); VDBG((LM_DEBUG, "(%P|%t) DBG: " "The Publisher has finished sending the samples.\n")); return 0; }