Fraxinus  2023.01.05-dev+develop.0da12
An IGT application
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) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 #include "cxImageServer.h"
13 
14 #include "cxLogger.h"
15 #include "cxTypeConversions.h"
16 #include <iostream>
17 #include <QCoreApplication>
18 #include <QHostAddress>
19 #include <QNetworkInterface>
20 #include <QTcpSocket>
22 //#include "cxSender.h"
24 
25 namespace cx
26 {
27 
28 ImageServer::ImageServer(QObject* parent) :
29  QTcpServer(parent)
30 {}
31 
33 {
34  bool ok = false;
35 
36  StringMap args = cx::extractCommandlineOptions(QCoreApplication::arguments());
38  if(!mImageSender)
39  return false;
40 
41  ok = true;
42 
43  return ok;
44 }
45 
47 {
48  bool started = this->listen(QHostAddress::Any, port);
49 
50  if (started)
51  {
52  //Find IP adresses
53  std::cout << "Server IP adresses: " << std::endl;
54  foreach(QNetworkInterface interface, QNetworkInterface::allInterfaces())
55  {
56  if (interface.flags().testFlag(QNetworkInterface::IsRunning))
57  foreach (QNetworkAddressEntry entry, interface.addressEntries())
58  {
59  if ( interface.hardwareAddress() != "00:00:00:00:00:00" && entry.ip().toString() != "127.0.0.1" && entry.ip().toString().contains(".") )
60  std::cout << string_cast(interface.name()) << " " << entry.ip().toString() << std::endl;
61  }
62  }
63 
64  std::cout << QString("Server is listening to port %2").arg(this->serverPort()).toStdString() << std::endl;
65  return true;
66  }
67  else
68  {
69  std::cout << "Server failed to start. Error: " << this->errorString().toStdString() << std::endl;
70  return false;
71  }
72 }
73 
75 {
76 }
77 
78 void ImageServer::incomingConnection(qintptr socketDescriptor)
79 {
80  std::cout << "Server: Incoming connection..." << std::endl;
81 
82  if (mSocket != 0)
83  {
84  reportError("The image server can only handle a single connection.");
85  return;
86  }
87 
88  mSocket = new QTcpSocket();
89  connect(mSocket, SIGNAL(disconnected()), this, SLOT(socketDisconnectedSlot()));
90  mSocket->setSocketDescriptor(socketDescriptor);
91  QString clientName = mSocket->localAddress().toString();
92  report("Connected to "+clientName+". Session started.");
93  SenderPtr sender(new GrabberSenderQTcpSocket(mSocket));
94 
95  mImageSender->startStreaming(sender);
96 }
97 
98 void ImageServer::socketDisconnectedSlot()
99 {
100  if (mImageSender)
101  mImageSender->stopStreaming();
102 
103  if (mSocket)
104  {
105  QString clientName = mSocket->localAddress().toString();
106  report("Disconnected from "+clientName+". Session ended.");
107  mSocket->deleteLater();
108  }
109 }
110 
112 {
113  std::cout << getArgumentHelpText(qApp->applicationName());
114  std::cout << std::endl;
115  std::cout << std::endl;
116  std::cout << "Press Ctrl + C to close the server."<< std::endl;
117  std::cout << std::endl;
118 }
119 
120 QString ImageServer::getArgumentHelpText(QString applicationName)
121 {
122  std::stringstream ss;
124 
125  ss << "Usage: " << applicationName << " (--arg <argval>)*" << std::endl;
126  ss << " --port : Tcp/IP port # (default=18333)" << std::endl;
127  ss << " --type : Grabber type (default=" << factory.getDefaultSenderType().toStdString() << ")"
128  << std::endl;
129  ss << std::endl;
130  ss << " Select one of the types below:" << std::endl;
131 
132  QStringList types = factory.getSenderTypes();
133  for (int i = 0; i < types.size(); ++i)
134  {
135  QStringList args = factory.getArgumentDescription(types[i]);
136  ss << std::endl;
137  ss << " type = " << types[i].toStdString() << std::endl;
138  for (int j = 0; j < args.size(); ++j)
139  ss << " " << args[j].toStdString() << std::endl;
140  }
141  return qstring_cast(ss.str());
142 }
143 
144 }
QString qstring_cast(const T &val)
void reportError(QString msg)
Definition: cxLogger.cpp:71
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:69
void incomingConnection(qintptr socketDescriptor)
bool startListen(int port)
StringMap extractCommandlineOptions(QStringList cmdline)
boost::shared_ptr< Sender > SenderPtr
Definition: cxSender.h:64
Namespace for all CustusX production code.