Ejemplo n.º 1
0
bool MqttClient::connect(String clientName, String username, String password)
{
	if (getConnectionState() != eTCS_Ready)
	{
		close();
		debugf("MQTT closed previous connection");
	}

	debugf("MQTT start connection");
	mqtt_init(&broker, clientName.c_str());
	if (clientName.length() > 0)
		mqtt_init_auth(&broker, username.c_str(), password.c_str());

	if(server) {
		TcpClient::connect(server, port);
	}
	else {
		TcpClient::connect(serverIp, port);
	}

	mqtt_set_alive(&broker, keepAlive);
	broker.socket_info = (void*)this;
	broker.send = staticSendPacket;

	int res = mqtt_connect(&broker);
	setTimeOut(USHRT_MAX);
	return res > 0;
}
Ejemplo n.º 2
0
bool IRCClient::part ( std::string channel, std::string reason )
{
  // we need to have at LEAST sent the username and stuff
  if (getConnectionState() < eSentNickAndUSer)
    return false;

  IRCCommandInfo  info;
  info.target = channel;
  if ( reason.size())
	  info.params.push_back(reason);

  if (!sendIRCCommand(eCMD_PART,info))
  {
    log("part Failed: PART command not sent",0);
    return false;
  }

  // notify that we parted the channel
  std::string nick = getNick();
  userManager.userPartChannel(nick, channel);

  trClientPartEventInfo  eventInfo;

  eventInfo.eventType = eIRCChannelPartEvent;
  eventInfo.reason = reason;
  eventInfo.user = getNick();

  callEventHandler(eventInfo.eventType,eventInfo);

  // todo, we really should go and remove the channel from our listing and kill any dead users
  return true;
}
Ejemplo n.º 3
0
Connection *createConnection(void) {
	Connection *conn = NULL;
	ListElement *elem = NULL;

	if (!(conn = malloc(sizeof(Connection))))
		ERREXIT("Cannot allocate memory for connection.");

	pthread_rwlock_init(&(conn->state.rwlock), NULL);

	pthread_mutex_init(&(conn->sock.mtx), NULL);

	conn->state.value = RUSP_CLOSED;

	conn->sock.fd = -1;

	elem = CONPOOL.head;

	while (elem) {

		if (getConnectionState((Connection *) elem->value) == RUSP_TIMEWT) {

			usleep(RUSP_TIMEWTTM);

			elem = CONPOOL.head;

		} else {

			elem = elem->next;
		}
	}

	conn->connid = (ConnectionId) addElementToList(&CONPOOL, conn);

	return conn;
}
Ejemplo n.º 4
0
void
SIPCall::onAnswered()
{
    if (getConnectionState() != ConnectionState::CONNECTED) {
        setState(CallState::ACTIVE, ConnectionState::CONNECTED);
        Manager::instance().peerAnsweredCall(*this);
    }
}
Ejemplo n.º 5
0
 virtual void printInfo(std::ostream& out)
 {
     out << "RPCChannel: ";
     out << getChannelName();
     out << " [";
     out << Channel::ConnectionStateNames[getConnectionState()];
     out << "]";
 }
void PanelConfig::initialize(QMap<QString, QString> config)
{
    configuration = config;
    emit getConnectionState();
    readXML("panel_config.xml");
    setupConfigList();
    setupTableHeader();
    selectConfig(0);
}
Ejemplo n.º 7
0
void PanelPlot::initialize(QMap<QString, QString> config)
{
    configuration = config;
    emit getConnectionState();
    readXML("panel_plot.xml");
    setupPlotList();
    initializePlot(0);
    setupPlotNames(0);
}
Ejemplo n.º 8
0
bool KMKMqttClient::disconnect()
{
	int res=mqtt_disconnect(&broker);

	if (getConnectionState() != eTCS_Ready)
	{
		close();
		debugf("KMKMQTT closed connection");
	}

	return res>0;
}
Ejemplo n.º 9
0
void setListeningConnection(Connection *conn, const struct sockaddr_in laddr) {

	if (getConnectionState(conn) != RUSP_CLOSED)
		ERREXIT("Cannot setup listening connection: connection not closed.");

	conn->sock.fd = openSocket();

	setSocketReusable(conn->sock.fd);

	bindSocket(conn->sock.fd, &laddr);

	setConnectionState(conn, RUSP_LISTEN);
}
Ejemplo n.º 10
0
void
SIPCall::refuse()
{
    if (!isIncoming() or getConnectionState() == ConnectionState::CONNECTED or !inv)
        return;

    stopAllMedia();

    // Notify the peer
    terminateSipSession(PJSIP_SC_DECLINE);

    setState(Call::ConnectionState::DISCONNECTED, ECONNABORTED);
    removeCall();
}
Ejemplo n.º 11
0
bool
SIPCall::onhold()
{
    if (not setState(CallState::HOLD))
        return false;

    stopAllMedia();

    if (getConnectionState() == ConnectionState::CONNECTED) {
        if (SIPSessionReinvite() != PJ_SUCCESS)
            RING_WARN("[call:%s] Reinvite failed", getCallId().c_str());
    }

    return true;
}
Ejemplo n.º 12
0
bool IRCClient::mode ( std::string theMode, std::string target, std::string option )
{  
  // we need to have at LEAST sent the username and stuff
  if (getConnectionState() < eSentNickAndUSer)
    return false;

  IRCCommandInfo  info;
  info.target = target;
  info.params.push_back(theMode);
  if (option.size())
    info.params.push_back(option);

  sendIRCCommand(eCMD_MODE,info);
  return true;
}
Ejemplo n.º 13
0
bool IRCClient::login ( std::string &nick, std::string &username, std::string &thenumericmode, std::string &fullname)
{
  if (!tcpClient || !tcpClient->connected())
    return false;

  char  someNumber[64];
  sprintf(someNumber,"%d",rand());

  if (!nick.size())
    nick = std::string("SomeLazyUser") + std::string(someNumber);

  if (!username.size())
    username = "******";

  if (!thenumericmode.size())
	  thenumericmode = "8"; // Sets user as invisible

  if (!fullname.size())
    fullname = "Lazy libIRC programmer";

  requestedNick = nick;

  IRCCommandInfo  info;
  info.params.push_back(nick);

  if (!sendIRCCommand(eCMD_NICK,info))
  {
    log("Login Failed: NICK command not sent",0);
    return false;
  }

  info.params.clear();
  info.params.push_back(username);
  info.params.push_back(thenumericmode);
  info.params.push_back("*");
  info.params.push_back(fullname);

  if (!sendIRCCommand(eCMD_USER,info))
  {
    log("Login Failed: USER command not sent",0);
    return false;
  }

  if (getConnectionState() < eSentNickAndUSer)
    setConnectionState(eSentNickAndUSer);

  return  true;
}
Ejemplo n.º 14
0
bool IRCClient::loginLegacy ( std::string &nick, std::string &username, std::string &fullname, std::string &host )
{
  if (!tcpClient || !tcpClient->connected())
    return false;

  char  someNumber[64];
  sprintf(someNumber,"%d",rand());

  if (!nick.size())
    nick = std::string("SomeLazyUser") + std::string(someNumber);

  if (!username.size())
    username = "******";

  if (!fullname.size())
    fullname = "Lazy libIRC programmer";

  if (!host.size())
    host = "localhost";

  requestedNick = nick;

  IRCCommandInfo  info;
  info.params.push_back(nick);

  if (!sendIRCCommand(eCMD_NICK,info))
  {
    log("Login Failed: NICK command not sent",0);
    return false;
  }

  info.params.clear();
  info.params.push_back(username);
  info.params.push_back(host);
  info.params.push_back(ircServerName);
  info.params.push_back(fullname);

  if (!sendIRCCommand(eCMD_USER,info))
  {
    log("Login Failed: USER command not sent",0);
    return false;
  }

  if (getConnectionState() < eSentNickAndUSer)
    setConnectionState(eSentNickAndUSer);

  return  true;
}
Ejemplo n.º 15
0
bool
SIPCall::internalOffHold(const std::function<void()>& sdp_cb)
{
    if (not setState(CallState::ACTIVE))
        return false;

    sdp_cb();

    if (getConnectionState() == ConnectionState::CONNECTED) {
        if (SIPSessionReinvite() != PJ_SUCCESS) {
            RING_WARN("[call:%s] resuming hold", getCallId().c_str());
            onhold();
            return false;
        }
    }

    return true;
}
Ejemplo n.º 16
0
bool IRCClient::kick ( std::string user, std::string channel, std::string reason )
{
  // we need to have at LEAST sent the username and stuff
  if (getConnectionState() < eSentNickAndUSer)
    return false;

  if (!userManager.userInChannel(user,channel))
    return false;

  IRCCommandInfo  info;
  info.target = channel;
  info.params.push_back(user);
  info.params.push_back(reason);

  if (!sendIRCCommand(eCMD_KICK,info))
  {
    log("Kick Failed: KICK command not sent",0);
    return false;
  }

  return true;
}
Ejemplo n.º 17
0
bool IRCClient::join ( std::string channel )
{
  // we need to have at LEAST sent the username and stuff
  if (getConnectionState() < eSentNickAndUSer)
    return false;

  IRCCommandInfo  info;
  info.target = channel;
  if (!sendIRCCommand(eCMD_JOIN,info))
  {
    log("Join Failed: JOIN command not sent",0);
    return false;
  }

  if (!sendIRCCommand(eCMD_MODE,info))
  {
    log("Join Failed: MODE command not sent",0);
    return false;
  }

  return true;
}
Ejemplo n.º 18
0
void RpcLibClientBase::confirmConnection()
{
    ClockBase* clock = ClockFactory::get();

    // make sure we can talk to the DroneServer
    //std::cout << "Contacting DroneServer..." << std::flush;
    //command_context.client.ping();
    //std::cout << "DroneServer is responding." << std::endl;

    std::cout << "Waiting for connection - " << std::flush;
    const TTimeDelta pause_time = 1;
    while (getConnectionState() != RpcLibClientBase::ConnectionState::Connected)
    {
        std::cout << "X" << std::flush;
        clock->sleep_for(pause_time); 
    }
    std::cout << std::endl << "Connected!" << std::endl;

    auto server_ver = getServerVersion();
    auto client_ver = getClientVersion();
    auto server_min_ver = getMinRequiredServerVersion();
    auto client_min_ver = getMinRequiredClientVersion();
    
    std::string ver_info = Utils::stringf("Client Ver:%i (Min Req:%i), Server Ver:%i (Min Req:%i)",
        client_ver, client_min_ver, server_ver, server_min_ver);

    if (server_ver < server_min_ver) {
        std::cerr << std::endl << ver_info << std::endl;
        std::cerr << std::endl << "AirSim server is of older version and not supported by this client. Please upgrade!" << std::endl;
    }
    else if (client_ver < client_min_ver) {
        std::cerr << std::endl << ver_info << std::endl;
        std::cerr << std::endl << "AirSim client is of older version and not supported by this server. Please upgrade!" << std::endl;
    }
    else
        std::cout << std::endl << ver_info << std::endl;
}
Ejemplo n.º 19
0
static void processRcvWndBase(Connection *conn, const Segment sgm) {

	switch (getConnectionState(conn)) {

		case RUSP_ESTABL:
			if (sgm.hdr.plds != 0) {
				writeStrBuff(&(conn->rcvusrbuff), sgm.pld, sgm.hdr.plds);
				slideWindow(&(conn->rcvwnd), sgm.hdr.plds);
				DBGPRINT(RUSP_DEBUG, "RCV (WND): base:%u end:%u RCVUSRBUFF:%zu RCVSGMBUFF:%ld", getWindowBase(&(conn->rcvwnd)), getWindowEnd(&(conn->rcvwnd)), getStrBuffSize(&(conn->rcvusrbuff)), getSgmBuffSize(&(conn->rcvsgmbuff)));
			}

			if (sgm.hdr.ctrl & RUSP_PSH)
				allignStrBuffSizeUsr(&(conn->rcvusrbuff));

			if (sgm.hdr.ctrl & RUSP_FIN) {
				allignStrBuffSizeUsr(&(conn->rcvusrbuff));
				setConnectionState(conn, RUSP_CLOSWT);
				slideWindow(&(conn->rcvwnd), 1);
				DBGPRINT(RUSP_DEBUG, "RCV (WND): base:%u end:%u RCVUSRBUFF:%zu RCVSGMBUFF:%ld", getWindowBase(&(conn->rcvwnd)), getWindowEnd(&(conn->rcvwnd)), getStrBuffSize(&(conn->rcvusrbuff)), getSgmBuffSize(&(conn->rcvsgmbuff)));
			}
			break;

		case RUSP_LSTACK:
			if ((sgm.hdr.ctrl & RUSP_SACK) && (sgm.hdr.ackn == getWindowNext(&(conn->sndwnd)))) {
				setConnectionState(conn, RUSP_CLOSED);
				pthread_exit(NULL);
			}
			break;

		case RUSP_FINWT1:
			if ((sgm.hdr.ctrl & RUSP_SACK) && (sgm.hdr.ackn == getWindowNext(&(conn->sndwnd)))) {
				setConnectionState(conn, RUSP_FINWT2);
			} else if ((sgm.hdr.ctrl & (RUSP_FIN | RUSP_SACK)) && (sgm.hdr.ackn != getWindowNext(&(conn->sndwnd)))) {
				setConnectionState(conn, RUSP_CLOSIN);
				slideWindow(&(conn->rcvwnd), 1);
				DBGPRINT(RUSP_DEBUG, "RCV (WND): base:%u end:%u RCVUSRBUFF:%zu RCVSGMBUFF:%ld", getWindowBase(&(conn->rcvwnd)), getWindowEnd(&(conn->rcvwnd)), getStrBuffSize(&(conn->rcvusrbuff)), getSgmBuffSize(&(conn->rcvsgmbuff)));
			} else if ((sgm.hdr.ctrl & (RUSP_FIN | RUSP_SACK)) && (sgm.hdr.ackn == getWindowNext(&(conn->sndwnd)))) {
				setConnectionState(conn, RUSP_TIMEWT);
				slideWindow(&(conn->rcvwnd), 1);
				DBGPRINT(RUSP_DEBUG, "RCV (WND): base:%u end:%u RCVUSRBUFF:%zu RCVSGMBUFF:%ld", getWindowBase(&(conn->rcvwnd)), getWindowEnd(&(conn->rcvwnd)), getStrBuffSize(&(conn->rcvusrbuff)), getSgmBuffSize(&(conn->rcvsgmbuff)));
				createThread(timeWaitFunction, conn, THREAD_DETACHED);
				pthread_exit(NULL);
			}
			break;

		case RUSP_FINWT2:
			if (sgm.hdr.ctrl & RUSP_FIN) {
				setConnectionState(conn, RUSP_TIMEWT);
				slideWindow(&(conn->rcvwnd), 1);
				DBGPRINT(RUSP_DEBUG, "RCV (WND): base:%u end:%u RCVUSRBUFF:%zu RCVSGMBUFF:%ld", getWindowBase(&(conn->rcvwnd)), getWindowEnd(&(conn->rcvwnd)), getStrBuffSize(&(conn->rcvusrbuff)), getSgmBuffSize(&(conn->rcvsgmbuff)));
				createThread(timeWaitFunction, conn, THREAD_DETACHED);
				pthread_exit(NULL);
			}
			break;

		case RUSP_CLOSIN:
			if ((sgm.hdr.ctrl & RUSP_SACK) && (sgm.hdr.ackn == getWindowNext(&(conn->sndwnd)))) {
				setConnectionState(conn, RUSP_TIMEWT);
				createThread(timeWaitFunction, conn, THREAD_DETACHED);
				pthread_exit(NULL);
			}
			break;

		default:
			break;
	}
}
Ejemplo n.º 20
0
ConnectionId passiveOpen(Connection *lconn) {
	Connection *aconn = NULL;
	Segment syn, synack, acksynack;
	char ssyn[RUSP_SGMS + 1], ssynack[RUSP_SGMS + 1], sacksynack[RUSP_SGMS + 1];
	int asock, synackretrans;
	struct sockaddr_in caddr;
	struct timespec start, end;
	long double sampleRTT;

	while (getConnectionState(lconn) == RUSP_LISTEN) {

		readUSocket(lconn->sock.fd, &caddr, ssyn, RUSP_SGMS);

		deserializeSegment(ssyn, &syn);

		DBGFUNC(RUSP_DEBUG, printInSegment(caddr, syn));

		if (syn.hdr.ctrl != RUSP_SYN)
			continue;

		setConnectionState(lconn, RUSP_SYNRCV);

		asock = openSocket();

		synack = createSegment(RUSP_SYN | RUSP_SACK, 0, 10, RUSP_NXTSEQN(syn.hdr.seqn, 1), NULL);

		serializeSegment(synack, ssynack);

		for (synackretrans = 0; synackretrans < RUSP_RETR; synackretrans++) {

			clock_gettime(CLOCK_MONOTONIC, &start);

			writeUSocket(asock, caddr, ssynack, strlen(ssynack));

			DBGFUNC(RUSP_DEBUG, printOutSegment(caddr, synack));

			setConnectionState(lconn, RUSP_SYNSND);

			if (!selectSocket(asock, RUSP_SAMPLRTT))
				continue;

			readUSocket(asock, &caddr, sacksynack, RUSP_SGMS);

			clock_gettime(CLOCK_MONOTONIC, &end);

			sampleRTT = getElapsed(start, end);

			deserializeSegment(sacksynack, &acksynack);

			DBGFUNC(RUSP_DEBUG, printInSegment(caddr, acksynack));

			if ((acksynack.hdr.ctrl == RUSP_SACK) &
				(acksynack.hdr.seqn == synack.hdr.ackn) &
				(acksynack.hdr.ackn == RUSP_NXTSEQN(synack.hdr.seqn, 1))) {

				aconn = createConnection();

				setConnectionState(aconn, RUSP_SYNSND);

				setupConnection(aconn, asock, caddr, acksynack.hdr.ackn, acksynack.hdr.seqn, sampleRTT);

				setConnectionState(lconn, RUSP_LISTEN);

				return aconn->connid;
			}
		}

		closeSocket(asock);

		setConnectionState(lconn, RUSP_LISTEN);
	}

	return -1;
}
Ejemplo n.º 21
0
int activeOpen(Connection *conn, const struct sockaddr_in laddr) {
	Segment syn, synack, acksynack;
	char ssyn[RUSP_SGMS + 1], ssynack[RUSP_SGMS + 1], sacksynack[RUSP_SGMS + 1];
	int asock, synretrans;
	struct sockaddr_in aaddr;
	struct timespec start, end;
	long double sampleRTT;

	if (getConnectionState(conn) != RUSP_CLOSED)
		ERREXIT("Cannot synchronize connection: connection not closed.");

	asock = openSocket();

	syn = createSegment(RUSP_SYN, 0, 0, 0, NULL);

	serializeSegment(syn, ssyn);

	for (synretrans = 0; synretrans < RUSP_SYN_RETR; synretrans++) {

		clock_gettime(CLOCK_MONOTONIC, &start);

		writeUSocket(asock, laddr, ssyn, strlen(ssyn));

		DBGFUNC(RUSP_DEBUG, printOutSegment(laddr, syn));

		setConnectionState(conn, RUSP_SYNSND);

		if (!selectSocket(asock, RUSP_SAMPLRTT))
			continue;

		readUSocket(asock, &aaddr, ssynack, RUSP_SGMS);

		clock_gettime(CLOCK_MONOTONIC, &end);

		sampleRTT = getElapsed(start, end);

		deserializeSegment(ssynack, &synack);

		DBGFUNC(RUSP_DEBUG, printInSegment(aaddr, synack));

		if ((synack.hdr.ctrl == (RUSP_SYN | RUSP_SACK)) &
			(synack.hdr.ackn == RUSP_NXTSEQN(syn.hdr.seqn, 1))) {

			setConnectionState(conn, RUSP_SYNRCV);

			acksynack = createSegment(RUSP_SACK, 0, RUSP_NXTSEQN(syn.hdr.seqn, 1), RUSP_NXTSEQN(synack.hdr.seqn, 1), NULL);

			serializeSegment(acksynack, sacksynack);

			writeUSocket(asock, aaddr, sacksynack, strlen(sacksynack));

			DBGFUNC(RUSP_DEBUG, printOutSegment(aaddr, acksynack));

			setupConnection(conn, asock, aaddr, acksynack.hdr.seqn, acksynack.hdr.ackn, sampleRTT);

			return conn->connid;
		}
	}

	closeSocket(asock);

	setConnectionState(conn, RUSP_CLOSED);

	return -1;
}
Ejemplo n.º 22
0
void EventConnection::processEvent(NetEvent *theEvent)
{
   if(getConnectionState() == NetConnection::Connected)
      theEvent->process(this);
}
void PanelMonitor::initialize(QMap<QString, QString> config)
{
    configuration = config;
    emit getConnectionState();
}
Ejemplo n.º 24
0
bool EventConnection::postNetEvent(NetEvent *theEvent)
{
   // Check if the direction this event moves is a valid direction.
   TNLAssertV(   (theEvent->getEventDirection() != NetEvent::DirUnset)
      && (theEvent->getEventDirection() != NetEvent::DirServerToClient || !isConnectionToServer())
      && (theEvent->getEventDirection() != NetEvent::DirClientToServer || !isConnectionToClient()),
      ("Trying to send wrong event direction in %s", theEvent->getClassName()));

   S32 classId = theEvent->getClassId(getNetClassGroup());
   if(U32(classId) >= mEventClassCount && getConnectionState() == Connected)
   {
      theEvent->incRef();
      theEvent->decRef(); // Avoids some type of memory leak by deleting here if nothing reference it.
      return false;
   }

   theEvent->notifyPosted(this);

   EventNote *event = mEventNoteChunker.alloc();
   event->mEvent = theEvent;
   event->mNextEvent = NULL;

   if(event->mEvent->mGuaranteeType == NetEvent::GuaranteedOrdered)
   {
      event->mSeqCount = mNextSendEventSeq++;
      if(!mSendEventQueueHead)
         mSendEventQueueHead = event;
      else
         mSendEventQueueTail->mNextEvent = event;
      mSendEventQueueTail = event;
   }
   else if(event->mEvent->mGuaranteeType == NetEvent::GuaranteedOrderedBigData)
   {
      BitStream bstream;
      const U32 start = 0;
      const U32 partsSize = 512;

      if(mConnectionParameters.mDebugObjectSizes)
         bstream.advanceBitPosition(BitStreamPosBitSize);
      
      S32 classId = event->mEvent->getClassId(getNetClassGroup());
      bstream.writeInt(classId, mEventClassBitSize);

      event->mEvent->pack(this, &bstream);
      logprintf(LogConsumer::LogEventConnection, "EventConnection %s: WroteEvent %s - %d bits", getNetAddressString(), event->mEvent->getDebugName(), bstream.getBitPosition() - start);

      if(mConnectionParameters.mDebugObjectSizes)
         bstream.writeIntAt(bstream.getBitPosition(), BitStreamPosBitSize, start);

      U32 size = bstream.getBytePosition();

      for(U32 i=0; i<size; i+=partsSize)
      {
         if(i+partsSize < size)
         {
            ByteBuffer *bytebuffer = new ByteBuffer(&bstream.getBuffer()[i], partsSize);
            bytebuffer->takeOwnership();  // may have to use this to prevent errors.
            s2rTNLSendDataParts(0, ByteBufferPtr(bytebuffer));
         }
         else
         {
            ByteBuffer *bytebuffer = new ByteBuffer(&bstream.getBuffer()[i], size-i);
            bytebuffer->takeOwnership();
            s2rTNLSendDataParts(1, ByteBufferPtr(bytebuffer));
         }
      }
      mEventNoteChunker.free(event);
   }
   else
   {
      event->mSeqCount = InvalidSendEventSeq;
      if(!mUnorderedSendEventQueueHead)
         mUnorderedSendEventQueueHead = event;
      else
         mUnorderedSendEventQueueTail->mNextEvent = event;
      mUnorderedSendEventQueueTail = event;
   }
   return true;
}
Ejemplo n.º 25
0
/**
 * @return if the client is connected to the server
 */
bool ClientConnectionAdapter::isConnected() {
	return getConnectionState()==&ClientConnectionState::IN_SYNC_WITH_SERVER;
}