Fraxinus  2023.01.05-dev+develop.0da12
An IGT application
cxLocalServerStreamerServer.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 =========================================================================*/
12 
13 #include "cxStringProperty.h"
14 #include "cxDoubleProperty.h"
15 #include "cxBoolProperty.h"
17 #include "cxImageStreamerOpenCV.h"
18 #include "cxUtilHelpers.h"
19 #include "cxLogger.h"
20 #include "QApplication"
21 #include <QDir>
22 #include "cxDataLocations.h"
23 #include "cxTypeConversions.h"
24 #include "cxFilePathProperty.h"
25 
26 
27 namespace cx
28 {
29 
30 
31 std::vector<PropertyPtr> LocalServerStreamerArguments::getSettings(QDomElement root)
32 {
33  std::vector<PropertyPtr> retval;
34  retval.push_back(this->getRunLocalServerOption(root));
35  retval.push_back(this->getLocalServerNameOption(root));
36  return retval;
37 }
38 
40 {
41  BoolPropertyPtr retval;
42 
43 #ifdef __APPLE__
44  bool defaultValue = true; // problems with opencv on mac - cannot close
45 #else
46  bool defaultValue = false;
47 #endif
48 
49  retval = BoolProperty::initialize("runlocalserver", "Run as separate process",
50  "Run streamer in a separate process",
51  defaultValue, root);
52  retval->setAdvanced(false);
53  retval->setGroup("Connection");
54  return std::move(retval);//Fixed copy bug on old compilers
55 }
56 
58 {
59  QString filename = "OpenIGTLinkServer";
60 #ifdef WIN32
61  filename += ".exe";
62 #endif
63 
64  QStringList paths = QStringList() << qApp->applicationDirPath();
65 #ifdef __APPLE__
66  // special case for running from the build tree, server built as bundle.
67  paths << QString("%1/%2.app/Contents/MacOS").arg(DataLocations::getBundlePath()).arg(filename);
68 #endif
69 
70  FilePathPropertyPtr retval;
71  retval = FilePathProperty::initialize("localservername", "Server Name",
72  "Name of server executable, used only if Run Local Server is set.",
73  filename,
74  paths,
75  root);
76  retval->setAdvanced(false);
77  retval->setGroup("Connection");
78  return retval;
79 }
80 
81 
85 
87 {
88  bool useLocalServer = LocalServerStreamerArguments().getRunLocalServerOption(root)->getValue();
89 
90  if (!useLocalServer)
91  return StreamerPtr();
92 
93  QStringList cmdlineArguments;
94  for (StringMap::iterator i=args.begin(); i!=args.end(); ++i)
95  cmdlineArguments << i->first << i->second;
96 
98  QString localServer = localServerProp->getEmbeddedPath().getAbsoluteFilepath();
99  boost::shared_ptr<LocalServerStreamer> streamer;
100  streamer.reset(new LocalServerStreamer(localServer, cmdlineArguments.join(" ")));
101 
102  return streamer;
103 }
104 
105 LocalServerStreamer::LocalServerStreamer(QString serverName, QString serverArguments) :
106  mServerName(serverName),
107  mServerArguments(serverArguments)
108 {
109  mLocalVideoServerProcess.reset(new ProcessWrapper(QString("Local Video Server: %1").arg(mServerName)));
110 
111  boost::shared_ptr<IGTLinkClientStreamer> igtLinkStreamer(new IGTLinkClientStreamer());
112  int defaultport = 18333;
113  igtLinkStreamer->setAddress("Localhost", defaultport);
114  mBase = igtLinkStreamer;
115 }
116 
118 {
119 }
120 
122 {
123  mSender = sender;
124  connect(mLocalVideoServerProcess.get(), &ProcessWrapper::stateChanged, this, &LocalServerStreamer::processStateChanged);
125  mLocalVideoServerProcess->launchWithRelativePath(mServerName, mServerArguments.split(" "));
126 }
127 
128 void LocalServerStreamer::processStateChanged()
129 {
130  if(mLocalVideoServerProcess->isRunning())
131  mBase->startStreaming(mSender);
132 }
133 
135 {
136  mBase->stopStreaming();
137 
138  if (mLocalVideoServerProcess->getProcess())
139  {
140  mLocalVideoServerProcess->getProcess()->close();
141  mLocalVideoServerProcess.reset();
142  }
143 }
144 
146 {
147  return localVideoServerIsRunning();
148 }
149 
150 bool LocalServerStreamer::localVideoServerIsRunning()
151 {
152  if (!mLocalVideoServerProcess || !mLocalVideoServerProcess->getProcess())
153  return false;
154  return this->mLocalVideoServerProcess->isRunning();
155 }
156 
157 
158 } // namespace cx
static BoolPropertyPtr initialize(const QString &uid, QString name, QString help, bool value, QDomNode root=QDomNode())
FilePathPropertyPtr getLocalServerNameOption(QDomElement root)
static StreamerPtr createStreamerIfEnabled(QDomElement root, StringMap args)
LocalServerStreamer(QString serverName, QString serverArguments)
BoolPropertyBasePtr getRunLocalServerOption(QDomElement root)
virtual void startStreaming(SenderPtr sender)
std::map< QString, QString > StringMap
boost::shared_ptr< class BoolPropertyBase > BoolPropertyBasePtr
static FilePathPropertyPtr initialize(const QString &uid, QString name, QString help, QString value, QStringList paths, QDomNode root=QDomNode())
boost::shared_ptr< class FilePathProperty > FilePathPropertyPtr
boost::shared_ptr< class BoolProperty > BoolPropertyPtr
boost::shared_ptr< Sender > SenderPtr
Definition: cxSender.h:64
SenderPtr mSender
Definition: cxStreamer.h:65
static QString getBundlePath()
return the folder where the bundle or executable are located.
boost::shared_ptr< class Streamer > StreamerPtr
std::vector< PropertyPtr > getSettings(QDomElement root)
Namespace for all CustusX production code.