CustusX  16.5
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  // special case for running from the build tree, server built as bundle.
88  paths << QString("%1/%2.app/Contents/MacOS").arg(DataLocations::getBundlePath()).arg(filename);
89 #endif
90 
91  FilePathPropertyPtr retval;
92  retval = FilePathProperty::initialize("localservername", "Server Name",
93  "Name of server executable, used only if Run Local Server is set.",
94  filename,
95  paths,
96  root);
97  retval->setAdvanced(false);
98  retval->setGroup("Connection");
99  return retval;
100 }
101 
102 
106 
108 {
109  bool useLocalServer = LocalServerStreamerArguments().getRunLocalServerOption(root)->getValue();
110 
111  if (!useLocalServer)
112  return StreamerPtr();
113 
114  QStringList cmdlineArguments;
115  for (StringMap::iterator i=args.begin(); i!=args.end(); ++i)
116  cmdlineArguments << i->first << i->second;
117 
119  QString localServer = localServerProp->getEmbeddedPath().getAbsoluteFilepath();
120  boost::shared_ptr<LocalServerStreamer> streamer;
121  streamer.reset(new LocalServerStreamer(localServer, cmdlineArguments.join(" ")));
122 
123  return streamer;
124 }
125 
126 LocalServerStreamer::LocalServerStreamer(QString serverName, QString serverArguments) :
127  mServerName(serverName),
128  mServerArguments(serverArguments)
129 {
130  mLocalVideoServerProcess.reset(new ProcessWrapper(QString("Local Video Server: %1").arg(mServerName)));
131 
132  boost::shared_ptr<IGTLinkClientStreamer> igtLinkStreamer(new IGTLinkClientStreamer());
133  int defaultport = 18333;
134  igtLinkStreamer->setAddress("Localhost", defaultport);
135  mBase = igtLinkStreamer;
136 }
137 
139 {
140 }
141 
143 {
144  mSender = sender;
145  connect(mLocalVideoServerProcess.get(), &ProcessWrapper::stateChanged, this, &LocalServerStreamer::processStateChanged);
146  mLocalVideoServerProcess->launchWithRelativePath(mServerName, mServerArguments.split(" "));
147 }
148 
149 void LocalServerStreamer::processStateChanged()
150 {
151  if(mLocalVideoServerProcess->isRunning())
152  mBase->startStreaming(mSender);
153 }
154 
156 {
157  mBase->stopStreaming();
158 
159  if (mLocalVideoServerProcess->getProcess())
160  {
161  mLocalVideoServerProcess->getProcess()->close();
162  mLocalVideoServerProcess.reset();
163  }
164 }
165 
167 {
168  return localVideoServerIsRunning();
169 }
170 
171 bool LocalServerStreamer::localVideoServerIsRunning()
172 {
173  if (!mLocalVideoServerProcess || !mLocalVideoServerProcess->getProcess())
174  return false;
175  return this->mLocalVideoServerProcess->isRunning();
176 }
177 
178 
179 } // 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:85
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)