CustusX  15.4.0-beta
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 
46 namespace cx
47 {
48 
49 
50 std::vector<PropertyPtr> LocalServerStreamerArguments::getSettings(QDomElement root)
51 {
52  std::vector<PropertyPtr> retval;
53  retval.push_back(this->getRunLocalServerOption(root));
54  retval.push_back(this->getLocalServerNameOption(root));
55  return retval;
56 }
57 
59 {
60  BoolPropertyPtr retval;
61 
62 #ifdef __APPLE__
63  bool defaultValue = true; // problems with opencv on mac - cannot close
64 #else
65  bool defaultValue = false;
66 #endif
67 
68 
69  retval = BoolProperty::initialize("runlocalserver", "Run as separate process",
70  "Run streamer in a separate process",
71  defaultValue, root);
72  retval->setAdvanced(false);
73  retval->setGroup("Connection");
74  return retval;
75 }
77 {
78  StringPropertyPtr retval;
79  QString defaultValue = this->getOpenIGTLinkServer().join(" ");
80  retval = StringProperty::initialize("localservername", "Server Name",
81  "Name of server executable, used only if Run Local Server is set.",
82  defaultValue, root);
83  retval->setGuiRepresentation(StringPropertyBase::grFILENAME);
84  retval->setAdvanced(false);
85  retval->setGroup("Connection");
86  return retval;
87 }
88 
93 QStringList LocalServerStreamerArguments::checkGrabberServerExist(QString path, QString filename)
94 {
95  QStringList retval;
96  path = QDir::cleanPath(path);
97  if (QDir(path).exists(filename))
98  retval << QDir(DataLocations::getBundlePath()).relativeFilePath(path + "/" + filename);
99 #ifdef __APPLE__
100  if (retval.isEmpty())
101  {
102  QString bundledPath = QString("%1/%2.app/Contents/MacOS").arg(path).arg(filename);
103  if (QDir(bundledPath).exists(filename))
104  retval << QDir(DataLocations::getBundlePath()).relativeFilePath(bundledPath + "/" + filename);
105  }
106 
107 #endif
108  return retval;
109 }
110 
111 QStringList LocalServerStreamerArguments::getOpenIGTLinkServer()
112 {
113  QString filename = "OpenIGTLinkServer";
114 #ifdef WIN32
115  filename = "OpenIGTLinkServer.exe";
116 // postfix = "--in_width 800 --in_height 600"; // if needed, add as gui options in the openCV group
117 #endif
118  return this->getGrabberServer(filename);
119 }
120 
121 QStringList LocalServerStreamerArguments::getGrabberServer(QString filename)
122 {
123 
124  QStringList result;
125 #ifdef __APPLE__
126  // run from installed folder on mac
127  result = this->checkGrabberServerExist(qApp->applicationDirPath(), filename);
128  if (!result.isEmpty())
129  return result;
130 #endif
131  // run from installed or build bin folder
132  result = this->checkGrabberServerExist(DataLocations::getBundlePath(), filename);
133  if (!result.isEmpty())
134  return result;
135  else
136  reportWarning("Failed to locate default grabber server");
137 
138  return result;
139 }
140 
141 
145 
146 
147 
149 {
150  bool useLocalServer = LocalServerStreamerArguments().getRunLocalServerOption(root)->getValue();
151 
152  if (!useLocalServer)
153  return StreamerPtr();
154 
155  QStringList cmdlineArguments;
156  for (StringMap::iterator i=args.begin(); i!=args.end(); ++i)
157  cmdlineArguments << i->first << i->second;
158 
159  QString localServer = LocalServerStreamerArguments().getLocalServerNameOption(root)->getValue();
160  boost::shared_ptr<LocalServerStreamer> streamer;
161  streamer.reset(new LocalServerStreamer(localServer, cmdlineArguments.join(" ")));
162 
163  return streamer;
164 }
165 
166 LocalServerStreamer::LocalServerStreamer(QString serverName, QString serverArguments) :
167  mServerName(serverName),
168  mServerArguments(serverArguments)
169 {
170  mLocalVideoServerProcess.reset(new ProcessWrapper(QString("Local Video Server: %1").arg(mServerName)));
171 
172  boost::shared_ptr<IGTLinkClientStreamer> igtLinkStreamer(new IGTLinkClientStreamer());
173  int defaultport = 18333;
174  igtLinkStreamer->setAddress("Localhost", defaultport);
175  mBase = igtLinkStreamer;
176 }
177 
179 {
180 
181 }
182 
184 {
185  mLocalVideoServerProcess->launchWithRelativePath(mServerName, mServerArguments.split(" "));
186 
187  this->waitForServerStart();
188  if (!this->localVideoServerIsRunning())
189  {
190  reportError("Local server failed to start");
191  return false;
192  }
193 
194  return mBase->startStreaming(sender);
195 }
196 
197 void LocalServerStreamer::waitForServerStart()
198 {
199  int waitTime = 5000;
200  while (waitTime > 0)
201  {
202  qApp->processEvents();
203  if (this->localVideoServerIsRunning())
204  return;
205  int interval = 50;
206  sleep_ms(interval);
207  waitTime -= interval;
208  }
209 }
210 
212 {
213  mBase->stopStreaming();
214 
215  if (mLocalVideoServerProcess->getProcess())
216  mLocalVideoServerProcess->getProcess()->close();
217 }
218 
220 {
221  return "LocalServer";
222 }
223 
224 bool LocalServerStreamer::localVideoServerIsRunning()
225 {
226  if (!mLocalVideoServerProcess->getProcess())
227  return false;
228  return this->mLocalVideoServerProcess->getProcess()->state() == QProcess::Running;
229 }
230 
231 
232 } // namespace cx
static BoolPropertyPtr initialize(const QString &uid, QString name, QString help, bool value, QDomNode root=QDomNode())
void reportError(QString msg)
Definition: cxLogger.cpp:92
static StreamerPtr createStreamerIfEnabled(QDomElement root, StringMap args)
LocalServerStreamer(QString serverName, QString serverArguments)
StringPropertyBasePtr getLocalServerNameOption(QDomElement root)
BoolPropertyBasePtr getRunLocalServerOption(QDomElement root)
virtual bool startStreaming(SenderPtr sender)
boost::shared_ptr< class StringProperty > StringPropertyPtr
std::map< QString, QString > StringMap
void reportWarning(QString msg)
Definition: cxLogger.cpp:91
boost::shared_ptr< class BoolPropertyBase > BoolPropertyBasePtr
static StringPropertyPtr initialize(const QString &uid, QString name, QString help, QString value, QStringList range, QDomNode root=QDomNode())
boost::shared_ptr< class BoolProperty > BoolPropertyPtr
boost::shared_ptr< Sender > SenderPtr
Definition: cxSender.h:88
static QString getBundlePath()
return the folder where the bundle or executable are located.
void sleep_ms(int ms)
boost::shared_ptr< class Streamer > StreamerPtr
std::vector< PropertyPtr > getSettings(QDomElement root)
boost::shared_ptr< class StringPropertyBase > StringPropertyBasePtr