static void clearEntriesByType(uint32_t** entriesByType) { SharedBuffer* buf = SharedBuffer::bufferFromData(entriesByType); const size_t N = buf->size() / sizeof(entriesByType[0]); for (size_t i = 0; i < N; i++) { uint32_t* entries = entriesByType[i]; if (entries != NULL) { SharedBuffer::bufferFromData(entries)->release(); } } buf->release(); }
// ============================================================================ // main // ============================================================================ int main(int argc, char* argv[]) { yat::Message * m = 0; YAT_LOG_STATIC("Instanciating Task..."); Consumer * dt = new Consumer(kLO_WATER_MARK, kHI_WATER_MARK); YAT_LOG_STATIC("Starting Task..."); try { dt->go(2000); } catch (const yat::Exception&) { YAT_LOG_STATIC("yat exception caught - could not start task. aborting..."); dt->exit(); return 0; } catch (...) { YAT_LOG_STATIC("unknown exception caught - could not start task. aborting..."); dt->exit(); return 0; } /* for (size_t i = 0; i < kNUM_MSGS; i++) { try { //- post msg to consumer dt->post(new yat::Message(kDUMMY_MSG), kPOST_MSG_TMO); //- simulate some time consuming activity yat::ThreadingUtilities::sleep(0, 100000); } catch (const std::bad_alloc&) { YAT_LOG_STATIC("std::bad_alloc except. caught - could not post msg#" << i); } catch (const yat::Exception&) { YAT_LOG_STATIC("tango except. caught - could not post msg#" << i); } catch (...) { YAT_LOG_STATIC("unknown except. caught - could not post msg#" << i); } } */ yat::Buffer<double> data(kNUM_MSGS); for ( size_t i = 0; i < kNUM_MSGS; i++ ) data[i] = 1. * i; data.force_length(kNUM_MSGS); for (size_t i = 0; i < kNUM_MSGS; i++) { try { SharedBuffer* sb = new SharedBuffer(); sb->capacity(i + 1); sb->memcpy(data.base(), i + 1); std::cout << "SharedBuffer* sb.length = " << sb->length() << std::endl; dt->post(kDATA_MSG, sb->duplicate(), false); sb->release(); //- simulate some time consuming activity yat::ThreadingUtilities::sleep(0, 100000); } catch (const std::bad_alloc&) { YAT_LOG_STATIC("std::bad_alloc except. caught - could not post msg#" << i); } catch (const yat::Exception&) { YAT_LOG_STATIC("tango except. caught - could not post msg#" << i); } catch (...) { YAT_LOG_STATIC("unknown except. caught - could not post msg#" << i); } } try { dt->exit(); } catch (const yat::Exception&) { YAT_LOG_STATIC("tango except. caught - could stop task. aborting..."); } catch (...) { YAT_LOG_STATIC("unknown except. caught - could stop task. aborting..."); return 0; } return 0; }
// ============================================================================ // Consumer::handle_message // ============================================================================ void Consumer::handle_message (yat::Message& _msg) { //- YAT_TRACE("Consumer::handle_message"); //- handle msg switch (_msg.type()) { //- TASK_INIT ---------------------- case yat::TASK_INIT: { //- "initialization" code goes here YAT_LOG("Consumer::handle_message::TASK_INIT::task is starting up"); this->ctrl_msg_counter++; } break; //- TASK_EXIT ---------------------- case yat::TASK_EXIT: { //- "release" code goes here YAT_LOG("Consumer::handle_message::TASK_EXIT::task is quitting"); this->ctrl_msg_counter++; } break; //- TASK_PERIODIC ------------------ case yat::TASK_PERIODIC: { //- code relative to the task's periodic job goes here YAT_LOG("Consumer::handle_message::handling TASK_PERIODIC msg"); } break; //- TASK_TIMEOUT ------------------- case yat::TASK_TIMEOUT: { //- code relative to the task's tmo handling goes here YAT_LOG("Consumer::handle_message::handling TASK_TIMEOUT msg"); } break; //- USER_DEFINED_MSG ----------------- case kDUMMY_MSG: { //- YAT_LOG("Consumer::handle_message::handling kDUMMY_MSG user msg"); this->user_msg_counter++; #if defined (YAT_DEBUG) if (_msg.id() < last_msg_id) this->wrong_order_msg_counter++; else this->lost_msg_counter += _msg.id() - (this->last_msg_id + 1); #endif //- simulate some time consuming activity yat::ThreadingUtilities::sleep(0, 10000); } break; //- kDATA_MSG ------------------- case kDATA_MSG: { //- code relative to the task's tmo handling goes here SharedBuffer* sb = _msg.detach_data<SharedBuffer>(); YAT_LOG("got a SharedBuffer containing " << sb->length() << " elements"); sb->release(); } break; default: YAT_LOG("Consumer::handle_message::unhandled msg type received"); break; } #if defined (YAT_DEBUG) this->last_msg_id = _msg.id(); #endif }
void FileIoWriteStream_test() { printf( "> FileIoWriteStream_test()\n" ); printf( "opening " ); #ifdef WIN32 const char *testFileName = "..\\..\\..\\FileIoWriteStream_test_output.txt"; #else const char *testFileName = "../../../FileIoWriteStream_test_output.txt"; #endif // in msvc this is a path relative to the project directory: SharedBuffer *path = SharedBufferAllocator::alloc(testFileName); READSTREAM *fp = FileIoWriteStream_open(path, FileIoRequest::READ_WRITE_OVERWRITE_OPEN_MODE); path->release(); assert( fp != 0 ); while (FileIoWriteStream_pollState(fp) == STREAM_STATE_OPENING) { printf("."); Sleep(10); } printf( "\ndone.\n" ); assert( FileIoWriteStream_pollState(fp) == STREAM_STATE_OPEN_IDLE ); printf( "seeking " ); FileIoWriteStream_seek(fp, 0); while (FileIoWriteStream_pollState(fp) == STREAM_STATE_OPEN_BUFFERING) { printf("."); Sleep(10); } assert( FileIoWriteStream_pollState(fp) == STREAM_STATE_OPEN_STREAMING ); printf( "\ndone.\n" ); printf( "writing:\n" ); const int lineCount = 100000; // write 1000 integers in text from 0 to lineCount, one per line for (int i=0; i < lineCount; ++i) { char s[128]; std::sprintf(s, "%d\n", i); size_t bytesToWrite = std::strlen(s); // write bytes out one at a time because the current implementation of write requires items to be block-aligned for (size_t j=0; j <bytesToWrite; ++j) { FileIoWriteStream_write( &s[j], 1, 1, fp ); } } printf( "\nclosing.\n" ); FileIoWriteStream_close(fp); // verify the test file contents { printf( "\nverifying...\n" ); Sleep(1000); // pray that file has been closed by now FILE *fp = std::fopen(testFileName, "rt"); assert( fp != 0 ); // read 1000 lines, verify that each contains the integers in [0,lineCount) for (int i=0; i < lineCount; ++i) { char s[1024]; std::fgets(s, 128, fp); int value = -1; std::sscanf(s, "%d", &value); assert( value == i ); } std::fclose(fp); } printf( "\ndone.\n" ); printf( "< FileIoWriteStream_test()\n" ); }