Results 1 to 3 of 3

Thread: QVector of pointers to custom classes that contain sockets

  1. #1
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QVector of pointers to custom classes that contain sockets

    I have two custom classes (shown below) that I am trying to use in order to have multiple connections to a server(s). These connections are to what is called APRS-IS servers, they way it works is, I connect to the server and send an authorization string, after I am authenticated the server sends me APRS "packets" as text across the socket as they come to the server that I am connected to. I know that my ServerConnection class (below) works for this purpose (individually), but I want to be connected to multiple ports of the server (representing different types of packets). So for that purpose I have a ConnectionInterface class (below), it has a QVector of pointers to the ServerConnection Objects.

    My problem is:

    I can create a ConnectionInterface object, I then use the newConnection method to create a new ServerConnection object and append that to the QVector

    I then connect to the server using the pointer to that ServerConnection and calling the ServerConnection::connectToServer() method. My problem is, no matter how many ServerConnections I have in the QVector when I call the connectToServer method, it seems to stay connected to whatever connection I call last:

    for instance, I call ConnectionInterface::connectToServer(0) //where 0 represents the index in the QVector of the ServerConnection object I want it to connect.

    I then can call connectToServer(1) and the server only shows I am connected on the port that the ServerConnection at index 1 represented.

    I appologize for the long explanation, but I didn't know any other way to state my problem. Any help would be GREATLY appreciated!

    Here is my code - sorry for triple-post

    ServerConnection.h
    Qt Code:
    1. [QTCLASS]
    2. #ifndef SERVERCONNECTION_H
    3. #define SERVERCONNECTION_H
    4.  
    5. #include <QTcpSocket>
    6. #include <QApplication>
    7.  
    8. class ServerConnection : public QObject
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. // Parameterized constructor
    14. ServerConnection(QString name, QString host, int port, QString authName, QString password, QString filterString);
    15. ServerConnection(QString name, QString host, int port, QString authName, QString password, QString filterString, bool writeEnabled);
    16. ~ServerConnection(); // Deconstructor
    17. private:
    18. QTcpSocket* socket; // Pointer to a QTcpSocket that will be this instance's connection
    19. QString name; // Name of this connection
    20. QString host; // Hostname that this connection will be established to
    21. int port; // Port number of the host that this connection will connect to
    22. QString authName; // Login name for the server that this will connect to
    23. QString password; // Password for the login name of the server this will connect to
    24. QString authString; // The authentication string used to connect to the server
    25. QString filterString; // The filter string entered by user in order to filter via server
    26. bool writeEnabled; // Lets this instance know if it can write to the connection or not
    27. bool connected; // Connected to server?
    28. public:
    29. QString getPacket(); // Gets packet that is waiting
    30. void connectToServer(); // Connect to the server using name, host, port, authname, password attributes given during object creation
    31. void disconnectFromServer(); // Disconnect from the currently connected server
    32. QString getName(); // Returns the name given to this connection
    33. QString getHostName(); // Returns the hostname of the server that this object connects to
    34. int getPortNumber(); // Returns the port number used to connect
    35. QString getUserName(); // Returns the username used to log onto server
    36. QString getPassword(); // Returns the password associated with the username used to log onto the server
    37. QString getFilterString(); // Returns the filter string that was set for this server
    38. bool isWriteEnabled(); // Is this instance able to write to socket?
    39. void setWriteEnabled(bool writeEnabled); // Sets if this socket can be written to
    40.  
    41. // change any parameters of this connection
    42. void changeParameters(QString name, QString host, int port, QString authName, QString password, QString filterString);
    43. void writeToSocket(QString aprsPacket); // writes the packet that is sent to this connection
    44. bool isConnected(); // Connected to server??
    45. signals:
    46. void newData(QString); // Sends the latest packet to the controller object
    47. private slots:
    48. void newDataFromConnection(); // receives data from connection and emits newData
    49. };
    50.  
    51. #endif // SERVERCONNECTION_H
    52. [/QTCLASS]
    To copy to clipboard, switch view to plain text mode 

    ServerConnection.cpp
    Qt Code:
    1. [QTCLASS]
    2. #include "serverconnection.h"
    3.  
    4. // Creates and returns an object of ServerConnection type that has attributes that are passed to it via parameters
    5. ServerConnection::ServerConnection(QString name, QString host, int port, QString authName, QString password, QString filterString)
    6. {
    7. // The below 6 lines set the attributes of this object to those given to the constructor via parameters
    8. this->name = name;
    9. this->host = host;
    10. this->port = port;
    11. this->authName = authName;
    12. this->password = password;
    13. this->filterString = filterString;
    14. connected = false; // Not connected to server
    15. socket = new QTcpSocket(this); // Create a new QTcpSocket
    16. socket->setObjectName(name);
    17.  
    18. // Set up the authentication string
    19. authString = "user " + authName + " pass " + password + " vers QTAPRS v1 " + filterString + "\n";
    20. connect(socket, SIGNAL(readyRead()), this, SLOT(newDataFromConnection())); // Connect this socket's readyread signal to slot
    21. }
    22.  
    23. ServerConnection::ServerConnection(QString name, QString host, int port, QString authName, QString password, QString filterString, bool writeEnabled)
    24. {
    25. // The below 6 lines set the attributes of this object to those given to the constructor via parameters
    26. this->name = name;
    27. this->host = host;
    28. this->port = port;
    29. this->authName = authName;
    30. this->password = password;
    31. this->filterString = filterString;
    32. this->writeEnabled = writeEnabled;
    33. connected = false; // Not connected to server
    34. socket = new QTcpSocket(this); // Create a new QTcpSocket
    35. socket->setObjectName(name);
    36.  
    37. // Set up the authentication string
    38. authString = "user " + authName + " pass " + password + " vers QTAPRS v1 " + filterString + "\n";
    39. connect(socket, SIGNAL(readyRead()), this, SLOT(newDataFromConnection())); // Connect this socket's readyread signal to slot
    40. }
    41.  
    42. // Deletes the connection and takes care of all of the logistical stuff
    43. ServerConnection::~ServerConnection()
    44. {
    45. disconnectFromServer(); // disconnect socket from server
    46. delete socket; // delete socket
    47. }
    48.  
    49. // Forms a connection to the server that this object represents
    50. void ServerConnection::connectToServer()
    51. {
    52. socket->connectToHost(host,port,QAbstractSocket::ReadWrite);// connects to host
    53. socket->waitForConnected(30000); // Make sure we're connected (get rid of latency)
    54. qDebug() << "Socket State = " << socket->state();
    55.  
    56. if(socket->state() != QAbstractSocket::ConnectedState){
    57. QApplication::beep(); // We are not connected, beep to warn user
    58. qDebug() << "NOT CONNECTED!!\n";
    59. }
    60. else{// we are connected
    61. connected = true; // socket is connected
    62. qDebug() << "Authentication String = " << authString;
    63. authString.append("\n");
    64. writeToSocket(authString); // Send server authentication string
    65. qDebug() << "AuthString written to socket\n";
    66. }
    67. }
    68.  
    69.  
    70. // disconnect from the server this object represents
    71. void ServerConnection::disconnectFromServer()
    72. {
    73. connected = false; // socket is not connected
    74. socket->disconnect(); // disconnect from server
    75. socket->close(); // close socket
    76. }
    77.  
    78. // Change the attributes of this connection
    79. void ServerConnection::changeParameters(QString name, QString host, int port, QString authName, QString password, QString filterString)
    80. { // Change this objects local attributes
    81. this->name = name;
    82. this->host = host;
    83. this->port = port;
    84. this->authName = authName;
    85. this->password = password;
    86. this->filterString = filterString;
    87. socket->setObjectName(name);
    88. }
    89.  
    90. // Write aprsPacket (given as parameter) to the socket
    91. void ServerConnection::writeToSocket(QString aprsPacket)
    92. {
    93. QByteArray block = aprsPacket.toAscii(); // allocate a bytearray to send aprsPacket with
    94. socket->write(block); // write block to socket
    95. qDebug() << "Wrote { " + aprsPacket + " } to server " + this->host + "\n";
    96. }
    97.  
    98. // new packet has been received by connection
    99. void ServerConnection::newDataFromConnection()
    100. {
    101. emit this->newData(getPacket()); // emit the packet to whomever cares
    102. //qDebug() << "newData() emitted\n";
    103. }
    104.  
    105. // returns the QString form of the packet received from the server
    106. QString ServerConnection::getPacket()
    107. {
    108. char buf[1024]; // Create a buffer of type char to hold packet
    109. memset(buf,0,1024); // Zeros out memory that buffer is located at
    110. socket->read(buf,1024); // reads data from socket and stores to buf
    111. QString string(buf); // casts the char buffer to a string
    112. //qDebug() << string.toUtf8();
    113. return string.toUtf8(); // returns the string that represents packet
    114. }
    115.  
    116. // is this object currently connected to a server?
    117. bool ServerConnection::isConnected()
    118. {
    119. return this->connected; // returns true if connected or false if not connected
    120. }
    121.  
    122. // Returns the name given to this connection
    123. QString ServerConnection::getName()
    124. {
    125. return this->name;
    126. }
    127.  
    128. // Returns the hostname of this connection
    129. QString ServerConnection::getHostName()
    130. {
    131. return this->host;
    132. }
    133.  
    134. // Returns the port that this connection uses
    135. int ServerConnection::getPortNumber()
    136. {
    137. return this->port;
    138. }
    139.  
    140. // Returns the username used to authenticate with server
    141. QString ServerConnection::getUserName()
    142. {
    143. return this->authName;
    144. }
    145.  
    146. // Returns password used to authenticate with server
    147. QString ServerConnection::getPassword()
    148. {
    149. return this->password;
    150. }
    151.  
    152. // Returns the filterstring used to do server-side filtering of aprs data
    153. QString ServerConnection::getFilterString()
    154. {
    155. return this->filterString;
    156. }
    157.  
    158. bool ServerConnection::isWriteEnabled()
    159. {
    160. return this->writeEnabled;
    161. }
    162. [/QTCLASS]
    To copy to clipboard, switch view to plain text mode 

    ConnectionInterface.h
    Qt Code:
    1. [QTCLASS]
    2.  
    3. #ifndef CONNECTIONINTERFACE_H
    4. #define CONNECTIONINTERFACE_H
    5.  
    6. #include <serverconnection.h>
    7. #include <QVector>
    8.  
    9. class ConnectionInterface : public QObject
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. ConnectionInterface(); // constructor
    15. ~ConnectionInterface(); // deconstructor
    16.  
    17. // Creates a new connection and returns true if connection was made, false if not
    18. bool newConnection(QString name, QString host, int port, QString authName, QString password, QString filterString, bool writeEnabled);
    19. private:
    20. int _numConnections; // The total number of connections QVector::size()
    21. QVector<ServerConnection*> _connections; // QVector containing pointers to connection objects
    22. public:
    23. void connectToServer(int connectionID); // Connect to the server represented by connection connectionID
    24. void disconnectFromServer(int connectionID); // disconnect from the server represented by connection connectionID
    25.  
    26. // Change any of the parameters of the connection represented by connection connectionID
    27. void changeParameters(QString name, QString host, int port, QString authName, QString password, QString filterString, int connectionID);
    28. void writeToSocket(QString aprsPacket); // Write the packet aprsPacket to connection to server represented by connection connectionID
    29. bool isConnected(int connectionID); // Tests if the connection represented by connection connectionID is connected
    30. QString getName(int connectionID); // Returns the name given to this connection
    31. QString getHostName(int connectionID); // Returns the hostname of the server that this object connects to
    32. int getPortNumber(int connectionID()); // Returns the port number used to connect
    33. QString getUserName(int connectionID); // Returns the username used to log onto server
    34. QString getPassword(int connectionID); // Returns the password associated with the username used to log onto the server
    35. QString getFilterString(int connectionID); // Returns the filter string that was set for this server
    36. void deleteConnection(int connectionID);
    37. public slots:
    38. void getData(QString data); // Slot that catches all emitted signals
    39. signals:
    40. void newData(QString); // emitted containing new packet
    41. };
    42.  
    43. #endif // CONNECTIONINTERFACE_H
    44. [/QTCLASS]
    To copy to clipboard, switch view to plain text mode 

    ConnectionInteface.cpp
    Qt Code:
    1. [QTCLASS]
    2. #include "connectioninterface.h"
    3.  
    4. ConnectionInterface::ConnectionInterface()
    5. {
    6. _numConnections = _connections.size(); // Set _numConnections to size of connection vector
    7. }
    8.  
    9. ConnectionInterface::~ConnectionInterface()
    10. {
    11. qDeleteAll(_connections.begin(), _connections.end()); // Delete all objects pointed to in the vector
    12. _connections.clear(); // Clear the pointers in the vector
    13. }
    14.  
    15. void ConnectionInterface::connectToServer(int connectionID)
    16. {
    17. if(_connections.size() > connectionID)
    18. {
    19. if(!_connections[connectionID]->isConnected())
    20. {
    21. _connections[connectionID]->connectToServer();
    22. }
    23. else
    24. {
    25. QApplication::beep();
    26. }
    27. }
    28. else
    29. {
    30. QApplication::beep();
    31. }
    32. }
    33.  
    34. void ConnectionInterface::changeParameters(QString name, QString host, int port, QString authName, QString password, QString filterString, int connectionID)
    35. {
    36. if(_connections.size() > connectionID)
    37. {
    38. _connections[connectionID]->changeParameters(name,host,port,authName,password,filterString);
    39. }
    40. else
    41. {
    42. QApplication::beep();
    43. }
    44. }
    45.  
    46. void ConnectionInterface::disconnectFromServer(int connectionID)
    47. {
    48. if(_connections.size() > connectionID)
    49. {
    50. if(_connections[connectionID]->isConnected())
    51. {
    52. _connections[connectionID]->disconnectFromServer();
    53. }
    54. else
    55. {
    56. QApplication::beep();
    57. }
    58. }
    59. else
    60. {
    61. QApplication::beep();
    62. }
    63. }
    64.  
    65. void ConnectionInterface::writeToSocket(QString aprsPacket)
    66. {
    67. for(int i = 0; i < _connections.size(); i++)
    68. {
    69. if(_connections[i]->isWriteEnabled())
    70. {
    71. _connections[i]->writeToSocket(aprsPacket);
    72. }
    73. }
    74. }
    75.  
    76. bool ConnectionInterface::isConnected(int connectionID)
    77. {
    78. if(_connections.size() > connectionID)
    79. {
    80. if(_connections[connectionID]->isConnected())
    81. {
    82. return true;
    83. }
    84. else
    85. {
    86. return false;
    87. }
    88. }
    89. else
    90. {
    91. return false;
    92. }
    93. }
    94.  
    95. bool ConnectionInterface::newConnection(QString name, QString host, int port, QString authName, QString password, QString filterString, bool writeEnabled)
    96. {
    97. ServerConnection* connection = new ServerConnection(name,host,port,authName,password,filterString,writeEnabled); // Creates a new connection Object
    98. connect(connection,SIGNAL(newData(QString)),this,SLOT(getData(QString))); // connects the signal emitted when a new packet is received on this connection
    99. _connections.push_back(connection); // Add this new connection object to the _connections vector
    100. }
    101.  
    102. void ConnectionInterface::getData(QString data)
    103. {
    104. qDebug() << data;
    105. emit newData(data); // New packet from a connection
    106. }
    107. [/QTCLASS]
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance for any information you can offer to help me fix this!

    Gadgetman53
    Last edited by Gadgetman53; 9th April 2011 at 23:59.

  2. #2
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QVector of pointers to custom classes that contain sockets

    any ideas anyone??

    Gadgetman53

  3. #3
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QVector of pointers to custom classes that contain sockets

    Figured this one out.

    For future reference to anyone having this issue, javaaprs (the server that runs and this project was to connect to) does not support multiple AUTHENTICATED instances of the same user.

Similar Threads

  1. Replies: 1
    Last Post: 22nd December 2010, 08:35
  2. Setting Custom defined classes to QVariant
    By node_ex in forum Qt Programming
    Replies: 0
    Last Post: 11th August 2010, 12:39
  3. QWebView custom JS Classes?
    By ts230 in forum Qt Programming
    Replies: 0
    Last Post: 7th April 2010, 05:17
  4. QVariant, QtDBus, pointers & custom classes
    By zuck in forum Qt Programming
    Replies: 7
    Last Post: 1st October 2009, 14:11
  5. UNIX Sockets vs. Qt Sockets Help
    By jmqt in forum Newbie
    Replies: 3
    Last Post: 6th July 2009, 15:26

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.