camera_t(zmq::context_t& context, const std::string& endpoint, const std::string& uuid, const std::string& www_dir): socket(context, ZMQ_REQ), uuid(uuid), photo_count(0), settings(), next(), wait_until(0), www_dir(www_dir) { photo_count = record_count(www_dir + "/cameras/" + uuid + ".db"); while(this->uuid.size() && this->uuid.back() == '\0') this->uuid.pop_back(); socket.connect(endpoint.c_str()); }
bool connect(const std::string &name, zmq::socket_t &socket, const std::string &endpoint) { const int max_tries = 8; log(name, boost::format("Connecting to the proxy: %1%...") % endpoint); for (int i = 0; i < max_tries; ++i) { try { socket.connect(endpoint.c_str()); } catch (zmq::error_t e) { boost::format fmt("Failed to connect to the proxy: %1%"); log(name, fmt % zmq_strerror(zmq_errno())); continue; } boost::format fmt("Connected to the proxy: %1%"); log(name, fmt % endpoint); return true; } return false; }
bool ServerInterface::ConnectToCommandServer(zmq::socket_t& commandSocket) const { commandSocket.connect(m_commandServer.c_str()); std::cout << "Connecting to command server... "; std::string const* requestS = m_factory.CreateInitMessage(m_matchToken); zmq::message_t request (requestS->size()); memcpy(static_cast<void*>(request.data()), requestS->data(), requestS->size()); delete requestS; commandSocket.send(request); zmq::message_t reply; commandSocket.recv(&reply); std::string replyS (static_cast<char const*>(reply.data()), reply.size()); bool success = m_factory.ParseInitReply(replyS); if (success) std::cout << "Success!" << std::endl; else std::cout << "Failure." << std::endl; return success; }
void worker::run () { try { assert(_context != NULL); assert(_connectAddr.c_str() != NULL); fprintf(stdout, "worker: connect address: %s\n", _connectAddr.c_str()); fprintf(stdout, "worker: stop requested: %d\n", _stopRequested); zmq::socket_t subscriber(*_context, ZMQ_SUB); subscriber.connect(_connectAddr.c_str()); const char* filter = ""; subscriber.setsockopt(ZMQ_SUBSCRIBE, filter, strlen(filter)); //capkproto::instrument_bbo bbo; _inproc = new zmq::socket_t(*_context, ZMQ_PUB); assert(_inproc); _inproc->connect(_inprocAddr.c_str()); zmq::message_t msg; while (1 && _stopRequested == false) { subscriber.recv(&msg); #ifdef DEBUG boost::posix_time::ptime time_start(boost::posix_time::microsec_clock::local_time()); #endif int64_t more; size_t more_size; more_size = sizeof(more); subscriber.getsockopt(ZMQ_RCVMORE, &more, &more_size); // send to orderbook on inproc socket - no locks needed according to zmq _inproc->send(msg, more ? ZMQ_SNDMORE : 0); #ifdef DEBUG boost::posix_time::ptime time_end(boost::posix_time::microsec_clock::local_time()); boost::posix_time::time_duration duration(time_end - time_start); fprintf(stderr, "Inbound from md interface time to receive and parse: %s\n", to_simple_string(duration).c_str()); #endif } } catch (std::exception& e) { std::cerr << "EXCEPTION: " << __FILE__ << __LINE__ << e.what(); } }
void operator()() { if (iod_cmd_socket_name) { cmd_interface = new zmq::socket_t(*MessagingInterface::getContext(), ZMQ_REQ); cmd_interface->connect(iod_cmd_socket_name); } int error_count = 0; while (!finished) { if (!connected) { boost::mutex::scoped_lock(update_mutex); if (!ctx) ctx = modbus_new_tcp(host.c_str(), port); if (modbus_connect(ctx) == -1) { ++error_count; fprintf(stderr, "Connection to %s:%d failed: %s (%d)\n", host.c_str(), port, modbus_strerror(errno), errno); modbus_free(ctx); ctx = 0; if (error_count > 5) exit(1); usleep(200000); continue; } else if (ctx) { connected = true; update_status = true; } error_count = 0; } else { performUpdates(); if (update_status) { sendStatus("initialising"); } #if 0 if (!collect_updates(bits_monitor, 0, tab_rp_bits, active_addresses, "modbus_read_bits", modbus_read_bits)) goto modbus_loop_end; if (!collect_updates(robits_monitor, 1, tab_ro_bits, ro_bits, "modbus_read_input_bits", modbus_read_input_bits)) goto modbus_loop_end; if (!collect_updates(regs_monitor, 3, tab_rq_registers, inputs, "modbus_read_input registers", modbus_read_input_registers)) { goto modbus_loop_end; } /*if (!collect_updates(holdings_monitor, 4, tab_rw_rq_registers, inputs, "modbus_read_registers", modbus_read_registers)) { goto modbus_loop_end; }*/ #else if (!collect_selected_updates<uint8_t>(robits_monitor, 1, tab_ro_bits, mc.monitors, "modbus_read_input_bits", modbus_read_input_bits)) { std::cout << "modbus_read_input_bits failed\n"; //goto modbus_loop_end; } if (!collect_selected_updates<uint8_t>(bits_monitor, 0, tab_rp_bits, mc.monitors, "modbus_read_bits", modbus_read_bits)) { std::cout << "modbus_read_bits failed\n"; //goto modbus_loop_end; } if (!collect_selected_updates<uint16_t>(regs_monitor, 3, tab_rq_registers, mc.monitors, "modbus_read_input registers", modbus_read_input_registers)) { std::cout << "modbus_read_input_registers failed\n"; //goto modbus_loop_end; } /*if (!collect_selected_updates(holdings_monitor, 4, tab_rw_rq_registers, mc.monitors, "modbus_read_registers", modbus_read_registers)) { goto modbus_loop_end; }*/ #endif if ( update_status) { sendStatus("active"); update_status = false; } } //modbus_loop_end: usleep(100000); } }
void ServerInterface::ConnectToStateServer(zmq::socket_t& stateSocket) const { std::cout << "Connecting to state server " << m_stateServer << std::endl; stateSocket.connect(m_stateServer.c_str()); stateSocket.setsockopt(ZMQ_SUBSCRIBE, m_matchToken.c_str(), m_matchToken.size()); }
Transport() : context_(1), requestSocket_(context_, ZMQ_REQ), replySocket_(context_, ZMQ_REP) { replySocket_.bind("tcp://*:5555"); requestSocket_.connect("tcp://localhost:5555"); }
void connect(::zmq::socket_t& socket, const AddressList& address_list) { typedef typename AddressList::const_iterator iter_t; for (iter_t it = address_list.begin(); it != address_list.end(); ++it) socket.connect(it->c_str()); }