예제 #1
0
파일: zmsg.hpp 프로젝트: 0000-bigtree/nscp
   bool recv(zmq::socket_t & socket) {
      clear();
      while(1) {
         zmq::message_t message(0);
         try {
            if (!socket.recv(&message, 0)) {
               return false;
            }
         } catch (zmq::error_t error) {
            //std::cout << "E: " << error.what() << std::endl;
            return false;
         }
         char *data = reinterpret_cast<char*>(message.data());
         if (message.size() == 17 && data[0] == 0) {
            push_back(encode_uuid(msg_to_string(message)));
         } else {
            push_back(msg_to_string(message));
         }
		 boost::int64_t more;
         size_t more_size = sizeof(more);
         socket.getsockopt(ZMQ_RCVMORE, &more, &more_size);
         if (!more) {
            break;
         }
      }
      return true;
   }
예제 #2
0
inline bool recv (zmq::socket_t & socket, tile_protocol & tile)
{
   zmq::message_t msg;
   socket.recv(&msg);
   std::string buf(static_cast<char*>(msg.data()),msg.size());
   return unserialise(buf, tile);
}
예제 #3
0
//  Receive 0MQ string from socket and convert into string
static std::string
s_recv (zmq::socket_t & socket) {

    zmq::message_t message;
    socket.recv(&message);
    return std::string(static_cast<char*>(message.data()), message.size());
}
예제 #4
0
void process_frontend(zmq::socket_t& frontend, int& available_workers) {
  zmq::message_t req;
  frontend.recv(&req);
  zmq::message_t reply(sizeof(available_workers));
  memcpy(reply.data(), &available_workers, sizeof(available_workers));
  frontend.send(reply);
}
예제 #5
0
bool recursive_receive(zmq::socket_t& receiving_socket, zmq::message_t& message,
                       const REQ& req, RES& response, bool& succeed) {
  bool rc = receiving_socket.recv(&message);

  if (rc) {
    auto serialized_resp = kZmqUtil->message_to_string(message);
    response.ParseFromString(serialized_resp);

    if (req.request_id() == response.response_id()) {
      succeed = true;
      return false;
    } else {
      return true;
    }
  } else {
    // timeout
    if (errno == EAGAIN) {
      succeed = false;
    } else {
      succeed = false;
    }

    return false;
  }
}
예제 #6
0
// 0 for received nothing
int recv_msg_async(zmq::socket_t &sock, boost::shared_array<uint8_t> &data)
{
    zmq::message_t msgt;
    int nbytes;
    try{
      nbytes = sock.recv(&msgt, ZMQ_DONTWAIT);
    }catch(zmq::error_t e){
      return -1;
    }

    if(nbytes == 0){
      if(zmq_errno() == EAGAIN)
	return 0;
      else
	return -1;
    }

    size_t len = msgt.size();    
    uint8_t *dataptr;
    try{
      dataptr = new uint8_t[len];
    }catch(std::bad_alloc e){
      return -1;
    }
    
    memcpy(dataptr, msgt.data(), len);
    data.reset(dataptr);
    return len;
}
예제 #7
0
/*
 * return number of bytes received, negative if error, 0 for received nothing, which should be treated as error
 */
int recv_msg(zmq::socket_t &sock, boost::shared_array<uint8_t> &data)
{
    zmq::message_t msgt;
    int nbytes;
    try{
      nbytes = sock.recv(&msgt);
    }catch(zmq::error_t e){
      //LOG(NOR, stderr, "recv failed: %s\n", e.what());
      return -1;
    }

    if(nbytes == 0) return 0;

    size_t len = msgt.size();    
    uint8_t *dataptr;
    try{
      dataptr = new uint8_t[len];
    }catch(std::bad_alloc e){
      //LOG(DBG, stderr, "can not allocate memory!\n");
      return -1;
    }
    
    memcpy(dataptr, msgt.data(), len);
    data.reset(dataptr);
    return len;
}
예제 #8
0
void
raw_sender(zmq::socket_t& s)
{
  for (size_t i = 0; i < ITERS; ++i)
  {
    zmq::message_t msg1(ARRAY_LEN(PART1)-1);
    memcpy(msg1.data(), PART1, msg1.size());
    s.send(msg1, ZMQ_SNDMORE);

    zmq::message_t msg2(ARRAY_LEN(PART2)-1);
    memcpy(msg2.data(), PART2, msg2.size());
    s.send(msg2, ZMQ_SNDMORE);

    zmq::message_t msg3(ARRAY_LEN(PART3)-1);
    memcpy(msg3.data(), PART3, msg3.size());
    s.send(msg3);

    zmq::message_t msg_res;
    s.recv(&msg_res, 0);

    if (i % 1000 == 0)
    {
      std::cout << ".";
      std::cout.flush();
    }
  }
}
예제 #9
0
  /*
   * return number of bytes received, negative if error, 0 for received nothing,
   *  which should be treated as error
   */
  int32_t RecvMsg(zmq::socket_t &sock, boost::shared_array<uint8_t> &data,
      bool *_more){
    zmq::message_t msgt;
    int nbytes;
    try{
      nbytes = sock.recv(&msgt);
    }catch(zmq::error_t &e){
      LOG(ERROR) << "RecvMsg error = " << e.what();
      return -1;
    }

    if(nbytes == 0) return 0;

    size_t len = msgt.size();    
    uint8_t *dataptr;
    try{
      dataptr = new uint8_t[len];
    }catch(std::bad_alloc &e){
      return -1;
    }
    
    memcpy(dataptr, msgt.data(), len);
    data.reset(dataptr);

    if(_more != NULL){
      int more_recv;
      size_t s = sizeof(int);
      sock.getsockopt(ZMQ_RCVMORE, &more_recv, &s);
      *_more = (more_recv == 1) ? true : false;
    }
    return len;
  }
예제 #10
0
void handleOp(zmq::socket_t &socket, int opCode) {
    std::string s_req, trace, line;
    std::cout<<"Trace: ";
    std::getline(std::cin, trace);
    Request req;
    Id id;
    Person person;
    Request_Header_Type type;
    switch (opCode) {
        case 1:
            type = Request_Header_Type_LOOKUP;
            std::cout<<"id: ";
            line.clear();
            std::getline(std::cin, line);
            id = makeId(fromString<int>(line));
            req = makeRequest(type, id, person, false, trace);
            break;
        case 3:
            type = Request_Header_Type_REMOVE;
            std::cout<<"id: ";
            line.clear();
            std::getline(std::cin, line);
            id = makeId(fromString<int>(line));
            req = makeRequest(type, id, person, false, trace);
            break;
        case 2:
            {
                type = Request_Header_Type_INSERT;
                std::cout<<"id: ";
                line.clear();
                std::getline(std::cin, line);
                id = makeId(fromString<int>(line));
                line.clear();
                std::cout<<"Name: ";
                std::getline(std::cin, line);
                std::string ph, ovr;
                std::cout<<"Phone: ";
                std::getline(std::cin, ph);
                std::cout<<"Overwrite: (y/n) ";
                std::getline(std::cin, ovr);
                person = makePerson(line, ph);
                req = makeRequest(type, id, person, ovr.compare("y") == 0,
                        trace);
                break;
            }
    };
    std::cout<<"Request: "<<requestToString(req);
    TRACE(std::cout, "");
    req.SerializeToString(&s_req);
    zmq::message_t z_req(s_req.size());
    memcpy(z_req.data(), (void *)s_req.data(), s_req.size());
    socket.send(z_req);
    zmq::message_t z_resp;
    socket.recv(&z_resp);
    std::string s_resp((char *)z_resp.data(), z_resp.size());
    Response resp;
    resp.ParseFromString(s_resp);
    std::cout<<"Response = "<<responseToString(resp);
}
예제 #11
0
void receiveStr(zmq::socket_t& socket, string& s, int flags = 0) {
	zmq::message_t request;
	if (!socket.recv(&request, flags)) {
		std::cerr << "Reception KO" << std::endl;
	}
	
	s.append((char const *) request.data(), request.size());
}
예제 #12
0
void _receive(zmq::socket_t & socket, std::list<zmq::message_t*> *result) {
    do {
        zmq::message_t * msg = new zmq::message_t();
        // TODO: error handling
        socket.recv(msg);
        result->push_back(msg);
    } while (sockopt_rcvmore(socket));
}
예제 #13
0
unsigned s_recv (zmq::socket_t & socket, std::string& buffer) {

    zmq::message_t message;
    socket.recv(&message);
    buffer.clear();
    buffer.assign(static_cast<char*>(message.data()), message.size());
   
    return message.size();
}
예제 #14
0
// capacity request
void process_status_request(zmq::socket_t& status, int& number_of_workers) {
  // null request
  zmq::message_t incoming_msg;
  status.recv(&incoming_msg);

  // send current capacity
  zmq::message_t status_msg(sizeof(number_of_workers));
  memcpy(status_msg.data(), &number_of_workers, sizeof(number_of_workers));
  status.send(status_msg);
}
예제 #15
0
    static bool recv_zmq_message(zmq::socket_t& sock,
                                 zmq::message_t& msg,
                                 T& object,
                                 int flags = ZMQ_NOBLOCK)
    {
        if (!sock.recv(&msg, flags)) {
            return false;
        }

        memcpy(&object, msg.data(), msg.size());
        return true;
    }
예제 #16
0
파일: server.cpp 프로젝트: brobits/uRPC
bool Server::getRequest (zmq::socket_t &socket, pb::RequestEnvelope &envelope) {
  long long more;
  size_t sz = sizeof (more);
  zmq::message_t request;
  
  socket.recv (&request);
  socket.getsockopt (ZMQ_RCVMORE, &more, &sz);
  bool isAnotherRequest = more != 0;
  envelope.ParseFromArray (request.data(), request.size());
  
  return isAnotherRequest;
}
예제 #17
0
    /** Receive a string. */
    inline bool recv(zmq::socket_t &s, std::string &v, int flags) 
    {
      zmq::message_t msg;
      
      IB_FIRST_PART(s.recv(&msg, flags));
      
      v.assign(
        static_cast<char*>(msg.data()), 
        static_cast<char*>(msg.data()) + msg.size()); 

      return true;
    }
예제 #18
0
bool ServerInterface::SendMove(enum Tetromino::Move move, int pieceId, zmq::socket_t& commandSocket) const {
    std::string const* moveCommandS = m_factory.CreateMoveMessage(move, pieceId);
    zmq::message_t moveCommand (moveCommandS->size());
    memcpy(static_cast<void*>(moveCommand.data()), moveCommandS->data(), 
                                moveCommandS->size());
    delete moveCommandS;
    commandSocket.send(moveCommand);

    zmq::message_t reply;
    commandSocket.recv(&reply);
    std::string replyS (static_cast<char const*>(reply.data()), reply.size());
    return m_factory.ParseMoveReply(replyS);
}
예제 #19
0
    inline bool recv(zmq::socket_t &s, T &v, int flags) 
    {
      zmq::message_t msg;
      IB_FIRST_PART(s.recv(&msg, flags));
      
      in_memory_buffer mb(static_cast<char*>(msg.data()), msg.size());
      std::istream is(&mb);
      is >> v;    

      IB_ASSERT(!is.fail(), ib_error::ECONVERSION);

      return true;
    }
예제 #20
0
std::vector<zmq::message_t> CentralProxy::receive_multipart(zmq::socket_t& socket_fe)
{
    std::vector<zmq::message_t> message;
    bool more = true;
    while (more)
    {
        message.push_back(zmq::message_t());
        zmq::message_t& msg = message.back();
        socket_fe.recv(&msg);
        more = msg.more();
    }
    return message;
}
예제 #21
0
bool
nutils::recv_zmq_message(zmq::socket_t& sock,
						 zmq::message_t& msg,
						 std::string& str,
						 int flags)
{
    if (!sock.recv(&msg, flags)) {
        return false;
    }

    str.clear();
    str.append(reinterpret_cast<char*>(msg.data()), msg.size());
    return true;
}
예제 #22
0
파일: zmq.cpp 프로젝트: plq/zmq
void recv_multipart(zmq::socket_t &socket) {
    int64_t more=-1;
    size_t more_size = sizeof (more);
    while(more) {
        zmq::message_t response;
        socket.recv(&response);

        std::string response_str((const char *)response.data(),
                                                             response.size());
        cout << response_str << endl;

        socket.getsockopt(ZMQ_RCVMORE, &more, &more_size);
    }
}
예제 #23
0
bool safeRecv(zmq::socket_t &sock, char *buf, int buflen, bool block, size_t &response_len, int64_t timeout) {
	char tnam[100];
	int pgn_rc = pthread_getname_np(pthread_self(),tnam, 100);
	assert(pgn_rc == 0);

	//{FileLogger fl(program_name); fl.f() << tnam << " receiving\n";}

	response_len = 0;
	int retries = 5;
	if (block && timeout == 0)
		timeout = 500;
	while (!MessagingInterface::aborted()) {
		try {
			zmq::pollitem_t items[] = { { (void*)sock, 0, ZMQ_POLLERR | ZMQ_POLLIN, 0 } };
			int n = zmq::poll( &items[0], 1, timeout);
			if (!n && block) {
				usleep(10); continue;
			}
			if (items[0].revents & ZMQ_POLLIN) {
				//{FileLogger fl(program_name); fl.f() << tnam << " safeRecv() collecting data\n"; }
				response_len = sock.recv(buf, buflen, ZMQ_DONTWAIT);
				if (response_len > 0 && response_len < (unsigned int)buflen) {
					buf[response_len] = 0;
					//if (response_len>10){FileLogger fl(program_name); fl.f() << tnam << " saveRecv() collected data '" << buf << "' with length " << response_len << "\n"; }
				}
				else {
					//if (response_len > 10){FileLogger fl(program_name); fl.f() << tnam << " saveRecv() collected data with length " << response_len << "\n"; }
				}
				if (!response_len && block) continue;
			}
			return (response_len == 0) ? false : true;
		}
		catch (zmq::error_t e) {
			{
				FileLogger fl(program_name); 
				fl.f() << tnam << " safeRecv error " << errno << " " << zmq_strerror(errno) << "\n";
			}
			if (--retries == 0) {
				exit(EXIT_FAILURE);
			}
			if (errno == EINTR) { std::cerr << "interrupted system call, retrying\n"; 
				if (block) continue;
			}
			usleep(10);
			return false;
		}
	}
	return false;
}
예제 #24
0
// relay data from workers to frontend
void process_backend(zmq::socket_t& frontend, zmq::socket_t& backend) {
  int64_t more;
  size_t moresz;
  moresz = sizeof(more);

  while(true) {
    zmq::message_t worker_msg;
    backend.recv(&worker_msg);
    backend.getsockopt(ZMQ_RCVMORE, &more, &moresz);
    frontend.send(worker_msg, more ? ZMQ_SNDMORE : 0);
    if(!more) {
      break;
    }
  }
}
예제 #25
0
 /**
  * Try to receive message part from socket.
  * @return false if would block
  */
 inline bool
 try_recv_msg(zmq::socket_t& sock, zmq::message_t& msg,
   int flags = ZMQ_NOBLOCK) throw(ZmqErrorType)
 {
   bool res = false;
   try
   {
     res = sock.recv(&msg, flags);
   }
   catch (const zmq::error_t& e)
   {
     throw_zmq_exception(e);
   }
   return res;
 }
예제 #26
0
 /**
  * Receive message part from socket
  */
 inline void
 recv_msg(zmq::socket_t& sock, zmq::message_t& msg,
   int flags = 0) throw(ZmqErrorType)
 {
   try
   {
     if (!sock.recv(&msg, flags))
     {
       throw zmq::error_t();
     }
   }
   catch (const zmq::error_t& e)
   {
     throw_zmq_exception(e);
   }
 }
예제 #27
0
bool
nutils::recv_zmq_message(zmq::socket_t& sock,
						 zmq::message_t& msg,
						 msgpack::object& obj,
						 int flags)
{
    if (!sock.recv(&msg, flags)) {
        return false;
    }

    msgpack::unpacked unpacked;
    msgpack::unpack(&unpacked, reinterpret_cast<const char*>(msg.data()), msg.size());
    obj = unpacked.get();

    return true;
}
 int
 relay_raw(zmq::socket_t& src, zmq::socket_t& dst, bool check_first_part)
 {
   int relayed = 0;
   for (
     bool more = check_first_part ? has_more(src) : true;
     more; ++relayed)
   {
     zmq::message_t cur_part;
     src.recv(&cur_part);
     more = has_more(src);
     int flag = more ? ZMQ_SNDMORE : 0;
     send_msg(dst, cur_part, flag);
   }
   return relayed;
 }
예제 #29
0
 std::string receiveString(zmq::socket_t & socket)
 {
   zmq::message_t message;
   std::string result = "";
   try
   {
     socket.recv(&message);
     result = std::string(static_cast<char*>(message.data()), message.size());
   }
   catch(const zmq::error_t& e) 
   {
     VLOG(1) << "ZMQ receive has thrown with type " << e.what();
     throw;
   }
   return result;
 }
예제 #30
0
/*
 * return number of bytes received, negative if error
 */
int recv_msg(zmq::socket_t &sock, commtest::cliid_t &cid, boost::shared_array<uint8_t> &data)
{
    zmq::message_t msgt;
    try{
      sock.recv(&msgt);
    }catch(zmq::error_t e){
      //LOG(NOR, stderr, "recv cid failed: %s\n", e.what());
      return -1;
    }

    size_t len = msgt.size();
    if(len != sizeof(commtest::cliid_t)) return -1;

    cid = *((commtest::cliid_t *) msgt.data());
    //LOG(DBG, stderr, "received id = 0x%x\n", cid);

    return recv_msg(sock, data);
}