/** * This function sends message object over descriptor. * * Input parameters * fd descriptor to write message to * pMsg message object to be sent * Return value * returns MSG_SUCCESS of successful receive * or MSG_ERROR in case of failure. */ int send_msg(int fd, msg_t * pMsg) { if (!fd) { return MSG_ERROR; } //write the message over descriptor if (writen(fd, pMsg, MSG_SIZE(pMsg->length)) != MSG_SIZE(pMsg->length)) { dprintf("Failed to write data of size:%lu \n", MSG_SIZE(pMsg->length)); return MSG_ERROR; } return MSG_SUCCESS; }
void Connector::assembleMessage( const util::Buffer &b ) { int offset = 0; int msgSize, need; int rcvLen=b.length(); while (offset < rcvLen) { int sLen=_msg.length(); // How many bytes need for complete message? if (sLen > MESSAGE_HEADER) { // Stored bytes already have header msgSize = MSG_SIZE(_msg.buffer()); need = msgSize - sLen; } else if (MESSAGE_HEADER-sLen <= (rcvLen-offset)) { // Stored bytes don't have the header, but with bytes received complete header! _msg.append( b.buffer()+offset, MESSAGE_HEADER-sLen ); offset += MESSAGE_HEADER-sLen; sLen = MESSAGE_HEADER; msgSize = MSG_SIZE(_msg.buffer()); need = msgSize-MESSAGE_HEADER; } else { // Can't complete the header _msg.append( b.buffer()+offset, rcvLen-offset ); offset = rcvLen; continue; } assert( need >= 0 ); // Copy bytes int rest = (rcvLen-offset); int copy = (rest > need) ? need : rest; // printf( "[Connector (%p)] rcvLen=%d, sLen=%d, msgSize=%d, need=%d, rest=%d, copy=%d\n", // this, rcvLen, sLen, msgSize, need, rest, copy ); _msg.append( b.buffer()+offset, copy ); offset += copy; // printf( "[Connector (%p)] msgLen=%d\n", this, _msg.length() ); // Is message complete? if (msgSize == _msg.length()) { messageReceived(&_msg); _msg.resize(0); } } }
void send_message(int sock, unsigned short int from, unsigned short int to, const char *str) { msg_t msg; bzero(msg.text, 141); msg.orig_uid = from; msg.dest_uid = to; msg.type = MSG; int str_size = strlen(str) + 1; if (str_size > 141) { msg.text_len = 141; memcpy(msg.text, str, 140); msg.text[140] = '\0'; } else { msg.text_len = str_size; memcpy(msg.text, str, str_size); } char buffer[BUFF_LEN]; encode(buffer, msg); int write_resp = write (sock, buffer, MSG_SIZE(msg)); if (write_resp < 0) { perror("write()"); exit(EXIT_FAILURE); } else if (write_resp == 0) { // server hung up printf("server hung up!\n"); exit(EXIT_SUCCESS); } }
static int read_Command(HMMD_COMMAND **ret_cmd, WORKER_ENV *env) { HMMD_HEADER hdr; HMMD_COMMAND *cmd = NULL; int n; /* read the command header */ if (readn(env->fd, &hdr, sizeof(hdr)) == -1) { if (errno && errno != ECONNREFUSED) LOG_FATAL_MSG("read", errno); return eslEOD; } /* read the command data */ n = MSG_SIZE(&hdr); if ((cmd = malloc(n)) == NULL) LOG_FATAL_MSG("malloc", errno); memset(cmd, 0, n); /* avoid uninitialized bytes. remove this, if we ever serialize/deserialize structures properly */ cmd->hdr.command = hdr.command; cmd->hdr.length = hdr.length; if (hdr.length > 0) { if (readn(env->fd, &cmd->init, hdr.length) == -1) { if (errno && errno != ECONNREFUSED) LOG_FATAL_MSG("read", errno); return eslEOD; } } *ret_cmd = cmd; return eslOK; }
/* Fetch a message from queue 'q' and store it in 'm' */ int mbox_recv(int q, msg_t * m) { lock_acquire(&Q[q].l); print_trace("Recv", q, -1); /* If no messages available, wait until there is one */ while (Q[q].count == 0) { condition_wait(&Q[q].l, &Q[q].moreData); } /* copy header from mbox.buffer to m */ buffer_to_msg(Q[q].buffer, Q[q].tail, (char *) &m->size, MSG_T_HEADER_SIZE); /* Move tail to the body of message */ Q[q].tail = (Q[q].tail + MSG_T_HEADER_SIZE) % BUFFER_SIZE; /* Copy body of message from mbox.buffer to m->body */ buffer_to_msg(Q[q].buffer, Q[q].tail, (char *) &m->body[0], m->size); /* Move tail to the next message */ Q[q].tail = (Q[q].tail + MSG_SIZE(m) - MSG_T_HEADER_SIZE) % BUFFER_SIZE; /* Freeing space can satisy more than one writter */ condition_broadcast(&Q[q].moreSpace); Q[q].count--; lock_release(&Q[q].l); return 1; }
int unpack_item(uint16_t type, uint16_t id, void * inbuf, uint16_t length, tlv_t * outtlv) { uint32_t outsize = 0; int ret = 0; void * value = NULL; message_t * newmsg = NULL; value = malloc(length); if (NULL == value) { return -1; } switch (id) { case TLV_TYPE_INT8: case TLV_TYPE_UINT8: outsize = length; ret = pack_int8((uint8_t *)inbuf, (uint8_t *)value, &outsize); break; case TLV_TYPE_INT16: case TLV_TYPE_UINT16: outsize = length; ret = unpack_int16((uint16_t *)inbuf, (uint16_t *)value, &outsize); break; case TLV_TYPE_INT32: case TLV_TYPE_UINT32: outsize = length; ret = unpack_int32((uint32_t *)inbuf, (uint32_t *)value, &outsize); break; case TLV_TYPE_BYTES: outsize = length; ret = pack_bytes(inbuf, length, value, &outsize); break; case TLV_TYPE_MSG: newmsg = msg_unpack((uint8_t *)inbuf, length); if (NULL == newmsg) { free(value); return -1; } memcpy(value, newmsg, MSG_SIZE(newmsg)); free(newmsg); break; default: free(value); return -1; } if (0 != ret) { free(value); } outtlv->id = id; outtlv->type = type; outtlv->length = length; outtlv->value = value; return ret; }
/** * This function reads message object from descriptor * and returns message object. * * Input parameters * fd descriptor to read message from * Output parameters * pMsg message object * Return value * returns MSG_SUCCESS on succssesful receive * or MSG_ERROR in case of failure. */ int read_msg(int fd, msg_t ** pMsg) { if (!fd) { return MSG_ERROR; } //message payload length int msg_length; //read length of the message if (read(fd, &msg_length, LENGTH_SIZE) != LENGTH_SIZE) { dprintf("Failed to read length \n"); return MSG_ERROR; } //allocate memory for local buffer of message length size char *msg_buffer = (char *) malloc(MSG_SIZE(msg_length)); if (!msg_buffer) { dprintf ("Failed to allocate memory for local buffer of size %lu \n", MSG_SIZE(msg_length)); return MSG_ERROR; } //copy length to msg_buffer memcpy(msg_buffer, &msg_length, LENGTH_SIZE); //read entire message from the buffer if (readn (fd, msg_buffer + LENGTH_SIZE, MSG_SIZE(msg_length - LENGTH_SIZE)) != (MSG_SIZE(msg_length - LENGTH_SIZE))) { dprintf("failed to read %d bytes\n", msg_length); return MSG_ERROR; } //parse the buffer into message if (parse_msg(msg_buffer, msg_length, pMsg) != MSG_SUCCESS) { dprintf("failed to parse message\n"); return MSG_ERROR; } //clean up memory free(msg_buffer); return MSG_SUCCESS; }
static void process_Shutdown(HMMD_COMMAND *cmd, WORKER_ENV *env) { int n; n = MSG_SIZE(cmd); cmd->hdr.status = eslOK; if (writen(env->fd, cmd, n) != n) { LOG_FATAL_MSG("write error", errno); } }
int main(int argc, char **argv) { struct msg msg; int i; for (i = 0; i < 32; ++i) { msg.hdr = MSG_HDR(i, i); memset(msg.data, i, i); msg.chksum = msg_chksum(&msg); xwrite(STDOUT_FILENO, &msg.hdr, sizeof msg.hdr); xwrite(STDOUT_FILENO, msg.data, MSG_SIZE(msg.hdr)); xwrite(STDOUT_FILENO, &msg.chksum, sizeof msg.chksum); } return EXIT_SUCCESS; }
/* Insert 'm' into the mailbox 'q' */ int mbox_send(int q, msg_t * m) { int msgSize = MSG_SIZE(m); lock_acquire(&Q[q].l); print_trace("Send", q, msgSize); /* Wait until there is enough space */ while (space_available(&Q[q]) < msgSize) { condition_wait(&Q[q].l, &Q[q].moreSpace); } /* copy from message m (header and body) to Q[q].buffer */ msg_to_buffer((char *) m, msgSize, Q[q].buffer, Q[q].head); Q[q].head = (Q[q].head + msgSize) % BUFFER_SIZE; /* Send of one message can only satisfy one reader. */ condition_signal(&Q[q].moreData); Q[q].count++; lock_release(&Q[q].l); return 1; }
static void process_InitCmd(HMMD_COMMAND *cmd, WORKER_ENV *env) { char *p; int n; int status; if (env->hmm_db != NULL) p7_hmmcache_Close(env->hmm_db); if (env->seq_db != NULL) p7_seqcache_Close(env->seq_db); env->hmm_db = NULL; env->seq_db = NULL; /* load the sequence database */ if (cmd->init.db_cnt != 0) { P7_SEQCACHE *sdb = NULL; p = cmd->init.data + cmd->init.seqdb_off; status = p7_seqcache_Open(p, &sdb, NULL); if (status != eslOK) { p7_syslog(LOG_ERR,"[%s:%d] - p7_seqcache_Open %s error %d\n", __FILE__, __LINE__, p, status); LOG_FATAL_MSG("cache seqdb error", status); } /* validate the sequence database */ cmd->init.sid[MAX_INIT_DESC-1] = 0; if (strcmp (cmd->init.sid, sdb->id) != 0 || cmd->init.db_cnt != sdb->db_cnt || cmd->init.seq_cnt != sdb->count) { p7_syslog(LOG_ERR,"[%s:%d] - seq db %s: integrity error %s - %s\n", __FILE__, __LINE__, p, cmd->init.sid, sdb->id); LOG_FATAL_MSG("database integrity error", 0); } env->seq_db = sdb; } /* load the hmm database */ if (cmd->init.hmm_cnt != 0) { P7_HMMCACHE *hcache = NULL; p = cmd->init.data + cmd->init.hmmdb_off; status = p7_hmmcache_Open(p, &hcache, NULL); if (status != eslOK) { p7_syslog(LOG_ERR,"[%s:%d] - p7_hmmcache_Open %s error %d\n", __FILE__, __LINE__, p, status); LOG_FATAL_MSG("cache hmmdb error", status); } if ( (status = p7_hmmcache_SetNumericNames(hcache)) != eslOK){ p7_syslog(LOG_ERR,"[%s:%d] - p7_hmmcache_SetNumericNames %s error %d\n", __FILE__, __LINE__, p, status); LOG_FATAL_MSG("cache hmmdb error", status); } /* validate the hmm database */ cmd->init.hid[MAX_INIT_DESC-1] = 0; /* TODO: come up with a new pressed format with an id to compare - strcmp (cmd->init.hid, hdb->id) != 0 */ if (cmd->init.hmm_cnt != 1 || cmd->init.model_cnt != hcache->n) { p7_syslog(LOG_ERR,"[%s:%d] - hmm db %s: integrity error\n", __FILE__, __LINE__, p); LOG_FATAL_MSG("database integrity error", 0); } env->hmm_db = hcache; printf("Loaded profile db %s; models: %d memory: %" PRId64 "\n", p, hcache->n, (uint64_t) p7_hmmcache_Sizeof(hcache)); } /* if stdout is redirected at the commandline, it causes printf's to be buffered, * which means status logging isn't printed. This line strongly requests unbuffering, * which should be ok, given the low stdout load of hmmpgmd */ setvbuf (stdout, NULL, _IONBF, BUFSIZ); printf("Data loaded into memory. Worker is ready.\n"); setvbuf (stdout, NULL, _IOFBF, BUFSIZ); /* write back to the master that we are on line */ n = MSG_SIZE(cmd); cmd->hdr.status = eslOK; if (writen(env->fd, cmd, n) != n) { LOG_FATAL_MSG("write error", errno); } }