Ejemplo n.º 1
0
/**
 * Called by an application to receive data from remote peers
 * @param local
 * @param remote
 * @param buffer_ptr
 * @param buffer_size
 * @return The number of received bytes or -1 if some error occurred.
 */
int recv_from_peer(const struct nodeID *local, struct nodeID **remote, uint8_t *buffer_ptr, int buffer_size)
{
	int size;
	if (receivedBuffer[rIdxUp].data==NULL) {	//block till first message arrives
		wait4data(local, NULL, NULL);
	}

	assert(receivedBuffer[rIdxUp].data && receivedBuffer[rIdxUp].id);

	(*remote) = receivedBuffer[rIdxUp].id;
	// retrieve a msg from the buffer
	size = receivedBuffer[rIdxUp].len;
	if (size>buffer_size) {
		fprintf(stderr, "Net-helper : recv_from_peer: buffer too small (size:%d > buffer_size: %d)!\n",size,buffer_size);
		return -1;
	}
	memcpy(buffer_ptr, receivedBuffer[rIdxUp].data, size);
	free(receivedBuffer[rIdxUp].data);
	receivedBuffer[rIdxUp].data = NULL;
	receivedBuffer[rIdxUp].id = NULL;

	rIdxUp = (rIdxUp+1)%NH_BUFFER_SIZE;

//	fprintf(stderr, "Net-helper : I've got mail!!!\n");

	return size;
}
int UnixSocketAdapter::send_msg(const string &msg, const string &src, 
    const string &dst, int timeout)
{
  DBG("sending out serialized SER command:\n<<%s>>.\n", msg.c_str());

  // FIXME: is this init really needed?!
  //if(init(src) || sendto(dst, msg.c_str(),msg.length())){
  if(sendto(dst, msg.c_str(),msg.length())){
    ERROR("...while sending request to SER.\n");
    return -1;
  }

  if (timeout) { // should it collect the reply?
    if(wait4data(timeout) < 1){ 
      ERROR("while waiting for SER's response\n");
      return -1;
    }

    string status_line;
    if(cacheMsg() || getParam(status_line)) 
      return -1;

    unsigned int res_code;
    string res_reason;
    if(parse_return_code(status_line.c_str(),res_code,res_reason))
      return -1;

    if( (res_code < 200) ||
        (res_code >= 300) ) {
      ERROR("SER answered: %i %s\n", res_code,res_reason.c_str());
      return -1;
    }
  }

  return 0;

}