forked from openigtlink/MatlabIGTL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathigtlopen.cpp
145 lines (110 loc) · 3.51 KB
/
igtlopen.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*=========================================================================
Program: Matlab Open IGT Link Interface -- igtlsend
Module: $RCSfile: $
Language: C++
Date: $Date: $
Version: $Revision: $
Copyright (c) Insight Software Consortium. All rights reserved.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
//
// SD = igtlopen(HOST, PORT);
//
// SD : (integer) Socket descriptor (-1 if failed to connect)
// HOST: (string) Hostname or IP address. If '' is specified,
// server socket will be created.
// PORT: (int) Port #
//
#include "mex.h"
#include <math.h>
#include <string.h>
#include "igtlOSUtil.h"
#include "igtlImageMessage.h"
#include "igtlMexClientSocket.h"
#include "igtlMexServerSocket.h"
using namespace std;
//#define pi (3.141592653589793)
#define ARG_ID_HOST 0 // host
#define ARG_ID_PORT 1 // port
#define ARG_ID_NUM 2 // total number of arguments
#define MAX_STRING_LEN 256
//extern void _main();
// -----------------------------------------------------------------
// Function declarations.
double& createMatlabScalar (mxArray*& ptr);
// -----------------------------------------------------------------
// Function definitions.
void mexFunction (int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[]) {
// ---------------------------------------------------------------
// Check numbers of arguments and outputs
if (nrhs != ARG_ID_NUM)
{
mexErrMsgTxt("Incorrect number of input arguments");
}
if (nlhs != 1)
{
mexErrMsgTxt("Incorrect number of output arguments");
}
// ---------------------------------------------------------------
// Check types of arguments
// HOST
if (!mxIsChar(prhs[ARG_ID_HOST]))
{
mexErrMsgTxt("HOST argument must be string.");
}
// PORT
if (!mxIsNumeric(prhs[ARG_ID_PORT]))
{
mexErrMsgTxt("PORT argument must be integer.");
}
// ---------------------------------------------------------------
// Set variables
char hostname[MAX_STRING_LEN+1];
mxGetString(prhs[ARG_ID_HOST], hostname, MAX_STRING_LEN);
int port = (int)*mxGetPr(prhs[ARG_ID_PORT]);
// TODO: check length of hostname
// ---------------------------------------------------------------
// Server or Client?
double& p = createMatlabScalar(plhs[0]);
if (strlen(hostname) == 0) // Server
{
// ----------------------------------------
// Set up OpenIGTLink server socket
igtl::MexServerSocket::Pointer serverSocket;
serverSocket = igtl::MexServerSocket::New();
int r = serverSocket->CreateServer(port);
if (r < 0)
{
p = -1;
mexErrMsgTxt("Cannot create a server socket.");
}
else
{
p = serverSocket->GetDescriptor();
}
}
else // Client
{
// ----------------------------------------
// Set up OpenIGTLink client socket
igtl::MexClientSocket::Pointer socket;
socket = igtl::MexClientSocket::New();
int result = socket->ConnectToServer(hostname, port);
//sleep(2);
if (result != 0)
{
p = -1;
}
else
{
p = socket->GetDescriptor();
}
}
}
double& createMatlabScalar (mxArray*& ptr) {
ptr = mxCreateDoubleMatrix(1,1,mxREAL);
return *mxGetPr(ptr);
}