1 /***********************************************************************/
2 /* Open Visualization Data Explorer                                    */
3 /* (C) Copyright IBM Corp. 1989,1999                                   */
4 /* ALL RIGHTS RESERVED                                                 */
5 /* This code licensed under the                                        */
6 /*    "IBM PUBLIC LICENSE - Open Visualization Data Explorer"          */
7 /***********************************************************************/
8 
9 #include <dxconfig.h>
10 
11 
12 #ifndef _DXLP_H
13 #define _DXLP_H
14 
15 #include "dxl.h"
16 #include "dict.h"
17 
18 #include <sys/types.h>
19 
20 #if defined(HAVE_WINSOCK_H)
21 #include <winsock.h>
22 #else
23 # if defined(HAVE_NETINET_IN_H)
24 # include <netinet/in.h>
25 # endif
26 #endif
27 
28 #if defined(HAVE_SYS_UN_H)
29 #include <sys/un.h>
30 #endif
31 
32 #ifdef OS2
33 #define INCL_DOS
34 #include <os2.h>
35 #endif
36 
37 #if defined(__cplusplus) || defined(c_plusplus)
38 extern "C" {
39 #endif
40 
41 /*
42  * Input handling structure.
43  */
44 #define DXL_EVENT_BUF_SIZE 128
45 struct DXLEvent {
46     int  type;
47     int  serial;
48     char *contents;
49     int  contentsSize;
50     char buffer[DXL_EVENT_BUF_SIZE];
51     struct DXLEvent *next;
52 };
53 typedef struct DXLEvent DXLEvent;
54 
55 
56 struct handlerEntry
57 {
58     int                 packetId;
59     char                *operand;
60     int                 operandLength;
61     DXLMessageHandler   handler;
62     const void          *data;          /* Call data passed to handler */
63     struct handlerEntry *next;
64 };
65 typedef struct handlerEntry *HandlerEntry;
66 
67 DXLError _dxl_InitMessageHandlers(DXLConnection *);
68 DXLError _dxl_FreeMessageHandlers(DXLConnection *);
69 DXLError _dxl_HandleMessage(DXLConnection *, int, int, const char *);
70 DXLError _dxl_SetSystemHandlerId(DXLConnection *conn,
71                 int type, int packetId,
72                 DXLMessageHandler h, const void *data);
73 DXLError _dxl_SetSystemHandlerString(DXLConnection *conn,
74                 int type, const char *str,
75                 DXLMessageHandler h, const void *data);
76 DXLError _dxl_SetSystemHandler(DXLConnection *conn,
77                 int type, int packetId, const char *str,
78                 DXLMessageHandler h, const void *data);
79 DXLError _dxl_RemoveSystemHandler(DXLConnection *conn,
80 		int type, int packetId, const char *str,
81 		DXLMessageHandler h);
82 
83 
84 
85 /*
86  * Actually, there are only 21 in dx2.1.1 (no $lnk), but having to many
87  * entries should not hurt.
88  */
89 #define PACKET_TYPE_COUNT 22
90 
91 /*
92  * Connection specific structure.
93  */
94 struct DXLConnection {
95     int fd;
96     int id;
97     int nEvents;
98 #if 00
99     int allocdEvents;
100 #else
101     DXLEvent *lastEvent;
102 #endif
103     DXLEvent *events;
104     int nLeftOver;
105     int allocdLeftOver;
106     char *leftOver;
107     int synchronous;
108     int syncing;                /* DXLSync is in the call stack if true */
109     int dxuiConnected;          /* Are we connected to the ui or exec */
110     int isExecuting;            /* Only valid when !dxuiConnected */
111     int majorVersion;           /* Major version # of remote app */
112     int minorVersion;           /* Minor version # of remote app */
113     int microVersion;           /* Micro version # of remote app */
114     int debugMessaging;         /* Print info about packets sent/received */
115     /*
116      * remember that packet types are one-origin
117      */
118     HandlerEntry systemMessageHandlers[PACKET_TYPE_COUNT+1];
119     HandlerEntry userMessageHandlers[PACKET_TYPE_COUNT+1];
120     _Dictionary  *objectHandlerDict;
121     _Dictionary  *valueHandlerDict;
122     /* Flag to check macro definition status
123      * Aids error checking and reduces user complexity by allowing
124      * DXLSend to be used for macro body
125      */
126     int macroDef;
127 #ifdef OS2
128     HWND PMAppWindow;
129     HEV  PMHandlerSemaphore;
130     TID  tidCheckInputThread;
131 #endif
132 #if defined(intelnt) || defined(WIN32)
133     HWND PMAppWindow;
134     HANDLE  PMHandlerSemaphore;
135     HANDLE  tidCheckInputThread;
136 #endif
137     void (*brokenConnectionCallback)(DXLConnection *, void *);
138     void *brokenConnectionCallbackData;
139     void (*installIdleEventHandler)(DXLConnection *);
140     void (*clearIdleEventHandler)(DXLConnection *);
141     void (*removeEventHandler)(DXLConnection *);
142     void  *eventHandlerData;
143 
144     /*
145      * The file descriptors are pipes opened in conn.c.   They're
146      * stored here so that they can be closed when the connection
147      * goes away.
148      */
149     int   in, out, err;
150 };
151 
152 #define _dxf_InstallIdleEventHandler(c) \
153 	if ((c) && ((c)->installIdleEventHandler)) \
154 	    ((c)->installIdleEventHandler)(c);
155 
156 #define _dxf_ClearIdleEventHandler(c) \
157 	if ((c) && ((c)->clearIdleEventHandler)) \
158 	    ((c)->clearIdleEventHandler)(c);
159 
160 #define _dxf_RemoveEventHandler(c) \
161 	if ((c) && ((c)->removeEventHandler)) \
162 	    ((c)->removeEventHandler)(c);
163 
164 #define SELECT_INTR(n, readlist, writelist, execptlist, timeout,rc) \
165         while((rc = select(n, readlist, writelist, execptlist, timeout)) \
166           < 0 && errno == EINTR)
167 
168 
169 extern char *_DXLPacketTypes[];
170 extern int _DXLNumPackets;
171 
172 /*
173  * Internal structure used for the connection functions.
174  */
175 struct DXLConnectionPrototype {
176     int sock;
177     struct sockaddr_in server;
178     int usock;
179 #if defined(HAVE_SYS_UN_H)
180     struct sockaddr_un userver;
181 #endif
182 };
183 typedef struct DXLConnectionPrototype DXLConnectionPrototype;
184 
185 /*
186  * Internal routines, provided for debugging.  These are used by DXLStartDX,
187  * and are not normally accessed by a user.  If an application  were to use
188  * these routines, it would call DXLInitPrototype, start dx however it
189  * wanted to (including by hand in the debugger) and have it connect to the
190  * returnedPort.  After dxui is started, the application would call
191  * DXLFinishConnection, and "free()" the DXLConnectionPrototype.
192  *
193  * returnedPort is the port number actually created, and can be passed to
194  * DXLStartChild .  forceTimeout is a boolean that makes DXLFinishConnection
195  * fail if the DX application didn't connect within some time.
196  *
197  * DXLInitPrototype and DXLFinishConnection return NULL and set the error code
198  * on error.
199  */
200 
201 /*
202  * These routines control the inital port tried in creating the connection
203  * (defaults to 1920) and the currently used timeout in seconds.
204  */
205 #define DXL_ACCEPT_TIMEOUT      60      /* Seconds */
206 #define DXL_BASE_PORT           1920
207 void            DXLSetBasePort(const int port);
208 int             DXLGetBasePort();
209 void _dxl_InvalidateSocket(DXLConnection *connection);
210 #if 0
211 void            DXLSetHost(const char *host);
212 char *          DXLGetHost();
213 #endif
214 int             DXLIsHostLocal(const char *host);
215 
216 /*
217  * Default error handler and a routine to facilitate calling the
218  * current handler.
219  */
220 void _DXLDefaultHandler(DXLConnection *conn, const char *message, void *data);
221 DXLError _DXLError(DXLConnection *conn, const char *message);
222 
223 DXLError DXLNextPacket(DXLConnection *conn, DXLEvent *event);
224 /*
225  * Find and read until a packet with the given type and  id is found.
226  * Return the found event in the given event, and remove the event
227  * from the event list.
228  */
229 DXLError DXLGetPacketId(DXLConnection *conn, DXLPacketTypeEnum type, int id,
230                                                 DXLEvent *event);
231 DXLError DXLGetPacketString(DXLConnection *conn, DXLPacketTypeEnum type,
232                                         const char *matchstr, DXLEvent *event);
233 DXLError DXLGetPacketIdString(DXLConnection *conn, DXLPacketTypeEnum type,
234                         int requestId, const char *str, DXLEvent *event);
235 int      DXLCheckPacketString(DXLConnection *conn, DXLPacketTypeEnum type,
236                                         const char *matchstr, DXLEvent *event);
237 DXLError DXLWaitPacketString(DXLConnection *conn, DXLPacketTypeEnum type,
238                                         const char *matchstr, DXLEvent *event);
239 DXLError DXLPeekPacket(DXLConnection *conn, DXLEvent *event);
240 DXLError DXLReadFromSocket(DXLConnection *conn);
241 int      DXLGetSocket(DXLConnection *conn);
242 DXLEvent *DXLNewEvent(int serial, DXLPacketTypeEnum ptype, const char *msg);
243 DXLError DXLFreeEvent(DXLEvent *event);
244 DXLError DXLClearEvent(DXLEvent *event);
245 DXLError DXLCopyEvent(DXLEvent *dst, DXLEvent *src);
246 DXLError DXLProcessEventList(DXLConnection *conn);
247 
248 #ifdef NON_ANSI_SPRINTF
249 #define SPRINTF _DXLsprintf
250 #else
251 #define SPRINTF sprintf
252 #endif
253 
254 DXLError DXLSendUnpacketized(DXLConnection *conn, const char *string);
255 DXLError DXLSendImmediate(DXLConnection *conn, const char *string);
256 
257 DXLError DXLSendPacket(DXLConnection *conn, DXLPacketTypeEnum ptype,
258                                         const char *string);
259 
260 
261 /*
262  * Value and Object handler functions that are used to set up functions
263  * that are called when values and objects are sent from the DX server.
264  */
265 #ifndef _LIBDX_H
266 typedef struct object *Object;
267 #endif  /* dx.h */
268 typedef void (*DXLObjectHandler)(DXLConnection *conn, const char *name,
269                                                 Object o, void *data);
270 DXLError DXLSetObjectHandler(DXLConnection *c, const char *name,
271                                         DXLObjectHandler h, const void *data);
272 DXLError DXLRemoveObjectHandler(DXLConnection *c, const char *name);
273 
274 
275 /*
276  * Setting and getting of input values specified by macro, module,
277  * module instance number and parameter index.
278  */
279 DXLError                DXLSetInputValue(DXLConnection *conn,
280                         const char *macro,
281                         const char *module,
282                         const int instance,
283                         const int number,
284                         const char *value);
285 DXLError                DXLSetInputInteger(DXLConnection *conn,
286                         const char *macro,
287                         const char *module,
288                         const int instance,
289                         const int number,
290                         const int value);
291 DXLError                DXLSetInputScalar(DXLConnection *conn,
292                         const char *macro,
293                         const char *module,
294                         const int instance,
295                         const int number,
296                         const double value);
297 DXLError                DXLSetInputString(DXLConnection *conn,
298                         const char *macro,
299                         const char *module,
300                         const int instance,
301                         const int number,
302                         const char *value);
303 
304 DXLError                DXLGetInputValue(DXLConnection *conn,
305                         const char *macro,
306                         const char *module,
307                         const int instance,
308                         const int number,
309                         const int valueLen,
310                         char *value);
311 DXLError                DXLGetInputInteger(DXLConnection *conn,
312                         const char *macro,
313                         const char *module,
314                         const int instance,
315                         const int number,
316                         int *value);
317 DXLError                DXLGetInputScalar(DXLConnection *conn,
318                         const char *macro,
319                         const char *module,
320                         const int instance,
321                         const int number,
322                         double *value);
323 DXLError                DXLGetInputString(DXLConnection *conn,
324                         const char *macro,
325                         const char *module,
326                         const int instance,
327                         const int number,
328                         const int valueLen,
329                         char *value);
330 
331 /*
332  * Setting and getting of output values specified by macro, module,
333  * module instance number and parameter index.
334  * Outputs are only settable and queryable for interactors.
335  */
336 DXLError                DXLSetOutputValue(DXLConnection *conn,
337                         const char *macro,
338                         const char *module,
339                         const int instance,
340                         const int number,
341                         const char *value);
342 DXLError                DXLSetOutputInteger(DXLConnection *conn,
343                         const char *macro,
344                         const char *module,
345                         const int instance,
346                         const int number,
347                         const int value);
348 DXLError                DXLSetOutputScalar(DXLConnection *conn,
349                         const char *macro,
350                         const char *module,
351                         const int instance,
352                         const int number,
353                         const double value);
354 DXLError                DXLSetOutputString(DXLConnection *conn,
355                         const char *macro,
356                         const char *module,
357                         const int instance,
358                         const int number,
359                         const char *value);
360 
361 DXLError                DXLGetOutputValue(DXLConnection *conn,
362                         const char *macro,
363                         const char *module,
364                         const int instance,
365                         const int number,
366                         const int valueLen,
367                         char *value);
368 DXLError                DXLGetOutputInteger(DXLConnection *conn,
369                         const char *macro,
370                         const char *module,
371                         const int instance,
372                         const int number,
373                         int *value);
374 DXLError                DXLGetOutputScalar(DXLConnection *conn,
375                         const char *macro,
376                         const char *module,
377                         const int instance,
378                         const int number,
379                         double *value);
380 DXLError                DXLGetOutputString(DXLConnection *conn,
381                         const char *macro,
382                         const char *module,
383                         const int instance,
384                         const int number,
385                         const int valueLen,
386                         char *value);
387 
388 #if defined(__cplusplus) || defined(c_plusplus)
389 }
390 #endif
391 #endif
392