bool sendMessage(const char *msg, zmq::socket_t &sock, std::string &response, int32_t timeout_us) { char tnam[100]; int pgn_rc = pthread_getname_np(pthread_self(),tnam, 100); assert(pgn_rc == 0); //NB_MSG << tnam << " sendMessage " << msg << "\n"; safeSend(sock, msg, strlen(msg)); char *buf = 0; size_t len = 0; if (safeRecv(sock, &buf, &len, true, (int64_t)timeout_us)) { response = buf; return true; } return false; }
void wait4Register(cli *client, blist messages, bstring incompleteMessage){ incompleteMessage = incompleteMessage ? incompleteMessage : safe_BfromCstr(""); /*recv messages until you receive a nick and user msg*/ bstring raw; int i = handleRegisterMessages(messages, client); while (!(client->nick && client->user)){ raw = safeRecv(client); messages = extractMessages(raw, &incompleteMessage); i = handleRegisterMessages(messages, client); } /*trim message list to be the list of messages recv'd*/ /*after the client completed registration*/ blistDeleteRange(messages, 0, i); /*complete client registration*/ registerClient(client, messages, incompleteMessage); }
bool sendMessage(const char *msg, zmq::socket_t &sock, std::string &response, int32_t timeout_us, MessageHeader header) { char tnam[100]; int pgn_rc = pthread_getname_np(pthread_self(),tnam, 100); assert(pgn_rc == 0); //{FileLogger fl(program_name); fl.f() << tnam << " sendMessage " << msg << "\n"; } safeSend(sock, msg, strlen(msg), header); char *buf; size_t len; MessageHeader response_header; //bool safeRecv(zmq::socket_t &sock, char **buf, size_t *response_len, bool block, uint64_t timeout, int *source) { if (safeRecv(sock, &buf, &len, true, (int64_t)timeout_us, response_header)) { response = buf; return true; } return false; }
void handleUser(cli *client, blist leftOverMessages, bstring incompleteMessage){ /*take care of any messages left over from registration*/ if (leftOverMessages->qty){ handleMessages(leftOverMessages, client); } bstrListDestroy(leftOverMessages); blist messages; bstring raw; incompleteMessage = incompleteMessage ? incompleteMessage : safe_BfromCstr(""); /*recv messages until you receive a nick and user msg*/ while (1){ raw = safeRecv(client); messages = extractMessages(raw, &incompleteMessage); if (messages){ handleMessages(messages, client); bstrListDestroy(messages); } } /*exit pthread*/ }
void handleClient(param* p) { printf("Client connected!:\n"); unsigned int size; char* song; safeRecv(p->sock, &size, sizeof(int), 0); size = ntohl(size); song = malloc(size+1); safeRecv(p->sock, song, size+1, 0); printf("Requested song:%s \n", song); char* fullSongPath = NULL; findFile(song, MUSIC_DIR, &fullSongPath); if (fullSongPath == NULL) { printf("Could not find song!\n"); close(p->sock); return; } printf("Playing song:%s\n", fullSongPath); fflush(stdout); int fd = 0; if ((fd = open(fullSongPath, O_RDONLY)) > 0) { free(song); free(fullSongPath); struct stat buf; fstat(fd, &buf); off_t size = htonl(buf.st_size); printf("Song has size of:%lu\n", buf.st_size); safeSend(p->sock, &size, sizeof(off_t), 0); char buffer[BUFFER_SIZE]; size_t total = 0; int bufferTotal = 0; do { //Fill the buffer bufferTotal = 0; int currentRead = 0; while(bufferTotal < BUFFER_SIZE) { currentRead = read (fd, buffer + bufferTotal, BUFFER_SIZE - bufferTotal); if (currentRead < 0) { perror("Could not read file:"); break; } if (currentRead == 0) { bufferTotal = 0; break; } bufferTotal += currentRead; } total += bufferTotal; safeSend(p->sock, buffer, bufferTotal, 0); printf("Sent so far: %lu out of %lu\n", total, buf.st_size); } while (bufferTotal > 0); } else { perror("Could not open song:"); } size = htonl(size); safeSend(p->sock, &size, sizeof(int), 0); free(p); }
Action::Status SyncRemoteStatesAction::execute() { Channel *chn = dynamic_cast<Channel*>(owner); if (internals->process_state == SyncRemoteStatesActionInternals::ps_init) { owner->start(this); status = Running; #if 0 if (!internals->sock){ internals->sock = new zmq::socket_t(*MessagingInterface::getContext(), ZMQ_PAIR); internals->sock->bind("inproc://syncstates"); chn->addSocket(10, "inproc://syncstates"); } #endif //assert(chn); //assert(chn == internals->chn); internals->process_state = SyncRemoteStatesActionInternals::ps_sending_messages; if (chn->syncRemoteStates(internals->messages)) { internals->iter = new std::list<char*>::iterator(internals->messages.begin()); if (*internals->iter != internals->messages.end()) internals->message_state = SyncRemoteStatesActionInternals::e_sending; else internals->message_state = SyncRemoteStatesActionInternals::e_done; } else{ status = Failed; error_str = "Failed to sync"; owner->stop(this); return status; } } if (internals->process_state == SyncRemoteStatesActionInternals::ps_sending_messages) { if (*internals->iter == internals->messages.end()) { internals->message_state = SyncRemoteStatesActionInternals::e_done; } if (internals->message_state == SyncRemoteStatesActionInternals::e_sending) { char *current_message = *(*internals->iter); internals->header.needReply(false); safeSend(*internals->sock, current_message, strlen(current_message), internals->header); free( current_message ); *internals->iter = internals->messages.erase(*internals->iter); internals->message_state = SyncRemoteStatesActionInternals::e_receiving; // skip receiving temporarily internals->message_state = SyncRemoteStatesActionInternals::e_done; } else if (internals->message_state == SyncRemoteStatesActionInternals::e_receiving) { char *buf; size_t len; if (safeRecv(*internals->sock, &buf, &len, false, 0, internals->header)) { //NB_MSG << "got reply: " << buf << "\n"; internals->message_state = SyncRemoteStatesActionInternals::e_done; delete[] buf; } else return Running; } if (internals->message_state == SyncRemoteStatesActionInternals::e_done) { if (*internals->iter != internals->messages.end()) { internals->message_state = SyncRemoteStatesActionInternals::e_sending; } else { if (internals->iter) {delete internals->iter; internals->iter =0; } } if (!internals->iter) { // finished sending messages if (internals->process_state == SyncRemoteStatesActionInternals::ps_sending_messages) { //safeSend(*cmd_client, "done", 4); std::string ack; //MessageHeader mh(ChannelInternals::SOCK_CTRL, ChannelInternals::SOCK_CTRL, false); //sendMessage("done", *cmd_client, ack, mh); internals->header.dest = MessageHeader::SOCK_CTRL; internals->header.dest = MessageHeader::SOCK_CTRL; internals->header.needReply(true); safeSend(*internals->sock, "done", 4, internals->header); internals->process_state = SyncRemoteStatesActionInternals::ps_waiting_ack; } } } } else if (internals->process_state == SyncRemoteStatesActionInternals::ps_waiting_ack) { char *ack; size_t len; if (safeRecv(*internals->sock, &ack, &len, false, 0, internals->header)) { DBG_CHANNELS << "channel " << chn->name << " got " << ack << " from server\n"; status = Complete; result_str = (const char *)ack; // force a new allocation delete[] ack; owner->stop(this); // execute a state change once all other actions are if (chn->isClient()) { SetStateActionTemplate ssat(CStringHolder("SELF"), "ACTIVE" ); SetStateAction *ssa = (SetStateAction*)ssat.factory(chn); chn->enqueueAction(ssa); } else { SetStateActionTemplate ssat(CStringHolder("SELF"), "DOWNLOADING" ); chn->enqueueAction(ssat.factory(chn)); } return status; } } return status; #if 0 std::stringstream ss; ss << owner->getName() << " failed to find machine " << target.get() << " for SetState action" << std::flush; std::string str = ss.str(); error_str = strdup(str.c_str()); status = Failed; owner->stop(this); return status; #endif }