Fotobot
Get data from your photovoltaic plant
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
SocketList Class Reference
Inheritance diagram for SocketList:

Public Member Functions

QTcpSocket * socket (int line)
 Returns socket associated with line. More...
 
void addLine (int line, const QString &hostname, int port)
 Adds new line to list. More...
 
void open (int line)
 Opens socket for line. More...
 

Static Public Member Functions

static SocketListsocketList (QObject *parent=NULL)
 

Private Member Functions

 SocketList (QObject *parent)
 

Private Attributes

QHash< int, QTcpSocket * > m_line_socket
 
QHash< int, QString > m_line_hostname
 
QHash< int, int > m_line_port
 
QThread * m_parentThread
 

Static Private Attributes

static SocketListm_socketList = NULL
 

Detailed Description

Definition at line 19 of file socketlist.h.

Member Function Documentation

void SocketList::addLine ( int  line,
const QString &  hostname,
int  port 
)

Adds new line to list.

Socket is created and opened later since it runs in different thread

Definition at line 25 of file socketlist.cpp.

25  {
26  if (m_line_socket.contains(line)) {
27  return;
28  }
29 
30  m_line_hostname[line] = hostname;
31  m_line_port [line] = port;
32  m_line_socket [line] = NULL;
33 
34 }
void SocketList::open ( int  line)

Opens socket for line.

Exceptions
QStringwhen en arror occured. Application can call open(int) again to recover from error

If the line does not exist, the application fails

Definition at line 37 of file socketlist.cpp.

37  {
38  qDebug() << "SocketList::open()" << line;
39 
40  if (m_parentThread != QThread::currentThread()) {
41  qFatal("Parent's thread not equals to currentThread in SocketList::open");
42  }
43 
44  if (!m_line_socket.contains(line)) {
45  qFatal("SocketList::open(line) line does not exist");
46  }
47 
48  if (m_line_socket[line] != NULL) {
49  QTcpSocket *socket = m_line_socket[line];
50  socket->close();
51  socket->deleteLater();
52  qDebug() << "SocketList::open() delete socket" << socket;
53  }
54 
55  QTcpSocket *socket = new QTcpSocket(this);
56  socket->connectToHost( m_line_hostname[line], m_line_port[line]);
57  if (!socket->waitForConnected()) {
58  qDebug() << "SocketList::open() could not open to host" << m_line_hostname[line] << m_line_port[line] << socket;
59  socket->deleteLater();
60  throw tr("Could not connect to %1:%2").arg(m_line_hostname[line]).arg(m_line_port[line]);
61  }
62 
63  m_line_socket[line] = socket;
64 }
QTcpSocket * socket(int line)
Returns socket associated with line.
Definition: socketlist.cpp:67
QTcpSocket * SocketList::socket ( int  line)

Returns socket associated with line.

Exceptions
QStringwhen an error occured. Application should call open(int) to recover from error

Definition at line 67 of file socketlist.cpp.

67  {
68  if (m_parentThread != QThread::currentThread()) {
69  qFatal("Parent's thread not equals to currentThread in SocketList::open");
70  }
71 
72  if (!m_line_socket.contains(line)) {
73  throw tr("Socket for line %1 does not exists").arg(line);
74  }
75 
76  QTcpSocket *socket = m_line_socket[line];
77  if (socket == NULL) {
78  open(line);
79  socket = m_line_socket[line];
80  }
81 
82  return socket;
83 }
QTcpSocket * socket(int line)
Returns socket associated with line.
Definition: socketlist.cpp:67
void open(int line)
Opens socket for line.
Definition: socketlist.cpp:37

The documentation for this class was generated from the following files: