8 #include "PlusConfigure.h" 9 #include "igsioCommon.h" 14 #include "vtkIGSIOTransformRepository.h" 17 #include <vtkSmartPointer.h> 18 #include <vtksys/CommandLineArguments.hxx> 21 PlusStatus ConnectClients(
int listeningPort, std::vector< vtkSmartPointer<vtkPlusOpenIGTLinkVideoSource> >& testClientList,
int numberOfClientsToConnect, vtkSmartPointer<vtkXMLDataElement> configRootElement)
23 if (configRootElement == NULL)
25 LOG_ERROR(
"PlusServer client configuration is missing");
29 int numberOfErrors = 0;
32 testClientList.clear();
34 for (
int i = 0;
i < numberOfClientsToConnect; ++
i)
36 vtkSmartPointer<vtkPlusOpenIGTLinkVideoSource> client = vtkSmartPointer<vtkPlusOpenIGTLinkVideoSource>::New();
37 client->SetDeviceId(
"OpenIGTLinkVideoReceiveDevice");
38 client->ReadConfiguration(configRootElement);
39 client->SetDeviceId(std::string(
"OpenIGTLinkVideoReceiveDevice") + igsioCommon::ToString<int>(
i));
40 client->SetServerAddress(
"127.0.0.1");
41 client->SetServerPort(listeningPort);
42 if (client->OutputChannelCount() == 0)
44 LOG_ERROR(
"No output channels in openIGTLink client.");
53 LOG_ERROR(
"Unable to retrieve the video source.");
56 client->SetBufferSize(*aChannel, 10);
57 client->SetMessageType(
"TrackedFrame");
58 igsioTransformName name(
"Image",
"Reference");
59 client->SetImageMessageEmbeddedTransformName(name);
64 LOG_ERROR(
"Client #" <<
i + 1 <<
" couldn't connect to server.");
69 LOG_DEBUG(
"Client #" <<
i + 1 <<
" successfully connected to server!");
73 LOG_ERROR(
"Client #" <<
i + 1 <<
" couldn't start recording frames.");
80 testClientList.push_back(client);
89 int numberOfErrors = 0;
90 for (
unsigned int i = 0;
i < testClientList.size(); ++
i)
94 LOG_ERROR(
"Client #" <<
i + 1 <<
" failed to stop recording");
100 LOG_ERROR(
"Client #" <<
i + 1 <<
" failed to disconnect from server");
105 LOG_DEBUG(
"Client #" <<
i + 1 <<
" successfully disconnected from server!");
112 vtkSmartPointer<vtkPlusOpenIGTLinkServer>
StartServer(
const std::string& inputConfigFileName)
115 std::string configFilePath = inputConfigFileName;
116 if (!vtksys::SystemTools::FileExists(configFilePath.c_str(),
true))
119 if (!vtksys::SystemTools::FileExists(configFilePath.c_str(),
true))
121 LOG_ERROR(
"Reading device set configuration file failed: " << inputConfigFileName <<
" does not exist in the current directory or in " <<
vtkPlusConfig::GetInstance()->GetDeviceSetConfigurationDirectory());
125 vtkSmartPointer<vtkXMLDataElement> configRootElement = vtkSmartPointer<vtkXMLDataElement>::Take(vtkXMLUtilities::ReadElementFromFile(configFilePath.c_str()));
126 if (configRootElement == NULL)
128 LOG_ERROR(
"Reading device set configuration file failed: syntax error in " << inputConfigFileName);
136 LOG_DEBUG(
"Device set configuration is read from file: " << inputConfigFileName);
137 std::ostringstream xmlFileContents;
138 igsioCommon::XML::PrintXML(xmlFileContents, vtkIndent(1), configRootElement);
139 LOG_DEBUG(
"Device set configuration file contents: " << std::endl << xmlFileContents.str());
141 LOG_INFO(
"Server status: Reading configuration.");
143 vtkSmartPointer<vtkPlusDataCollector> dataCollector = vtkSmartPointer<vtkPlusDataCollector>::New();
144 if (dataCollector->ReadConfiguration(configRootElement) !=
PLUS_SUCCESS)
146 LOG_ERROR(
"Datacollector failed to read configuration");
151 vtkSmartPointer<vtkIGSIOTransformRepository> transformRepository = vtkSmartPointer<vtkIGSIOTransformRepository>::New();
152 if (transformRepository->ReadConfiguration(configRootElement) !=
PLUS_SUCCESS)
154 LOG_ERROR(
"Transform repository failed to read configuration");
158 LOG_INFO(
"Server status: Connecting to devices.");
161 LOG_ERROR(
"Datacollector failed to connect to devices");
167 LOG_ERROR(
"Datacollector failed to start");
171 LOG_INFO(
"Server status: Starting servers.");
172 std::vector<vtkPlusOpenIGTLinkServer*> serverList;
173 for (
int i = 0;
i < configRootElement->GetNumberOfNestedElements(); ++
i)
175 vtkXMLDataElement* serverElement = configRootElement->GetNestedElement(
i);
176 if (STRCASECMP(serverElement->GetName(),
"PlusOpenIGTLinkServer") != 0)
182 vtkSmartPointer<vtkPlusOpenIGTLinkServer>
server = vtkSmartPointer<vtkPlusOpenIGTLinkServer>::New();
183 LOG_DEBUG(
"Initializing Plus OpenIGTLink server... ");
184 if (
server->Start(dataCollector, transformRepository, serverElement, configFilePath) !=
PLUS_SUCCESS)
186 LOG_ERROR(
"Failed to start OpenIGTLink server");
196 int main(
int argc,
char** argv)
199 bool printHelp(
false);
200 std::string inputConfigFileName;
201 std::string testingConfigFileName;
202 int verboseLevel = vtkPlusLogger::LOG_LEVEL_UNDEFINED;
204 const double WAIT_TIME_SEC = 5.0;
205 const int NUM_TEST_CLIENTS = 5;
207 vtksys::CommandLineArguments args;
208 args.Initialize(argc, argv);
210 args.AddArgument(
"--help", vtksys::CommandLineArguments::NO_ARGUMENT, &printHelp,
"Print this help.");
211 args.AddArgument(
"--server-config-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &inputConfigFileName,
"Name of the server configuration file.");
212 args.AddArgument(
"--verbose", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &verboseLevel,
"Verbose level (1=error only, 2=warning, 3=info, 4=debug, 5=trace)");
213 args.AddArgument(
"--testing-config-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &testingConfigFileName,
"Name of the testing configuration file");
217 std::cerr <<
"Problem parsing arguments." << std::endl;
218 std::cout <<
"Help: " << args.GetHelp() << std::endl;
224 std::cout << args.GetHelp() << std::endl;
230 if (inputConfigFileName.empty())
232 LOG_ERROR(
"--server-config-file argument is required!");
233 std::cout <<
"Help: " << args.GetHelp() << std::endl;
237 if (testingConfigFileName.empty())
239 LOG_ERROR(
"--testing-config-file argument is required!");
240 std::cout <<
"Help: " << args.GetHelp() << std::endl;
247 vtkSmartPointer<vtkPlusOpenIGTLinkServer>
server =
StartServer(inputConfigFileName);
250 LOG_ERROR(
"Unable to start server.");
254 vtkSmartPointer<vtkXMLDataElement> configRootElement = vtkSmartPointer<vtkXMLDataElement>::New();
257 LOG_ERROR(
"Unable to read test configuration from file " << testingConfigFileName.c_str());
261 std::vector< vtkSmartPointer<vtkPlusOpenIGTLinkVideoSource> > outTestClients;
264 LOG_ERROR(
"Unable to connect clients to PlusServer!");
268 LOG_INFO(
"Clients are connected");
270 const double commandQueuePollIntervalSec = 0.010;
271 const double startTime = vtkIGSIOAccurateTimer::GetSystemTime();
272 while (vtkIGSIOAccurateTimer::GetSystemTime() < startTime + WAIT_TIME_SEC)
274 server->ProcessPendingCommands();
277 vtkIGSIOAccurateTimer::DelayWithEventProcessing(commandQueuePollIntervalSec);
280 LOG_INFO(
"Requested testing time elapsed");
283 unsigned int numOfActuallyConnectedClients =
server->GetNumberOfConnectedClients();
284 if (numOfActuallyConnectedClients != outTestClients.size())
286 LOG_ERROR(
"Number of connected clients to PlusServer doesn't match the requirements (" 287 << numOfActuallyConnectedClients <<
" out of " << outTestClients.size() <<
").");
293 LOG_INFO(
"Disconnecting clients...");
296 LOG_ERROR(
"Unable to disconnect clients from PlusServer!");
299 LOG_INFO(
"Clients are disconnected");
PlusStatus ConnectClients(int listeningPort, std::vector< vtkSmartPointer< vtkPlusOpenIGTLinkVideoSource > > &testClientList, int numberOfClientsToConnect, vtkSmartPointer< vtkXMLDataElement > configRootElement)
int main(int argc, char **argv)
virtual PlusStatus SetInputImageOrientation(US_IMAGE_ORIENTATION imageOrientation)
static vtkPlusConfig * GetInstance()
PlusStatus DisconnectClients(std::vector< vtkSmartPointer< vtkPlusOpenIGTLinkVideoSource > > &testClientList)
vtkSmartPointer< vtkPlusOpenIGTLinkServer > StartServer(const std::string &inputConfigFileName)
static vtkIGSIOLogger * Instance()
Contains an optional timestamped circular buffer containing the video images and a number of timestam...
std::string GetDeviceSetConfigurationPath(const std::string &subPath)
void SetDeviceSetConfigurationFileName(const std::string &aFilePath)
PlusStatus GetVideoSource(vtkPlusDataSource *&aVideoSource) const
void SetDeviceSetConfigurationData(vtkXMLDataElement *deviceSetConfigurationData)
static PlusStatus ReadDeviceSetConfigurationFromFile(vtkXMLDataElement *config, const char *filename)
vtkNew< vtkPlusOpenIGTLinkServer > server
Interface to a 3D positioning tool, video source, or generalized data stream.