void ThunkTests::testBlockingSend() { const size_t NUMINTS = 10; int nums[NUMINTS] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; if (isReceiver()) { receiverAssertIntsSorted(nums, NUMINTS); } else { for (size_t i = 0; i < NUMINTS; ++i) { nums[i] = htonl(nums[i]); } size_t next = 0; while (next < NUMINTS) { fprintf(stderr, "Sending int... "); int rc = cmm_send(data_sock, &nums[next], sizeof(int), 0, CMM_LABEL_BACKGROUND, NULL, NULL); if (rc >= 0) { fprintf(stderr, "sent, rc=%d\n", rc); CPPUNIT_ASSERT_EQUAL_MESSAGE("Integer sent", (int)sizeof(int), rc); next++; sleep(1); } else { fprintf(stderr, "failed! rc=%d, errno=%d\n", rc, errno); CPPUNIT_FAIL("Failed to send integer"); } } } }
void ThunkTests::testThunks() { const size_t NUMINTS = 10; int nums[NUMINTS] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; const int LARGE_BUF_SIZE = 1024*1024; char buf[LARGE_BUF_SIZE]; memset(buf, 'Q', sizeof(buf)); if (isReceiver()) { int rc = cmm_recv(data_sock, buf, LARGE_BUF_SIZE, MSG_WAITALL, NULL); CPPUNIT_ASSERT_EQUAL_MESSAGE("Received large buffer", LARGE_BUF_SIZE, rc); receiverAssertIntsSorted(nums, NUMINTS); } else { for (size_t i = 0; i < NUMINTS; ++i) { nums[i] = htonl(nums[i]); } int rc = cmm_write(data_sock, buf, LARGE_BUF_SIZE, CMM_LABEL_BACKGROUND, NULL, NULL); CPPUNIT_ASSERT_EQUAL_MESSAGE("Sent large buffer", LARGE_BUF_SIZE, rc); struct thunk_args *th_arg = new struct thunk_args(data_sock, nums, NUMINTS, 0); PthreadScopedLock lock(&th_arg->mutex); while (th_arg->next < th_arg->n) { th_arg->running = false; fprintf(stderr, "Sending int... "); rc = cmm_send(th_arg->sock, &th_arg->nums[th_arg->next], sizeof(int), 0, CMM_LABEL_BACKGROUND, thunk_fn, th_arg); if (rc >= 0) { fprintf(stderr, "sent, rc=%d\n", rc); CPPUNIT_ASSERT_EQUAL_MESSAGE("Integer sent", (int)sizeof(int), rc); th_arg->next++; rc = 0; th_arg->running = true; //sleep(1); } else { fprintf(stderr, "not sent, rc=%d\n", rc); CPPUNIT_ASSERT_EQUAL_MESSAGE("Deferred, not failed", CMM_DEFERRED, rc); while (!th_arg->running) { pthread_cond_wait(&th_arg->cv, &th_arg->mutex); } } } } }
bool MessageQueue::Receive(char *message,bool wait){ if (!isWorking()){ std::cout << "[Error] Not working!\n"; return false; } if (!isReceiver()){ std::cout << "[Error] I am not receiver! ["<<type<<"]\n"; return false; } int readSize=-1; if (readSize = msgrcv(MessageQueueId, &Buffer, MSG_SIZE, 1,(wait ? 0 : IPC_NOWAIT) ) < 0){ return false; } LastReadSize = readSize; for (int i=0;i<MSG_SIZE+1;i++){ message[i] = Buffer.mtext[i]; } return true; }