int RabbitInBoundConnectionPoint::ListenMessage(void** destBuffer) throw (ConnectException)
			{
				amqp_rpc_reply_t res;
				amqp_envelope_t envelope;

				amqp_maybe_release_buffers(_conn);

				struct timeval timeout;
				timeout.tv_sec = 5;
				timeout.tv_usec = 0;

				res = amqp_consume_message(_conn, &envelope, &timeout, 0);
				  
				if (AMQP_RESPONSE_NORMAL == res.reply_type) 
				{
					int messageLen = (int) envelope.message.body.len;
					void* message = malloc(messageLen); 
					memcpy(message, envelope.message.body.bytes, messageLen);
					amqp_basic_ack(_conn, 1, envelope.delivery_tag, false);
					GetRabbitError("Ack Error");
					amqp_destroy_envelope(&envelope);
					*destBuffer = message;
					return messageLen;
				}
				else
				{
					if(res.library_error != AMQP_STATUS_TIMEOUT)
					{
						LOG(ERROR) << "Error al leer de Rabbit: " << amqp_error_string2(res.library_error);
						throw ConnectException("Error al leer de Rabbit", true);
					}
				}

				return 0;
			}
Exemplo n.º 2
0
void TCPSocket::connect(const char *name, unsigned short port) {
    if (this->_state != CREATED) {
        throw SocketStateException("Wrong state for operation");
    }

    struct sockaddr_in remote_addr;

    const int GETHOSTBYNAME_R_BUFSIZE = 4096;
    char tmp_buf[GETHOSTBYNAME_R_BUFSIZE];

    struct hostent *result;
#if defined( linux ) || defined( __linux )
    struct hostent ret;

    int h_errnop;

    if (gethostbyname_r(name, &ret, tmp_buf, GETHOSTBYNAME_R_BUFSIZE,
            &result, &h_errnop) == -1) {
        throw DNSException(h_errno);
    }

    if (result == NULL) {
        throw DNSException(h_errno);
    }
#endif

#if defined( sun ) || defined( __sun )
    struct hostent entry;
    int h_errnop;
    if ((result = gethostbyname_r(name, &entry, tmp_buf, GETHOSTBYNAME_R_BUFSIZE,
            &h_errnop)) == NULL) {
        throw DNSException(h_errno);
    }
#endif


    memset(&remote_addr, 0, sizeof (remote_addr));
    remote_addr.sin_addr = *((in_addr *) result->h_addr_list[0]);
    remote_addr.sin_family = AF_INET;
    remote_addr.sin_port = htons(port);

    if (this->_nonblocking == false) {
        if (::connect(this->_b->_sock, (struct sockaddr*) & remote_addr, sizeof (remote_addr)) == -1) {
            if (errno == EAGAIN)
                throw EAGAINException();
            else
                throw ConnectException(errno);
        }
        this->_state = CONNECTED;
    }

    if (this->_nonblocking == true) {
        ::connect(this->_b->_sock, (struct sockaddr*) & remote_addr, sizeof (remote_addr));
        this->_state = CONNECTING;
    }

#ifdef DEBUG
    fprintf(stderr, "connected to %s port %d\n", name, port);
#endif
}
Exemplo n.º 3
0
 void LocalServerConnection<BufferType>::Close() {
   m_pendingChannels->Break(NotConnectedException());
   while(!m_pendingChannels->IsEmpty()) {
     PendingChannelEntry* entry = m_pendingChannels->Top();
     m_pendingChannels->Pop();
     entry->m_result.SetException(ConnectException("Server unavailable."));
   }
 }
Exemplo n.º 4
0
void HttpClient::connect() {
  try {
    auto ipAddr = System::Ipv4Resolver(m_dispatcher).resolve(m_address);
    m_connection = System::TcpConnector(m_dispatcher).connect(ipAddr, m_port);
    m_streamBuf.reset(new System::TcpStreambuf(m_connection));
    m_connected = true;
  } catch (const std::exception& e) {
    throw ConnectException(e.what());
  }
}
Exemplo n.º 5
0
std::auto_ptr<Socket>
Connector::connect(unsigned short port, const char *host)
throw (ConnectException&)
{
 	// connect to server
 	bsd::SOCK_Connector connector;
 	std::auto_ptr<Socket> clientConnectionPtr(new Socket);
	bsd::SOCK_Stream peer = clientConnectionPtr->getConnectionHandle();
	if (connector.connect(peer, host, port) < 0)
		throw ConnectException("could not to connect to server");
	return clientConnectionPtr;
}
Exemplo n.º 6
0
void TCPSocket::validate_connect() {
    if (this->_state == CONNECTING) {
        int error;
        socklen_t len = sizeof (error);
        this->getsockopt(SOL_SOCKET, SO_ERROR, &error, &len);
        if (error != 0) {
            // can this happen?
            if (error == EAGAIN)
                throw EAGAINException();
            else
                throw ConnectException(error);
        }
        this->_state = CONNECTED;
    }
}
Exemplo n.º 7
0
//////////////////////////////////////////////////////////////////////
// send datagram to peer
//////////////////////////////////////////////////////////////////////
uint DatagramSocket::send (Datagram * pDatagram )
	throw(ConnectException , Error )
{
	__BEGIN_TRY 

	Assert(pDatagram != NULL);
	try {

	int nSent = SocketAPI::sendto_ex(m_SocketID , pDatagram->getData() , pDatagram->getLength() , 0 , pDatagram->getAddress() , szSOCKADDR_IN);

	return (uint)nSent;

	} catch (ConnectException & t ) {
		cout <<"DatagramSocket::send Exception Check!" << endl;
		cout << t.toString() << endl;
		throw ConnectException("DatagramSocket의 상위로 던진다");
	}

	__END_CATCH
}
Exemplo n.º 8
0
void ClientImpl::initiateConnection(boost::shared_ptr<PendingConnection> &pc) throw (voltdb::ConnectException, voltdb::LibEventException){


    std::stringstream ss;
    ss << "ClientImpl::initiateConnection to " << pc->m_hostname << ":" << pc->m_port;
    logMessage(ClientLogger::INFO, ss.str());
    struct bufferevent * bev = bufferevent_socket_new(m_base, -1, BEV_OPT_CLOSE_ON_FREE | BEV_OPT_THREADSAFE);
    if (bev == NULL) {
        throw ConnectException();
    }
    FreeBEVOnFailure protector(bev);
    bufferevent_setcb(bev, authenticationReadCallback, NULL, authenticationEventCallback, pc.get());

    if (bufferevent_socket_connect_hostname(bev, NULL, AF_INET, pc->m_hostname.c_str(), pc->m_port) != 0) {

        ss.str("");
        ss << "!!!! ClientImpl::initiateConnection to " << pc->m_hostname << ":" << pc->m_port << " failed";
    	logMessage(ClientLogger::ERROR, ss.str());

        throw voltdb::LibEventException();
    }
    protector.success();
    }
Exemplo n.º 9
0
void ClientImpl::createConnection(const std::string& hostname, const unsigned short port) throw (voltdb::Exception, voltdb::ConnectException, voltdb::LibEventException) {

    std::stringstream ss;
    ss << "ClientImpl::createConnection" << " hostname:" << hostname << " port:" << port;
    logMessage(ClientLogger::INFO, ss.str());

    PendingConnectionSPtr pc(new PendingConnection(hostname, port, m_base, this));
    initiateConnection(pc);

    if (event_base_dispatch(m_base) == -1) {
        throw voltdb::LibEventException();
    }

    if (pc->m_status) {
        if (event_base_dispatch(m_base) == -1) {
            throw voltdb::LibEventException();
        }

        if (pc->m_loginExchangeCompleted) {
            return;
        }
    }
    throw ConnectException();
}
Exemplo n.º 10
0
void ClientImpl::finalizeAuthentication(PendingConnection* pc, struct bufferevent *bev) throw (voltdb::Exception, voltdb::ConnectException){

    logMessage(ClientLogger::DEBUG, "ClientImpl::finalizeAuthentication");

    FreeBEVOnFailure protector(bev);
    if (pc->m_loginExchangeCompleted) {

        logMessage(ClientLogger::DEBUG, "ClientImpl::finalizeAuthentication OK");

        if (!m_instanceIdIsSet) {
            m_instanceIdIsSet = true;
            m_clusterStartTime = pc->m_response.clusterStartTime();
            m_leaderAddress = pc->m_response.leaderAddress();
        } else {
            if (m_clusterStartTime != pc->m_response.clusterStartTime() ||
                m_leaderAddress != pc->m_response.leaderAddress()) {
                throw ClusterInstanceMismatchException();
            }
        }
        //save event for host id
        m_hostIdToEvent[pc->m_response.hostId()] = bev;
        bufferevent_setwatermark( bev, EV_READ, 4, HIGH_WATERMARK);
        m_bevs.push_back(bev);

        m_contexts[bev] =
               boost::shared_ptr<CxnContext>(
                   new CxnContext(pc->m_hostname, pc->m_port));
        boost::shared_ptr<CallbackMap> callbackMap(new CallbackMap());
        m_callbacks[bev] = callbackMap;

        //Add callback for Topology Notification to map for magic volt session id
        boost::shared_ptr<TopologyNotificationCallback> topoNotificationCallback(new TopologyNotificationCallback(&m_distributer));
        callbackMap->insert(std::pair< int64_t, boost::shared_ptr<ProcedureCallback> >(VOLT_NOTIFICATION_MAGIC_NUMBER, topoNotificationCallback));

        bufferevent_setcb(
               bev,
               voltdb::regularReadCallback,
               voltdb::regularWriteCallback,
               voltdb::regularEventCallback, this);

        {
            boost::mutex::scoped_lock lock(m_pendingConnectionLock);
            for (std::list<PendingConnectionSPtr>::iterator i = m_pendingConnectionList.begin(); i != m_pendingConnectionList.end(); ++i) {
                if (i->get() == pc) {
                    m_pendingConnectionList.erase(i);
                    m_pendingConnectionSize.store(m_pendingConnectionList.size(), boost::memory_order_release);
                    break;
                }
            }
        }

        //update topology info and procedures info
        if (m_useClientAffinity) {
            updateHashinator();
            subscribeToTopologyNotifications();
        }

    	std::stringstream ss;
    	ss << "connectionActive " << m_contexts[bev]->m_name << ":" << m_contexts[bev]->m_port ;
    	logMessage(ClientLogger::INFO, ss.str());

        //Notify client that a connection was active
        if (m_listener.get() != NULL) {
            try {

                 m_listener->connectionActive( m_contexts[bev]->m_name, m_bevs.size() );
            } catch (const std::exception& e) {
                std::cerr << "Status listener threw exception on connection active: " << e.what() << std::endl;
            }
        }

    }
    else {


        logMessage(ClientLogger::DEBUG, "ClientImpl::finalizeAuthentication Fail");

    	std::stringstream ss;
    	ss << "connection failed " << " " << pc->m_hostname << ":" << pc->m_port;
    	logMessage(ClientLogger::ERROR, ss.str());

        throw ConnectException();
    }
            protector.success();
}