CFEL - ASG Software Suite  2.5.0
CASS
jocassview/tcpclient.cpp
Go to the documentation of this file.
1 //Copyright (C) 2011 Lutz Foucar
2 
3 /**
4  * @file jocassview/tcpclient.cpp file contains the classes connect to cass
5  *
6  * @author Lutz Foucar
7  */
8 
9 #include <QtCore/QDebug>
10 #include <QtCore/QFuture>
11 
12 #if QT_VERSION >= 0x050000
13 #include <QtConcurrent/QtConcurrent>
14 #include <QtWidgets/QMessageBox>
15 #else
16 #include <QtCore/QtConcurrentRun>
17 #include <QtGui/QMessageBox>
18 #endif
19 
20 #include <QtTest/QTest>
21 
22 #include "tcpclient.h"
23 
24 #include "id_list.h"
25 #include "soapCASSsoapProxy.h"
26 #include "CASSsoap.nsmap"
27 #include "result.hpp"
28 
29 using namespace jocassview;
30 using namespace std;
31 
33  : _transferredBytes(0)
34 {
35 }
36 
38 {
39 }
40 
42 {
43  CASSsoapProxy client;
44  client.soap_endpoint = _server.c_str();
45 
46  bool ret(false);
47  QFuture<int> future = QtConcurrent::run(&client,&CASSsoapProxy::getHistogram,
48  key.toStdString(), id, &ret);
49  while (future.isRunning())
50  {
51  QCoreApplication::processEvents(QEventLoop::AllEvents);
52  QTest::qSleep(10);
53  }
54  if( (future.result() != SOAP_OK) || !ret)
55  return result_t::shared_pointer();
56  soap_multipart::iterator attachment(client.dime.begin());
57  if(client.dime.end() == attachment)
58  return result_t::shared_pointer();
59  _transferredBytes += (*attachment).size;
61  cass::Serializer serializer( std::string((char *)(*attachment).ptr, (*attachment).size) );
62  serializer >> *result;
63  return result;
64 }
65 
67 {
68  /** set up the client */
69  CASSsoapProxy client;
70  client.soap_endpoint = _server.c_str();
71  soap_set_dime(&client);
72  bool success;
73  /** serialize the id list */
74  cass::Serializer serializer;
75  IdList idlist(list);
76  idlist.serialize(serializer);
77  string data(serializer.buffer());
78  /** add the serialized list to the dime attachment and sent the request */
79  soap_set_dime_attachment(&client, (char*)data.data(), data.size(),
80  "application/processorList", "0", 0, NULL);
81  int status = client.getResults(false, &success);
82  /** create the container for the results */
84  /** when the communication failed return here */
85  if (status != SOAP_OK || !success)
86  return results;
87  /** create a deserializing object and retrieve all results from it */
88  soap_multipart::iterator attachment(client.dime.begin());
89  if(client.dime.end() == attachment)
90  return results;
91  cass::Serializer deserializer(std::string((char*)(*attachment).ptr,(*attachment).size));
92  for (int i(0); i<list.size(); ++i)
93  {
95  deserializer >> *result;
96  results.push_back(result);
97  }
98  return results;
99 }
100 
102 {
103  CASSsoapProxy client;
104  client.soap_endpoint = _server.c_str();
105 
106  bool ret(true);
107  QFuture<int> future = QtConcurrent::run(&client,&CASSsoapProxy::getPostprocessorIds, &ret);
108  while (future.isRunning())
109  {
110  QCoreApplication::processEvents(QEventLoop::AllEvents);
111  QTest::qSleep(10);
112  }
113  if( (future.result() != SOAP_OK) || !ret)
114  {
115  QMessageBox::information(0, tr("TcpClient"),
116  tr("Error resultNames: No communication to '") +
117  QString::fromStdString(_server)+ tr("' possible."));
118  return QStringList();
119  }
120  soap_multipart::iterator attachment (client.dime.begin());
121  if(client.dime.end() == attachment)
122  {
123  QMessageBox::information(0, tr("TcpClient"),
124  tr("Error resultNames: Server '")+
125  QString::fromStdString(_server) + tr("'didn't send data"));
126  return QStringList();
127  }
128 // qDebug() << "TCPClient: DIME attachment:" << endl
129 // << "TCPClient: Memory=" << (void*)(*attachment).ptr << endl
130 // << "TCPClient: Size=" << (*attachment).size << endl
131 // << "TCPClient: Type=" << ((*attachment).type?(*attachment).type:"null") << endl
132 // << "TCPClient: ID=" << ((*attachment).id?(*attachment).id:"null") << endl;
133  _transferredBytes += (*attachment).size;
134  cass::Serializer serializer( std::string((char *)(*attachment).ptr, (*attachment).size) );
135  IdList list(serializer);
136 // qDebug() << list.getList();
137  return list.getList();
138 }
139 
141 {
142  return QString("TCPClient");
143 }
144 
146 {
147  return _transferredBytes;
148 }
149 
151 {
152  CASSsoapProxy client;
153  client.soap_endpoint = _server.c_str();
154 
155  bool ret(false);
156  QFuture<int> future = QtConcurrent::run(&client,&CASSsoapProxy::readini,0,&ret);
157  while (future.isRunning())
158  {
159  QCoreApplication::processEvents(QEventLoop::AllEvents);
160  QTest::qSleep(10);
161  }
162  if(!ret)
163  QMessageBox::information(0, tr("TcpClient"),
164  tr("Error: Cannot reload the ini file "));
165 }
166 
167 void TCPClient::broadcastCommand(const QString &command) const
168 {
169  CASSsoapProxy client;
170  client.soap_endpoint = _server.c_str();
171 
172  bool ret(false);
173  QFuture<int> future = QtConcurrent::run(&client,&CASSsoapProxy::controlDarkcal,
174  command.toStdString(), &ret);
175  while (future.isRunning())
176  {
177  QCoreApplication::processEvents(QEventLoop::AllEvents);
178  QTest::qSleep(10);
179  }
180  if(!ret)
181  QMessageBox::information(0, tr("TcpClient"),
182  tr("Error: Cannot broadcast ") + command);
183 }
184 
185 void TCPClient::sendCommandTo(const QString &key, const QString &command) const
186 {
187  CASSsoapProxy client;
188  client.soap_endpoint = _server.c_str();
189 
190  bool ret(false);
191  QFuture<int> future = QtConcurrent::run(&client,&CASSsoapProxy::receiveCommand,
192  key.toStdString(), command.toStdString(),
193  &ret);
194  while (future.isRunning())
195  {
196  QCoreApplication::processEvents(QEventLoop::AllEvents);
197  QTest::qSleep(10);
198  }
199  if(!ret)
200  QMessageBox::information(0, tr("TcpClient"),
201  tr("Error: Cannot send '") + command + tr("' to '")+
202  key +tr("'"));
203 }
204 
205 void TCPClient::setServer(const QString &serverstring)
206 {
207  _server = serverstring.toStdString();
208 }
209 
211 {
212  CASSsoapProxy client;
213  client.soap_endpoint = _server.c_str();
214 
215  bool ret(false);
216  QFuture<int> future = QtConcurrent::run(&client,&CASSsoapProxy::quit, &ret);
217  while (future.isRunning())
218  {
219  QCoreApplication::processEvents(QEventLoop::AllEvents);
220  QTest::qSleep(10);
221  }
222  if(!ret)
223  QMessageBox::information(0, tr("TcpClient"),
224  tr("Error: Cannot communicate quitServer command."));
225 }
226 
228 {
229  CASSsoapProxy client;
230  client.soap_endpoint = _server.c_str();
231 
232  bool ret(false);
233  QFuture<int> future = QtConcurrent::run(&client,&CASSsoapProxy::clearHistogram, key.toStdString(), &ret);
234  while (future.isRunning())
235  {
236  QCoreApplication::processEvents(QEventLoop::AllEvents);
237  QTest::qSleep(10);
238  }
239  if(!ret)
240  QMessageBox::information(0, tr("TcpClient"),
241  tr("Error: Cannot communicate clearHistogram command."));
242 }
size_t _transferredBytes
the amount of bytes transferred
const QStringList & getList() const
getter for the internal list
void reloadIni() const
reload .ini file
void quitServer() const
tell the server to quit
virtual ~TCPClient()
destructor
std::tr1::shared_ptr< self_type > shared_pointer
a shared pointer of this class
Definition: result.hpp:323
file contains the classes connect to cass
STL namespace.
void broadcastCommand(const QString &command) const
broadcast a command to all processors in the server
result classes
tr(const char *sourceText, const char *disambiguation=0, int n=-1)
information(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons=Ok, StandardButton defaultButton=NoButton)
QString type() const
return the type of source we are
void setServer(const QString &serverstring)
change the server to connect to
fromStdString(const std::string &str)
processEvents(QEventLoop::ProcessEventsFlags flags=QEventLoop::AllEvents)
A string serializer.
Definition: serializer.hpp:287
void clearHistogram(QString key) const
clear the histogram of a processor
const std::string buffer() const
retrieve a const reference to the compressed string.
Definition: serializer.hpp:329
QStringList resultNames()
retrieve the list of available histograms
size_t receivedBytes() const
retrieve the transferred bytes
auxiliary data[Processor]
cass::Result< float > result_t
define the result type
Definition: data_source.h:30
file contains the classes that can serialize the key list
std::string _server
the server string
void sendCommandTo(const QString &key, const QString &command) const
broadcast a command to all processors in the server
push_back(const T &value)
QVector< result_t::shared_pointer > results(const QStringList &list)
retrieve a list of results all with the same id
result_t::shared_pointer result(const QString &key, quint64 id=0)
retrieve a specific result
TCPClient()
default constructor
void serialize(cass::SerializerBackend &out) const
implmented but doesn't do anything
and the shutter status[Processor]