Fraxinus  16.5.0-fx-rc1
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxImageServer.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 #include "cxImageServer.h"
34 
35 #include "cxLogger.h"
36 #include "cxTypeConversions.h"
37 #include <iostream>
38 #include <QCoreApplication>
39 #include <QHostAddress>
40 #include <QNetworkInterface>
41 #include <QTcpSocket>
43 //#include "cxSender.h"
45 
46 namespace cx
47 {
48 
49 ImageServer::ImageServer(QObject* parent) :
50  QTcpServer(parent)
51 {}
52 
54 {
55  bool ok = false;
56 
57  StringMap args = cx::extractCommandlineOptions(QCoreApplication::arguments());
59  if(!mImageSender)
60  return false;
61 
62  ok = true;
63 
64  return ok;
65 }
66 
68 {
69  bool started = this->listen(QHostAddress::Any, port);
70 
71  if (started)
72  {
73  //Find IP adresses
74  std::cout << "Server IP adresses: " << std::endl;
75  foreach(QNetworkInterface interface, QNetworkInterface::allInterfaces())
76  {
77  if (interface.flags().testFlag(QNetworkInterface::IsRunning))
78  foreach (QNetworkAddressEntry entry, interface.addressEntries())
79  {
80  if ( interface.hardwareAddress() != "00:00:00:00:00:00" && entry.ip().toString() != "127.0.0.1" && entry.ip().toString().contains(".") )
81  std::cout << string_cast(interface.name()) << " " << entry.ip().toString() << std::endl;
82  }
83  }
84 
85  std::cout << QString("Server is listening to port %2").arg(this->serverPort()).toStdString() << std::endl;
86  return true;
87  }
88  else
89  {
90  std::cout << "Server failed to start. Error: " << this->errorString().toStdString() << std::endl;
91  return false;
92  }
93 }
94 
96 {
97 }
98 
99 void ImageServer::incomingConnection(qintptr socketDescriptor)
100 {
101  std::cout << "Server: Incoming connection..." << std::endl;
102 
103  if (mSocket != 0)
104  {
105  reportError("The image server can only handle a single connection.");
106  return;
107  }
108 
109  mSocket = new QTcpSocket();
110  connect(mSocket, SIGNAL(disconnected()), this, SLOT(socketDisconnectedSlot()));
111  mSocket->setSocketDescriptor(socketDescriptor);
112  QString clientName = mSocket->localAddress().toString();
113  report("Connected to "+clientName+". Session started.");
114  SenderPtr sender(new GrabberSenderQTcpSocket(mSocket));
115 
116  mImageSender->startStreaming(sender);
117 }
118 
119 void ImageServer::socketDisconnectedSlot()
120 {
121  if (mImageSender)
122  mImageSender->stopStreaming();
123 
124  if (mSocket)
125  {
126  QString clientName = mSocket->localAddress().toString();
127  report("Disconnected from "+clientName+". Session ended.");
128  mSocket->deleteLater();
129  }
130 }
131 
133 {
134  std::cout << getArgumentHelpText(qApp->applicationName());
135  std::cout << std::endl;
136  std::cout << std::endl;
137  std::cout << "Press Ctrl + C to close the server."<< std::endl;
138  std::cout << std::endl;
139 }
140 
141 QString ImageServer::getArgumentHelpText(QString applicationName)
142 {
143  std::stringstream ss;
145 
146  ss << "Usage: " << applicationName << " (--arg <argval>)*" << std::endl;
147  ss << " --port : Tcp/IP port # (default=18333)" << std::endl;
148  ss << " --type : Grabber type (default=" << factory.getDefaultSenderType().toStdString() << ")"
149  << std::endl;
150  ss << std::endl;
151  ss << " Select one of the types below:" << std::endl;
152 
153  QStringList types = factory.getSenderTypes();
154  for (int i = 0; i < types.size(); ++i)
155  {
156  QStringList args = factory.getArgumentDescription(types[i]);
157  ss << std::endl;
158  ss << " type = " << types[i].toStdString() << std::endl;
159  for (int j = 0; j < args.size(); ++j)
160  ss << " " << args[j].toStdString() << std::endl;
161  }
162  return qstring_cast(ss.str());
163 }
164 
165 }
QString qstring_cast(const T &val)
void reportError(QString msg)
Definition: cxLogger.cpp:92
QStringList getSenderTypes() const
all available sender types
std::string string_cast(const T &val)
ImageServer(QObject *parent=NULL)
std::map< QString, QString > StringMap
virtual ~ImageServer()
static void printHelpText()
static QString getArgumentHelpText(QString applicationName)
QStringList getArgumentDescription(QString type) const
arguments for one streamer
Factory class for creating streamer objects.
void report(QString msg)
Definition: cxLogger.cpp:90
void incomingConnection(qintptr socketDescriptor)
bool startListen(int port)
StringMap extractCommandlineOptions(QStringList cmdline)
boost::shared_ptr< Sender > SenderPtr
Definition: cxSender.h:85