RflySimSDK v3.05
RflySimSDK说明文档
载入中...
搜索中...
未找到
vrpn_FunctionGenerator.h
1#ifndef VRPN_FUNCTIONGENERATOR_H
2#define VRPN_FUNCTIONGENERATOR_H
3
4#include <stddef.h> // for NULL
5
6#include "vrpn_Analog.h" // for vrpn_CHANNEL_MAX
7#include "vrpn_BaseClass.h" // for vrpn_Callback_List, etc
8#include "vrpn_Configure.h" // for VRPN_CALLBACK, VRPN_API
9#include "vrpn_Connection.h"
10#include "vrpn_Shared.h" // for timeval
11#include "vrpn_Types.h" // for vrpn_int32, vrpn_uint32, etc
12
13
14const vrpn_uint32 vrpn_FUNCTION_CHANNELS_MAX = vrpn_CHANNEL_MAX;
15
16extern const char* vrpn_FUNCTION_MESSAGE_TYPE_CHANNEL;
17extern const char* vrpn_FUNCTION_MESSAGE_TYPE_CHANNEL_REQUEST;
18extern const char* vrpn_FUNCTION_MESSAGE_TYPE_ALL_CHANNEL_REQUEST;
19extern const char* vrpn_FUNCTION_MESSAGE_TYPE_SAMPLE_RATE;
20extern const char* vrpn_FUNCTION_MESSAGE_TYPE_START;
21extern const char* vrpn_FUNCTION_MESSAGE_TYPE_STOP;
22extern const char* vrpn_FUNCTION_MESSAGE_TYPE_CHANNEL_REPLY;
23extern const char* vrpn_FUNCTION_MESSAGE_TYPE_START_REPLY;
24extern const char* vrpn_FUNCTION_MESSAGE_TYPE_STOP_REPLY;
25extern const char* vrpn_FUNCTION_MESSAGE_TYPE_SAMPLE_RATE_REPLY;
26extern const char* vrpn_FUNCTION_MESSAGE_TYPE_INTERPRETER_REQUEST;
27extern const char* vrpn_FUNCTION_MESSAGE_TYPE_INTERPRETER_REPLY;
28extern const char* vrpn_FUNCTION_MESSAGE_TYPE_ERROR;
29
31
32// a base class for all functions that vrpn_FunctionGenerator
33// can generate
35{
36public:
38
39 // concrete classes should implement this to generate the appropriate
40 // values for the function the class represents. nValue samples should be
41 // generated beginning at time startTime, and these samples should be placed
42 // in the provided buffer. several data members of 'channel' can modify the
43 // times for which values are generated.
44 // returns the time of the last sample generated.
45 virtual vrpn_float32 generateValues( vrpn_float32* buf, vrpn_uint32 nValues,
46 vrpn_float32 startTime, vrpn_float32 sampleRate,
47 vrpn_FunctionGenerator_channel* channel ) const = 0;
48
49 // concrete classes should implement this to encode their
50 // function information into the specified buffer 'buf'. The
51 // remaining length in the buffer is stored in 'len'. At return,
52 // 'len' should be set to the number of characters remaining in the
53 // buffer and the number of characters written should be returned,
54 // save in case of failure, when negative should be returned.
55 virtual vrpn_int32 encode_to( char** buf, vrpn_int32& len ) const = 0;
56
57 // concrete classes should implement this to decode their
58 // function information from the specified buffer. The remaining
59 // length in the buffer is stored in 'len'. At return, 'len' should
60 // be set to the number of characters remaining in the the buffer
61 // and the number of characters read should be returned, save in case
62 // of failure, when negative should be returned
63 virtual vrpn_int32 decode_from( const char** buf, vrpn_int32& len ) = 0;
64
65 virtual vrpn_FunctionGenerator_function* clone( ) const = 0;
66
67 // used when encoding/decoding to specify function type
68 enum FunctionCode
69 {
70 FUNCTION_NULL = 0,
71 FUNCTION_SCRIPT = 1
72 };
73
74 // concrete classes should implement this to return the
75 // appropriate FunctionCode, from above
76 virtual FunctionCode getFunctionCode( ) const = 0;
77
78
79};
80
81
82// the NULL function: generate all zeros
85{
86public:
89
90 vrpn_float32 generateValues( vrpn_float32* buf, vrpn_uint32 nValues,
91 vrpn_float32 startTime, vrpn_float32 sampleRate,
92 vrpn_FunctionGenerator_channel* channel ) const;
93
94 vrpn_int32 encode_to( char** buf, vrpn_int32& len ) const;
95 vrpn_int32 decode_from( const char** buf, vrpn_int32& len );
96 vrpn_FunctionGenerator_function* clone( ) const;
97protected:
98 FunctionCode getFunctionCode( ) const { return FUNCTION_NULL; }
99
100};
101
102
104: public virtual vrpn_FunctionGenerator_function
105{
106public:
108 vrpn_FunctionGenerator_function_script( const char* script );
112
113 virtual vrpn_float32 generateValues( vrpn_float32* buf, vrpn_uint32 nValues,
114 vrpn_float32 startTime, vrpn_float32 sampleRate,
115 vrpn_FunctionGenerator_channel* channel ) const;
116
117 vrpn_int32 encode_to( char** buf, vrpn_int32& len ) const;
118 vrpn_int32 decode_from( const char** buf, vrpn_int32& len );
119 vrpn_FunctionGenerator_function* clone( ) const;
120
121 // returns a copy of the script. caller is responsible for
122 // calling 'delete []' to free the returned string.
123 char* getScript( ) const;
124
125 const char* getConstScript( ) const
126 { return script; }
127
128 vrpn_bool setScript( char* script );
129
130protected:
131 FunctionCode getFunctionCode( ) const { return FUNCTION_SCRIPT; }
132 char* script;
133};
134
135
137{
138 // note: the channel will delete its function when the function is
139 // no longer needed (e.g., when the channel is destroyed or the function changed)
140public:
144
145 const vrpn_FunctionGenerator_function* getFunction( ) const { return function; }
146 void setFunction( vrpn_FunctionGenerator_function* function );
147
148 // these return zero on success and negative on some failure.
149 vrpn_int32 encode_to( char** buf, vrpn_int32& len ) const;
150 vrpn_int32 decode_from( const char** buf, vrpn_int32& len );
151
152protected:
154
155};
156
157
159{
160public:
161 vrpn_FunctionGenerator( const char* name, vrpn_Connection* c = NULL );
162 virtual ~vrpn_FunctionGenerator( );
163
164 // returns the requested channel, or null if channelNum is
165 // greater than the maximum number of channels.
166 const vrpn_FunctionGenerator_channel* getChannel( vrpn_uint32 channelNum );
167
168 vrpn_uint32 getNumChannels( ) const { return numChannels; }
169
170 vrpn_float32 getSampleRate( )
171 { return sampleRate; }
172
173 enum FGError
174 {
175 NO_FG_ERROR = 0,
176 INTERPRETER_ERROR = 1, // the interpreter (for script) had some problem
177 TAKING_TOO_LONG = 2, // samples were not generated quickly enough
178 INVALID_RESULT_QUANTITY = 3, // an incorrect number of values was generated
179 INVALID_RESULT_RANGE = 4 // generated values were out of range
180 };
181
182protected:
183 vrpn_float32 sampleRate; // samples per second
184 vrpn_uint32 numChannels;
185 vrpn_FunctionGenerator_channel* channels[vrpn_FUNCTION_CHANNELS_MAX];
186
187 vrpn_int32 channelMessageID; // id for channel message (remote -> server)
188 vrpn_int32 requestChannelMessageID; // id for messages requesting channel info be sent (remote -> server)
189 vrpn_int32 requestAllChannelsMessageID; // id for messages requesting channel info of all channels be sent (remote -> server)
190 vrpn_int32 sampleRateMessageID; // id for message to request a sampling rate (remote -> server)
191 vrpn_int32 startFunctionMessageID; // id for message to start generating the function (remote -> server)
192 vrpn_int32 stopFunctionMessageID; // id for message to stop generating the function (remote -> server)
193 vrpn_int32 requestInterpreterMessageID; // id for message to request interpreter description (remote -> server)
194
195 vrpn_int32 channelReplyMessageID; // id for reply for channel message (server -> remote)
196 vrpn_int32 startFunctionReplyMessageID; // id for reply to start-function message (server -> remote)
197 vrpn_int32 stopFunctionReplyMessageID; // id for reply to stop-function message (server -> remote)
198 vrpn_int32 sampleRateReplyMessageID; // id for reply to request-sample-rate message (server -> remote)
199 vrpn_int32 interpreterReplyMessageID; // id for reply to request-interpreter message (server -> remote)
200 vrpn_int32 errorMessageID; // id for error reports
201
202 vrpn_int32 gotConnectionMessageID; // for new-connection message
203
204 virtual int register_types( );
205
206 char msgbuf[vrpn_CONNECTION_TCP_BUFLEN];
207 struct timeval timestamp;
208}; // end class vrpn_FunctionGenerator
209
210
212{
213public:
214 vrpn_FunctionGenerator_Server( const char* name, vrpn_uint32 numChannels = vrpn_FUNCTION_CHANNELS_MAX, vrpn_Connection* c = NULL );
216
217 virtual void mainloop( );
218
219 // sub-classes should implement these functions. they will be called when messages
220 // are received for the particular request. at the end of these functions, servers
221 // should call the appropriate send*Reply function, even (especially!) if the requested
222 // change was rejected.
223 virtual void setChannel( vrpn_uint32 channelNum, vrpn_FunctionGenerator_channel* channel ) = 0;
224 virtual void start( ) = 0;
225 virtual void stop( ) = 0;
226 virtual void setSampleRate( vrpn_float32 rate ) = 0;
227
228 vrpn_uint32 setNumChannels( vrpn_uint32 numChannels );
229
230 // sub-classes should implement this function to provide a description of the type
231 // of interpreter used to interpret vrpn_FunctionGenerator_function_script
232 virtual const char* getInterpreterDescription( ) = 0;
233
234 // sub-classes should not override these methods; these take care of
235 // receiving requests
236 static int VRPN_CALLBACK handle_channel_message( void* userdata, vrpn_HANDLERPARAM p );
237 static int VRPN_CALLBACK handle_channelRequest_message( void* userdata, vrpn_HANDLERPARAM p );
238 static int VRPN_CALLBACK handle_allChannelRequest_message( void* userdata, vrpn_HANDLERPARAM p );
239 static int VRPN_CALLBACK handle_start_message( void* userdata, vrpn_HANDLERPARAM p );
240 static int VRPN_CALLBACK handle_stop_message( void* userdata, vrpn_HANDLERPARAM p );
241 static int VRPN_CALLBACK handle_sample_rate_message( void* userdata, vrpn_HANDLERPARAM p );
242 static int VRPN_CALLBACK handle_interpreter_request_message( void* userdata, vrpn_HANDLERPARAM p );
243
244protected:
245
246 // sub-classes should call these functions to inform the remote side of
247 // changes (or of non-changes, when a requested change cannot be accepted).
248 // returns 0 on success and negative on failure.
249 int sendChannelReply( vrpn_uint32 channelNum );
250 int sendSampleRateReply( );
251 int sendStartReply( vrpn_bool started );
252 int sendStopReply( vrpn_bool stopped );
253 int sendInterpreterDescription( );
254
255 // sub-classes should use this function to report an error in function generation
256 int sendError( FGError error, vrpn_int32 channel );
257
258 vrpn_int32 decode_channel( const char* buf, const vrpn_int32 len, vrpn_uint32& channelNum,
260 vrpn_int32 decode_channel_request( const char* buf, const vrpn_int32 len, vrpn_uint32& channelNum );
261 vrpn_int32 decode_sampleRate_request( const char* buf, const vrpn_int32 len, vrpn_float32& sampleRate );
262
263 vrpn_int32 encode_channel_reply( char** buf, vrpn_int32& len, const vrpn_uint32 channelNum );
264 vrpn_int32 encode_start_reply( char** buf, vrpn_int32& len, const vrpn_bool isStarted );
265 vrpn_int32 encode_stop_reply( char** buf, vrpn_int32& len, const vrpn_bool isStopped );
266 vrpn_int32 encode_sampleRate_reply( char** buf, vrpn_int32& len, const vrpn_float32 sampleRate );
267 vrpn_int32 encode_interpreterDescription_reply( char** buf, vrpn_int32& len, const char* desc );
268 vrpn_int32 encode_error_report( char** buf, vrpn_int32& len, const FGError err, const vrpn_int32 channel );
269
270}; // end class vrpn_FunctionGenerator_Server
271
272
273//----------------------------------------------------------
274// ************** Users deal with the following *************
275
276// User routine to handle function-generator channel replies. This
277// is called when the function-generator server replies with new
278// setting for some channel.
280{
281 struct timeval msg_time; // Time of the report
282 vrpn_uint32 channelNum; // Which channel is being reported
285typedef void (VRPN_CALLBACK *vrpn_FUNCTION_CHANGE_REPLY_HANDLER)( void *userdata,
287
288// User routine to handle function-generator start replies. This
289// is called when the function-generator server reports that it
290// has started generating functions.
292{
293 struct timeval msg_time; // Time of the report
294 vrpn_bool isStarted; // did the function generation start?
296typedef void (VRPN_CALLBACK *vrpn_FUNCTION_START_REPLY_HANDLER)( void *userdata,
297 const vrpn_FUNCTION_START_REPLY_CB info );
298
299// User routine to handle function-generator stop replies. This
300// is called when the function-generator server reports that it
301// has stopped generating functions.
303{
304 struct timeval msg_time; // Time of the report
305 vrpn_bool isStopped; // did the function generation stop?
307typedef void (VRPN_CALLBACK *vrpn_FUNCTION_STOP_REPLY_HANDLER)( void *userdata,
308 const vrpn_FUNCTION_STOP_REPLY_CB info );
309
310// User routine to handle function-generator sample-rate replies.
311// This is called when the function-generator server reports that
312// the function-generation sample rate has changed.
314{
315 struct timeval msg_time; // Time of the report
316 vrpn_float32 sampleRate;
318typedef void (VRPN_CALLBACK *vrpn_FUNCTION_SAMPLE_RATE_REPLY_HANDLER)( void *userdata,
320
321
322// User routine to handle function-generator interpreter-description replies.
323// This is called when the function-generator server reports the description
324// of its interpreter.
326{
327 struct timeval msg_time; // Time of the report
328 char* description;
330typedef void (VRPN_CALLBACK *vrpn_FUNCTION_INTERPRETER_REPLY_HANDLER)( void *userdata,
332
333
334// User routine to handle function-generator error notifications.
335// This is called when the function-generator server reports some
336// error in the generation of a function.
338{
339 struct timeval msg_time; // Time of the report
340 vrpn_FunctionGenerator::FGError err;
341 vrpn_int32 channel;
343typedef void (VRPN_CALLBACK *vrpn_FUNCTION_ERROR_HANDLER)( void *userdata,
344 const vrpn_FUNCTION_ERROR_CB info );
345
346
348{
349public:
350 vrpn_FunctionGenerator_Remote( const char* name, vrpn_Connection* c = NULL );
351 virtual ~vrpn_FunctionGenerator_Remote( ) { }
352
353 int setChannel( const vrpn_uint32 channelNum, const vrpn_FunctionGenerator_channel* channel );
354 int requestChannel( const vrpn_uint32 channelNum );
355 int requestAllChannels( );
356 int requestStart( );
357 int requestStop( );
358 int requestSampleRate( const vrpn_float32 rate );
359 int requestInterpreterDescription( );
360
361 virtual void mainloop( );
362
363 // (un)Register a callback handler to handle a channel reply
364 virtual int register_channel_reply_handler( void *userdata,
365 vrpn_FUNCTION_CHANGE_REPLY_HANDLER handler );
366 virtual int unregister_channel_reply_handler( void *userdata,
367 vrpn_FUNCTION_CHANGE_REPLY_HANDLER handler );
368
369 // (un)Register a callback handler to handle a start reply
370 virtual int register_start_reply_handler( void *userdata,
371 vrpn_FUNCTION_START_REPLY_HANDLER handler );
372 virtual int unregister_start_reply_handler( void *userdata,
373 vrpn_FUNCTION_START_REPLY_HANDLER handler );
374
375 // (un)Register a callback handler to handle a stop reply
376 virtual int register_stop_reply_handler( void *userdata,
377 vrpn_FUNCTION_STOP_REPLY_HANDLER handler );
378 virtual int unregister_stop_reply_handler( void *userdata,
379 vrpn_FUNCTION_STOP_REPLY_HANDLER handler );
380
381 // (un)Register a callback handler to handle a sample-rate reply
382 virtual int register_sample_rate_reply_handler( void *userdata,
383 vrpn_FUNCTION_SAMPLE_RATE_REPLY_HANDLER handler );
384 virtual int unregister_sample_rate_reply_handler( void *userdata,
385 vrpn_FUNCTION_SAMPLE_RATE_REPLY_HANDLER handler );
386
387 // (un)Register a callback handler to handle an interpreter message
388 virtual int register_interpreter_reply_handler( void *userdata,
389 vrpn_FUNCTION_INTERPRETER_REPLY_HANDLER handler );
390 virtual int unregister_interpreter_reply_handler( void *userdata,
391 vrpn_FUNCTION_INTERPRETER_REPLY_HANDLER handler );
392
393 virtual int register_error_handler( void* userdata,
394 vrpn_FUNCTION_ERROR_HANDLER handler );
395 virtual int unregister_error_handler( void* userdata,
396 vrpn_FUNCTION_ERROR_HANDLER handler );
397
398 static int VRPN_CALLBACK handle_channelReply_message( void* userdata, vrpn_HANDLERPARAM p );
399 static int VRPN_CALLBACK handle_startReply_message( void* userdata, vrpn_HANDLERPARAM p );
400 static int VRPN_CALLBACK handle_stopReply_message( void* userdata, vrpn_HANDLERPARAM p );
401 static int VRPN_CALLBACK handle_sampleRateReply_message( void* userdata, vrpn_HANDLERPARAM p );
402 static int VRPN_CALLBACK handle_interpreterReply_message( void* userdata, vrpn_HANDLERPARAM p );
403 static int VRPN_CALLBACK handle_error_message( void* userdata, vrpn_HANDLERPARAM p );
404
405protected:
412
413
414 vrpn_int32 decode_channel_reply( const char* buf, const vrpn_int32 len, vrpn_uint32& channelNum );
415 vrpn_int32 decode_start_reply( const char* buf, const vrpn_int32 len, vrpn_bool& isStarted );
416 vrpn_int32 decode_stop_reply( const char* buf, const vrpn_int32 len, vrpn_bool& isStopped );
417 vrpn_int32 decode_sampleRate_reply( const char* buf, const vrpn_int32 len );
418 vrpn_int32 decode_interpreterDescription_reply( const char* buf, const vrpn_int32 len, char** desc );
419 vrpn_int32 decode_error_reply( const char* buf, const vrpn_int32 len, FGError& error, vrpn_int32& channel );
420
421 vrpn_int32 encode_channel( char** buf, vrpn_int32& len, const vrpn_uint32 channelNum,
422 const vrpn_FunctionGenerator_channel* channel );
423 vrpn_int32 encode_channel_request( char** buf, vrpn_int32& len, const vrpn_uint32 channelNum );
424 vrpn_int32 encode_sampleRate_request( char** buf, vrpn_int32& len, const vrpn_float32 sampleRate );
425
426}; // end class vrpn_FunctionGenerator_Remote
427
428
429#endif // VRPN_FUNCTIONGENERATOR_H
定义 vrpn_BaseClass.h:310
定义 vrpn_BaseClass.h:361
Generic connection class not specific to the transport mechanism.
定义 vrpn_Connection.h:562
定义 vrpn_FunctionGenerator.h:348
定义 vrpn_FunctionGenerator.h:212
定义 vrpn_FunctionGenerator.h:137
定义 vrpn_FunctionGenerator.h:85
定义 vrpn_FunctionGenerator.h:105
定义 vrpn_FunctionGenerator.h:35
定义 vrpn_FunctionGenerator.h:159
virtual int register_types()
定义 vrpn_FunctionGenerator.h:280
定义 vrpn_FunctionGenerator.h:338
定义 vrpn_FunctionGenerator.h:326
定义 vrpn_FunctionGenerator.h:314
定义 vrpn_FunctionGenerator.h:292
定义 vrpn_FunctionGenerator.h:303
This structure is what is passed to a vrpn_Connection message callback.
定义 vrpn_Connection.h:41