Exemplo n.º 1
0
CITPLib::CITPLib(QObject *parent)
  : QObject(parent),
    m_peerSocket(NULL)
{
  
  m_peerSocket = new PeerInformationSocket(this);
  Q_CHECK_PTR(m_peerSocket);
  connect(m_peerSocket, SIGNAL(peersUpdated()),
	  this, SIGNAL(peersUpdated()));
}
Exemplo n.º 2
0
void PeerInformationSocket::addPeer(const QHostAddress &host, quint16 listeningPort, 
				    const QString &typeString, 
				    const QString &name, const QString &state)
{
  // check if we already have this peer
  foreach (const Peer *peer, m_peerList)
    {
      if (peer)
	{
	  if (peer->peerHost() == host.toString() && peer->peerListeningPort() == listeningPort)
	    {
	      return;
	    }
	}
    }

  //  qDebug() << "Adding new peer to list:" << host.toString() << listeningPort << typeString << name << state;

  // add the newly discovered peer
  Peer *newPeer;
  if ("Visualizer" == typeString)
    {
      newPeer = new Visualizer(host, listeningPort, name, state);
    }
  else
    {
      newPeer = new Peer(host, listeningPort, name, state);
    }
  Q_CHECK_PTR(newPeer);
  m_peerList.append(newPeer);

  qDebug() << "added peer, peer list size:" << m_peerList.size();

  emit peersUpdated();
}
Exemplo n.º 3
0
void Messenger::onTimerdiscovery()
{
    QString packet = PCK_HEADER "DISCOVERY:" + _mypeer.ID();
    //QHostAddress target = QHostAddress("224.1.2.3");
    //QHostAddress target = QHostAddress("192.168.0.255");
    QHostAddress target = QHostAddress::Broadcast;
    logSent(packet, target);
    _udp.writeDatagram(packet.toUtf8(), target, 2880);

    // chekc for olds
    for(int i=0; i<_peers.count(); i++)
        if(_peers[i].Lastseen.secsTo(QTime::currentTime()) > 10)
        {
            _peers.removeAt(i);
            emit peersUpdated();
        }

    // room list
    for(int i=0; i<_rooms.count(); i++)
        roomList(_rooms[i]);

    for(int i=0; i<_rooms.count(); i++)
    {
        QString room = _rooms[i];
        if(!_roomslist.contains(room)) continue;
        for(int j=0; j<_roomslist[room].count(); j++)
            if(_roomslist[room].at(j).Lastseen.secsTo(QTime::currentTime()) > 10)
            {
                QString name = _roomslist[room][j].ID();
                _roomslist[room].removeAt(j);
                emit roomListUpdated(room, "*** " + name + " Left. ***");
            }
    }
}
Exemplo n.º 4
0
void Messenger::processTheDatagram(QByteArray data, QHostAddress sender)
{
    QString str_packet = QString(data);
    QStringList packet = str_packet.split(':');
    if(packet.count()<4)
    {
        qWarning("Warning: Unknown Packet. ");
        return;
    }
    if(packet[0] != "288")
    {
        qWarning("Warning: Protocol Mismatch. ");
        return;
    }
    if(packet[1] != "0")
    {
        qWarning("Warning: Protocol Version Mismatch. ");
        return;
    }
    if(packet[2] == "DISCOVERY")
    {
        int found = -1;
        for(int i=0; i<_peers.count(); i++)
            if(_peers[i].ID() == packet[3]) found = i;
        if(found == -1)
        {
            Peer newpeer;
            QString peerid = packet[3];
            QStringList peerids = peerid.split('@');
            if(peerids.count() != 2)
            {
                qWarning("Warning: Wronge new Peer ID.");
                return;
            }
            newpeer.Name = peerids[0];
            newpeer.Domain = peerids[1];
            newpeer.Host = sender;
            newpeer.Lastseen = QTime::currentTime();
            _peers.append(newpeer);
            emit peersUpdated();
        }
        else
        {
            _peers[found].Lastseen = QTime::currentTime();
        }
    }

    if(packet[2] == "ROOMLIST")
    {
        QString room = packet[3];
        if(!_rooms.contains(room)) return;
        if(!_roomslist.contains(room))
            _roomslist.insert(room, PeerList());

        int found = -1;
        for(int i=0; i<_roomslist[room].count(); i++)
            if(_roomslist[room][i].ID() == packet[4]) found = i;

        if(found == -1)
        {
            Peer newpeer;
            QString peerid = packet[4];
            QStringList peerids = peerid.split('@');
            if(peerids.count() != 2)
            {
                qWarning("Warning: Wronge new Peer ID.");
                return;
            }
            newpeer.Name = peerids[0];
            newpeer.Domain = peerids[1];
            newpeer.Host = sender;
            newpeer.Lastseen = QTime::currentTime();
            _roomslist[room].append(newpeer);
            emit roomListUpdated(room, "***  " + newpeer.ID() + "  Joined. ***");
        }
        else
        {
            _roomslist[room][found].Lastseen = QTime::currentTime();
        }
    }


    if(packet[2] == "PM")
    {
        QString from = packet[3];
        QString text = packet[4];
        for(int i=5; i<packet.count(); i++)
            text += ":" + packet[i];
        emit receivedPM(from, text);
    }
    if(packet[2] == "ROOM")
    {
        QString room = packet[3];
        QString from = packet[4];
        QString text = packet[5];
        for(int i=6; i<packet.count(); i++)
            text += ":" + packet[i];

        bool found=false;
        for(int i=0;i<_rooms.count(); i++)
            if(_rooms[i] == room) found = true;

        if(found)
            emit receivedRoom(room, from, text);
    }
}