Ejemplo n.º 1
0
void QueryThread::open(const ConnectionData& cd) {
    if (!isRunning()) {
        start(LowPriority);
    }
    emit message(tr("Sending Connect Request to Database Connection..."));
    emit connectRequest(cd, connectionName());
}
Ejemplo n.º 2
0
//returns 1 if everything worked as expected, other error codes will follow
int ReefServer::connect(std::string aka, std::string ownIp, std::string reefIp){
	identity = aka;
	//make a Request to connect to the reef, saves Reef Addresslist in adr_list
	connectRequest(aka, ownIp, reefIp);

	//use the adr_list to connect your sub to all publisher in the reef
	connectSubscribe();
	return 1;
}
Ejemplo n.º 3
0
TransmitThread::TransmitThread(QObject *parent) :
    QThread(parent),
    m_pTcpSocket(NULL),
    m_hHostIp(QHostAddress("127.0.0.1")),
    m_nHostPort(6010),
    m_bConnect(false),
    m_bDisconnect(false),
    m_bRunning(false),
    m_bStopped(false)
{
    connect(this, SIGNAL(recvSignal(const QByteArray &)), parent, SLOT(switchStation(const QByteArray &)));
    connect(this, SIGNAL(connectSignal()), parent, SLOT(connectRequest()));
    connect(this, SIGNAL(disconnectSignal()), parent, SLOT(disconnectRequest()));
}
Ejemplo n.º 4
0
bool ConnectToNovad()
{
	if(IsNovadUp(false))
	{
		return true;
	}

	DisconnectFromNovad();

	//Create new base
	if(libeventBase == NULL)
	{
		evthread_use_pthreads();
		libeventBase = event_base_new();
		pthread_mutex_init(&bufferevent_mutex, NULL);
	}

	//Builds the key path
	string key = Config::Inst()->GetPathHome();
	key += "/config/keys";
	key += NOVAD_LISTEN_FILENAME;

	//Builds the address
	struct sockaddr_un novadAddress;
	novadAddress.sun_family = AF_UNIX;
	memset(novadAddress.sun_path, '\0', sizeof(novadAddress.sun_path));
	strncpy(novadAddress.sun_path, key.c_str(), sizeof(novadAddress.sun_path));

	{
		Lock buffereventLock(&bufferevent_mutex);
		bufferevent = bufferevent_socket_new(libeventBase, -1,
				BEV_OPT_CLOSE_ON_FREE | BEV_OPT_THREADSAFE |  BEV_OPT_UNLOCK_CALLBACKS | BEV_OPT_DEFER_CALLBACKS );
		if(bufferevent == NULL)
		{
			LOG(ERROR, "Unable to create a socket to Nova", "");
			DisconnectFromNovad();
			return false;
		}

		bufferevent_setcb(bufferevent, MessageManager::MessageDispatcher, NULL, MessageManager::ErrorDispatcher, NULL);

		if(bufferevent_enable(bufferevent, EV_READ) == -1)
		{
			LOG(ERROR, "Unable to enable socket events", "");
			return false;
		}

		if(bufferevent_socket_connect(bufferevent, (struct sockaddr *)&novadAddress, sizeof(novadAddress)) == -1)
		{
			bufferevent = NULL;
			LOG(DEBUG, "Unable to connect to NOVAD: "+string(strerror(errno))+".", "");
			return false;
		}

		IPCSocketFD = bufferevent_getfd(bufferevent);
		if(IPCSocketFD == -1)
		{
			LOG(DEBUG, "Unable to connect to Novad: "+string(strerror(errno))+".", "");
			bufferevent_free(bufferevent);
			bufferevent = NULL;
			return false;
		}

		if(evutil_make_socket_nonblocking(IPCSocketFD) == -1)
		{
			LOG(DEBUG, "Unable to connect to Novad", "Could not set socket to non-blocking mode");
			bufferevent_free(bufferevent);
			bufferevent = NULL;
			return false;
		}

		MessageManager::Instance().DeleteEndpoint(IPCSocketFD);
		MessageManager::Instance().StartSocket(IPCSocketFD, bufferevent);
	}
	pthread_create(&eventDispatchThread, NULL, EventDispatcherThread, NULL);

	//Test if the connection is up
	Ticket ticket = MessageManager::Instance().StartConversation(IPCSocketFD);

	RequestMessage connectRequest(REQUEST_PING);
	if(!MessageManager::Instance().WriteMessage(ticket, &connectRequest))
	{
		return false;
	}

	Message *reply = MessageManager::Instance().ReadMessage(ticket);
	if(reply->m_messageType == ERROR_MESSAGE && ((ErrorMessage*)reply)->m_errorType == ERROR_TIMEOUT)
	{
		LOG(DEBUG, "Timeout error when waiting for message reply", "");
		delete reply;
		return false;
	}

	if(reply->m_messageType != REQUEST_MESSAGE)
	{
		stringstream s;
		s << "Expected message type of REQUEST_MESSAGE but got " << reply->m_messageType;
		LOG(DEBUG, s.str(), "");

		reply->DeleteContents();
		delete reply;
		return false;
	}
	RequestMessage *connectionReply = (RequestMessage*)reply;
	if(connectionReply->m_contents.m_requesttype() != REQUEST_PONG)
	{
		stringstream s;
		s << "Expected control type of CONTROL_CONNECT_REPLY but got " <<connectionReply->m_contents.m_requesttype();
		LOG(DEBUG, s.str(), "");

		reply->DeleteContents();
		delete reply;
		return false;
	}
	delete connectionReply;

	return true;
}