CustusX  15.8
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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) 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 =========================================================================*/
33 
34 #include "cxStringProperty.h"
35 #include "cxDoubleProperty.h"
36 #include "cxBoolProperty.h"
38 #include "cxImageStreamerOpenCV.h"
39 #include "cxUtilHelpers.h"
40 #include "cxLogger.h"
41 #include "QApplication"
42 #include <QDir>
43 #include "cxDataLocations.h"
44 #include "cxTypeConversions.h"
45 #include "cxFilePathProperty.h"
46 
47 
48 namespace cx
49 {
50 
51 
52 std::vector<PropertyPtr> LocalServerStreamerArguments::getSettings(QDomElement root)
53 {
54  std::vector<PropertyPtr> retval;
55  retval.push_back(this->getRunLocalServerOption(root));
56  retval.push_back(this->getLocalServerNameOption(root));
57  return retval;
58 }
59 
61 {
62  BoolPropertyPtr retval;
63 
64 #ifdef __APPLE__
65  bool defaultValue = true; // problems with opencv on mac - cannot close
66 #else
67  bool defaultValue = false;
68 #endif
69 
70  retval = BoolProperty::initialize("runlocalserver", "Run as separate process",
71  "Run streamer in a separate process",
72  defaultValue, root);
73  retval->setAdvanced(false);
74  retval->setGroup("Connection");
75  return retval;
76 }
77 
79 {
80  QString filename = "OpenIGTLinkServer";
81 #ifdef WIN32
82  filename += ".exe";
83 #endif
84 
85  QStringList paths = QStringList() << qApp->applicationDirPath();
86 #ifdef __APPLE__
87  paths << QString("%1/%2.app/Contents/MacOS").arg(DataLocations::getBundlePath()).arg(filename);
88 #endif
89 
90  FilePathPropertyPtr retval;
91  retval = FilePathProperty::initialize("localservername", "Server Name",
92  "Name of server executable, used only if Run Local Server is set.",
93  filename,
94  paths,
95  root);
96  retval->setAdvanced(false);
97  retval->setGroup("Connection");
98  return retval;
99 }
100 
101 
105 
107 {
108  bool useLocalServer = LocalServerStreamerArguments().getRunLocalServerOption(root)->getValue();
109 
110  if (!useLocalServer)
111  return StreamerPtr();
112 
113  QStringList cmdlineArguments;
114  for (StringMap::iterator i=args.begin(); i!=args.end(); ++i)
115  cmdlineArguments << i->first << i->second;
116 
118  QString localServer = localServerProp->getEmbeddedPath().getAbsoluteFilepath();
119  boost::shared_ptr<LocalServerStreamer> streamer;
120  streamer.reset(new LocalServerStreamer(localServer, cmdlineArguments.join(" ")));
121 
122  return streamer;
123 }
124 
125 LocalServerStreamer::LocalServerStreamer(QString serverName, QString serverArguments) :
126  mServerName(serverName),
127  mServerArguments(serverArguments)
128 {
129  mLocalVideoServerProcess.reset(new ProcessWrapper(QString("Local Video Server: %1").arg(mServerName)));
130 
131  boost::shared_ptr<IGTLinkClientStreamer> igtLinkStreamer(new IGTLinkClientStreamer());
132  int defaultport = 18333;
133  igtLinkStreamer->setAddress("Localhost", defaultport);
134  mBase = igtLinkStreamer;
135 }
136 
138 {
139 }
140 
142 {
143  mSender = sender;
144  connect(mLocalVideoServerProcess.get(), &ProcessWrapper::stateChanged, this, &LocalServerStreamer::processStateChanged);
145  mLocalVideoServerProcess->launchWithRelativePath(mServerName, mServerArguments.split(" "));
146 }
147 
148 void LocalServerStreamer::processStateChanged()
149 {
150  if(mLocalVideoServerProcess->isRunning())
151  mBase->startStreaming(mSender);
152 }
153 
155 {
156  mBase->stopStreaming();
157 
158  if (mLocalVideoServerProcess->getProcess())
159  {
160  mLocalVideoServerProcess->getProcess()->close();
161  mLocalVideoServerProcess.reset();
162  }
163 }
164 
166 {
167  return localVideoServerIsRunning();
168 }
169 
170 bool LocalServerStreamer::localVideoServerIsRunning()
171 {
172  if (!mLocalVideoServerProcess || !mLocalVideoServerProcess->getProcess())
173  return false;
174  return this->mLocalVideoServerProcess->isRunning();
175 }
176 
177 
178 } // 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:88
SenderPtr mSender
Definition: cxStreamer.h:86
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)