forked from openigtlink/MatlabIGTL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
igtlwaitcon.cpp
149 lines (113 loc) · 3.66 KB
/
igtlwaitcon.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
143
144
145
146
147
148
149
/*=========================================================================
Program: Matlab Open IGT Link Interface -- igtlwaitcon
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.
=========================================================================*/
//
// cfd = igtlwaitcon(SD, DATA);
//
// SD : (integer) Server Socket descriptor (-1 if failed to connect)
// TIMEOUT : (integer) Time out in millisecond
// cfd : (integer) Client file descriptor
//
#include "mex.h"
#include <math.h>
#include <string.h>
#include "igtlOSUtil.h"
#include "igtlMexServerSocket.h"
#include "igtlMexClientSocket.h"
using namespace std;
//#define pi (3.141592653589793)
#define ARG_ID_SD 0
#define ARG_ID_TIMEOUT 1 // time out
#define ARG_ID_NUM 2 // total number of arguments
#define MAX_STRING_LEN 256
//extern void _main();
// -----------------------------------------------------------------
// Function declarations.
double& createMatlabScalar (mxArray*& ptr);
int checkArguments(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[]);
int checkData(const char* type, const mxArray* prhs);
// -----------------------------------------------------------------
// Function definitions.
void mexFunction (int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
// ---------------------------------------------------------------
// Get reference to return value
double& retVal = createMatlabScalar(plhs[0]);
// ---------------------------------------------------------------
// Check arguments
if (checkArguments(nlhs, plhs, nrhs, prhs) == 0)
{
retVal = 0;
return;
}
// ---------------------------------------------------------------
// Set header variables
int sd;
int timeOut;
/* currently not used.
char type[MAX_STRING_LEN];
char name[MAX_STRING_LEN]; */
sd = (int)*mxGetPr(prhs[ARG_ID_SD]);
timeOut = (int)*mxGetPr(prhs[ARG_ID_TIMEOUT]);
igtl::MexServerSocket::Pointer serverSocket;
serverSocket = igtl::MexServerSocket::New();
serverSocket->SetDescriptor(sd);
igtl::MexSocket::Pointer socket;
socket = serverSocket->WaitForConnection(timeOut);
// ---------------------------------------------------------------
// Return result to Matlab
if (socket.IsNotNull()) // if client connected
{
// Create a message buffer to receive header
retVal = socket->GetDescriptor();
}
else
{
retVal = -1;
}
}
int checkArguments(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");
return 0;
}
if (nlhs != 1)
{
mexErrMsgTxt("Incorrect number of output arguments");
return 0;
}
// ---------------------------------------------------------------
// Check types of arguments
// SD -- socket descriptor
if (!mxIsNumeric(prhs[ARG_ID_SD]))
{
mexErrMsgTxt("SD argument must be integer.");
return 0;
}
// TIMEOUT -- time out
if (!mxIsNumeric(prhs[ARG_ID_TIMEOUT]))
{
mexErrMsgTxt("DATA argument must be integer.");
return 0;
}
return 1;
}
double& createMatlabScalar (mxArray*& ptr) {
ptr = mxCreateDoubleMatrix(1,1,mxREAL);
return *mxGetPr(ptr);
}