bool StartNovad(bool blocking) { if(IsNovadUp(false)) { return true; } if (!blocking) { if(system("nohup novad > /dev/null&") != 0) { return false; } else { return true; } } else { if(system("novad") != 0) { return false; } else { return true; } } }
void StatusNovaWrapper() { if(!ConnectToNovad()) { cout << "Novad Status: Not running" << endl; } else { if(IsNovadUp(false)) { cout << "Novad Status: Running and responding" << endl; } else { cout << "Novad Status: Not responding" << endl; } CloseNovadConnection(); } }
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; }