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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
|
/*
* Ivy, C interface
*
* Copyright (C) 1997-2000
* Centre d'Études de la Navigation Aérienne
*
* Main loop based on Tcl
*
* Authors: François-Régis Colin <fcolin@cena.dgac.fr>
*
* $Id$
*
* Please refer to file version.h for the
* copyright notice regarding this software
*/
#include <sys/time.h>
#include <unistd.h>
#ifdef __APPLE__
#include <malloc/malloc.h>
#else
#include <malloc.h>
#endif
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <tcl.h>
#include "ivytcl.h"
#include "ivysocket.h"
#include "ivychannel.h"
#include "ivy.h"
#include "timer.h"
/* defined or not in the Makefile only */
#ifdef TCL_CHANNEL_INTEGRATION
/* On utilisa la boucle Standard TCL */
/* mais il y a des problemes sur les socket server */
/* Il n'y a pas de Tcl_MakeTCPserver */
struct _channel {
HANDLE fd;
void *data;
Tcl_Channel tcl_channel;
ChannelHandleDelete handle_delete;
ChannelHandleRead handle_read;
ChannelHandleWrite handle_write;
};
static int channel_initialized = 0;
#ifdef WIN32
WSADATA WsaData;
#endif
void IvyChannelInit(void)
{
#ifdef WIN32
int error;
#endif
if ( channel_initialized ) return;
/* pour eviter les plantages quand les autres applis font core-dump */
#ifndef WIN32
signal( SIGPIPE, SIG_IGN);
#endif
#ifdef WIN32
error = WSAStartup (0x0101, &WsaData);
if (error == SOCKET_ERROR) {
printf ("WSAStartup failed.\n");
}
#endif
channel_initialized = 1;
}
static void
IvyHandleFd(ClientData cd,
int mask)
{
Channel channel = (Channel)cd;
/*printf("handle event %d\n", mask);*/
if (mask == TCL_READABLE) {
(*channel->handle_read)(channel,channel->fd,channel->data);
} else if (mask == TCL_WRITABLE) {
(*channel->handle_write)(channel,channel->fd,channel->data);
} else if (mask == TCL_EXCEPTION) {
(*channel->handle_delete)(channel->data);
}
}
void IvyChannelRemove( Channel channel )
{
if ( channel->handle_delete )
(*channel->handle_delete)( channel->data );
Tcl_DeleteChannelHandler(channel->tcl_channel, IvyHandleFd, (ClientData) channel);
ckfree((char *) channel);
}
Channel IvyChannelAdd(HANDLE fd, void *data,
ChannelHandleDelete handle_delete,
ChannelHandleRead handle_read,
ChannelHandleWrite handle_write
)
{
Channel channel;
channel = (Channel)ckalloc( sizeof (struct _channel) );
if ( !channel )
{
fprintf(stderr,"NOK Memory Alloc Error\n");
exit(0);
}
channel->handle_delete = handle_delete;
channel->handle_read = handle_read;
channel->handle_write = handle_write;
channel->data = data;
channel->fd = fd;
/*printf("Create handle fd %d\n", fd);*/
channel->tcl_channel = Tcl_MakeTcpClientChannel((void*) (long) fd);
Tcl_CreateChannelHandler(channel->tcl_channel, TCL_READABLE|TCL_EXCEPTION,
IvyHandleFd, (ClientData) channel);
return channel;
}
void IvyChannelAddWritableEvent(Channel channel)
{
// On change juste le masque
Tcl_CreateChannelHandler(channel->tcl_channel, TCL_READABLE|TCL_WRITABLE|TCL_EXCEPTION,
IvyHandleFd, (ClientData) channel);
}
void IvyChannelClearWritableEvent(Channel channel)
{
// On change juste le masque
Tcl_CreateChannelHandler(channel->tcl_channel, TCL_READABLE|TCL_EXCEPTION,
IvyHandleFd, (ClientData) channel);
}
#else
/* On utilisa la procedure Idle et la boucle Standard Ivy */
void IvyIdleProc(ClientData clientData)
{
IvyIdle();
Tcl_DoWhenIdle(IvyIdleProc,0);
}
#endif
void
IvyChannelStop ()
{
/* To be implemented */
#ifndef TCL_CHANNEL_INTEGRATION
Tcl_CancelIdleCall(IvyIdleProc,0);
#endif
}
/* Code from PLC */
#define INTEGER_SPACE 30
typedef struct _filter_struct {
MsgRcvPtr id;
char *script;
char *filter;
Tcl_Interp *interp;
} filter_struct;
static Tcl_HashTable filter_table;
static Tcl_HashTable app_table;
static int filter_id = 0;
static char *nom_de_l_appli = NULL;
static char *message_de_bienvenue;
static void
IvyAppCB(IvyClientPtr app,
void *user_data, /* script a appeler */
IvyApplicationEvent event)
{
static char *app_event_str[] = {
"Connected", "Disconnected" };
filter_struct *filter = (filter_struct *) user_data;
int result, size, dummy;
char *script_to_call;
Tcl_HashEntry *entry;
entry = Tcl_FindHashEntry(&app_table, IvyGetApplicationName(app));
if (event == IvyApplicationConnected) {
if (!entry) {
entry = Tcl_CreateHashEntry(&app_table, IvyGetApplicationName(app), &dummy);
Tcl_SetHashValue(entry, (ClientData) app);
}
}
size = strlen(filter->script) + INTEGER_SPACE;
if (entry) {
size += strlen(IvyGetApplicationName(app)) + 3;
}
else {
size += 4;
}
script_to_call = ckalloc(size);
strcpy(script_to_call, filter->script);
strcat(script_to_call, " ");
if (entry) {
strcat(script_to_call, " \"");
strcat(script_to_call, IvyGetApplicationName(app));
strcat(script_to_call, "\"");
}
else {
strcat(script_to_call, "???");
}
strcat(script_to_call, " \"");
strcat(script_to_call, app_event_str[event%2]);
strcat(script_to_call, "\"");
Tcl_Preserve(filter->interp);
result = Tcl_GlobalEval(filter->interp, script_to_call);
ckfree(script_to_call);
if (result != TCL_OK) {
Tcl_BackgroundError(filter->interp);
}
Tcl_Release(filter->interp);
if (event == IvyApplicationDisconnected) {
if (entry) {
Tcl_DeleteHashEntry(entry);
}
}
}
static void
IvyDieCB(IvyClientPtr app,
void *user_data, /* script a appeler */
int id)
{
filter_struct *filter = (filter_struct *) user_data;
int result, size;
char idstr[INTEGER_SPACE];
char *script_to_call;
sprintf(idstr, "%d", id);
size = strlen(filter->script) + INTEGER_SPACE + 1;
script_to_call = ckalloc(size);
strcpy(script_to_call, filter->script);
strcat(script_to_call, " \"");
strcat(script_to_call, idstr);
strcat(script_to_call, "\"");
Tcl_Preserve(filter->interp);
result = Tcl_GlobalEval(filter->interp, script_to_call);
ckfree(script_to_call);
if (result != TCL_OK) {
Tcl_BackgroundError(filter->interp);
}
Tcl_Release(filter->interp);
}
static void
IvyMsgCB(IvyClientPtr app,
void *user_data,
int argc,
char **argv)
{
filter_struct *filter = (filter_struct *) user_data;
int result, i, size;
char *script_to_call;
size = strlen(filter->script) + 3;
for (i = 0; i < argc; i++) {
size += strlen(argv[i]) + 3;
}
size ++;
size += strlen(IvyGetApplicationName(app))+4;
script_to_call = ckalloc(size);
strcpy(script_to_call, filter->script);
strcat(script_to_call, " \"");
strcat(script_to_call, IvyGetApplicationName(app));
strcat(script_to_call, "\"");
/* strcat(script_to_call, " {"); */
for (i = 0; i < argc; i++) {
strcat(script_to_call, " \"");
strcat(script_to_call, argv[i]);
strcat(script_to_call, "\"");
}
/* strcat(script_to_call, " }"); */
Tcl_Preserve(filter->interp);
result = Tcl_GlobalEval(filter->interp, script_to_call);
ckfree(script_to_call);
if (result != TCL_OK) {
Tcl_BackgroundError(filter->interp);
}
Tcl_Release(filter->interp);
}
static void
IvyDirectMsgCB(IvyClientPtr app,
void *user_data,
int id,
char *msg)
{
filter_struct *filter = (filter_struct *) user_data;
int result, size;
char *script_to_call;
char int_buffer[INTEGER_SPACE];
sprintf(int_buffer, "%d", id);
size = strlen(filter->script) + 1;
size += strlen(int_buffer) + 1;
size += strlen(msg) + 1;
script_to_call = ckalloc(size);
strcpy(script_to_call, filter->script);
strcat(script_to_call, " ");
strcat(script_to_call, int_buffer);
strcat(script_to_call, " \"");
strcat(script_to_call, msg);
strcat(script_to_call, "\"");
Tcl_Preserve(filter->interp);
result = Tcl_GlobalEval(filter->interp, script_to_call);
ckfree(script_to_call);
if (result != TCL_OK) {
Tcl_BackgroundError(filter->interp);
}
Tcl_Release(filter->interp);
}
/* Real TCL interface */
static int
IvyInitCmd(ClientData clientData,
Tcl_Interp *interp,
int argc,
const char **argv)
{
filter_struct *app;
filter_struct *die;
if (nom_de_l_appli) {
Tcl_AppendResult(interp, "Ivy has been already inited", (char *) NULL);
return TCL_ERROR;
}
if (argc != 5) {
Tcl_AppendResult(interp, "wrong # of args: \"",
argv[0], " appName readyMsg connectScript dieScript\"",
(char *) NULL);
return TCL_ERROR;
}
nom_de_l_appli = ckalloc(strlen(argv[1])+1);
strcpy(nom_de_l_appli, argv[1]);
message_de_bienvenue = ckalloc(strlen(argv[2])+1);
strcpy(message_de_bienvenue, argv[2]);
app = (filter_struct *) ckalloc(sizeof(filter_struct));
die = (filter_struct *) ckalloc(sizeof(filter_struct));
app->interp = interp;
die->interp = interp;
app->script = ckalloc(strlen(argv[3])+1);
strcpy(app->script, argv[3]);
die->script = ckalloc(strlen(argv[4])+1);
strcpy(die->script, argv[4]);
Tcl_InitHashTable(&filter_table, TCL_ONE_WORD_KEYS);
Tcl_InitHashTable(&app_table, TCL_STRING_KEYS);
/*printf("appel de businit avec %s %d %s\n", argv[1], port, argv[3]);*/
IvyInit(nom_de_l_appli, message_de_bienvenue,
IvyAppCB, (void *) app, IvyDieCB, (void *) die);
return TCL_OK;
}
static int
IvyStartCmd(ClientData clientData,
Tcl_Interp *interp,
int argc,
const char **argv)
{
if (argc != 2) {
Tcl_AppendResult(interp, "wrong # of args: \"",
argv[0], " msg\"", (char *) NULL);
return TCL_ERROR;
}
IvyStart(argv[1]);
return TCL_OK;
}
static int
IvyBindCmd(ClientData clientData,
Tcl_Interp *interp,
int argc,
const char **argv)
{
filter_struct *filter;
Tcl_HashEntry *entry;
int dummy;
char msg[INTEGER_SPACE];
if (argc != 3) {
Tcl_AppendResult(interp, "wrong # of args: \"",
argv[0], " filter script\"", (char *) NULL);
return TCL_ERROR;
}
filter = (filter_struct *) ckalloc(sizeof(filter_struct));
filter->filter = ckalloc(strlen(argv[1])+1); /* regexp */
strcpy(filter->filter, argv[1]);
filter->script = ckalloc(strlen(argv[2])+1); /* callback proc name */
strcpy(filter->script, argv[2]);
filter->id = IvyBindMsg(IvyMsgCB, (void *) filter, filter->filter, NULL); /* MsgPtr id */
filter->interp = interp;
entry = Tcl_CreateHashEntry(&filter_table, (char *) (long) filter_id, &dummy);
Tcl_SetHashValue(entry, (ClientData) filter);
sprintf(msg, "%d", filter_id);
filter_id++;
Tcl_SetResult(interp, msg, TCL_VOLATILE);
return TCL_OK;
}
static int
IvyUnbindCmd(ClientData clientData,
Tcl_Interp *interp,
int argc,
const char **argv)
{
unsigned long filterId;
char *end;
filter_struct *filter;
Tcl_HashEntry *entry;
if (argc != 2) {
Tcl_AppendResult(interp, "wrong # of args: \"",
argv[0], " filterId\"", (char *) NULL);
return TCL_ERROR;
}
filterId = strtol(argv[1], &end, 10);
if (*end) {
Tcl_AppendResult(interp, argv[0], " wrong filter id: \"", argv[1],
(char *) NULL);
return TCL_ERROR;
}
entry = Tcl_FindHashEntry(&filter_table, (char *) filterId);
if (!entry) {
Tcl_AppendResult(interp, argv[0], " unknown filter: \"", argv[1],
(char *) NULL);
return TCL_ERROR;
}
filter = (filter_struct *) Tcl_GetHashValue(entry);
Tcl_DeleteHashEntry(entry);
IvyUnbindMsg(filter->id);
ckfree(filter->script);
ckfree(filter->filter);
ckfree((char *) filter);
return TCL_OK;
}
static int
IvySendCmd(ClientData clientData,
Tcl_Interp *interp,
int argc,
const char **argv)
{
if (argc != 2) {
Tcl_AppendResult(interp, "wrong # of args: \"",
argv[0], " msg\"", (char *) NULL);
return TCL_ERROR;
}
IvySendMsg(argv[1], NULL);
return TCL_OK;
}
static int
IvyApplicationListCmd(ClientData clientData,
Tcl_Interp *interp,
int argc,
const char **argv)
{
Tcl_HashEntry *entry;
Tcl_HashSearch search;
if (argc != 1) {
Tcl_AppendResult(interp, "wrong # of args: \"",
argv[0], "\"", (char *) NULL);
return TCL_ERROR;
}
entry = Tcl_FirstHashEntry(&app_table, &search);
while (entry) {
Tcl_AppendElement(interp, Tcl_GetHashKey(&app_table, entry));
entry = Tcl_NextHashEntry(&search);
}
return TCL_OK;
}
static int
IvyApplicationHostCmd(ClientData clientData,
Tcl_Interp *interp,
int argc,
const char **argv)
{
Tcl_HashEntry *entry;
if (argc != 2) {
Tcl_AppendResult(interp, "wrong # of args: \"",
argv[0], " appName\"", (char *) NULL);
return TCL_ERROR;
}
entry = Tcl_FindHashEntry(&app_table, argv[1]);
if (!entry) {
Tcl_AppendResult(interp, "Application \"",
argv[1], "\" not connected", (char *) NULL);
return TCL_ERROR;
}
Tcl_SetResult(interp, IvyGetApplicationHost((IvyClientPtr) Tcl_GetHashValue(entry)),
TCL_STATIC);
return TCL_OK;
}
static int
IvyApplicationMsgsCmd(ClientData clientData,
Tcl_Interp *interp,
int argc,
const char **argv)
{
Tcl_HashEntry *entry;
char **msgs, **scan;
if (argc != 2) {
Tcl_AppendResult(interp, "wrong # of args: \"",
argv[0], " appName\"", (char *) NULL);
return TCL_ERROR;
}
entry = Tcl_FindHashEntry(&app_table, argv[1]);
if (!entry) {
Tcl_AppendResult(interp, "Application \"",
argv[1], "\" not connected", (char *) NULL);
return TCL_ERROR;
}
msgs = IvyGetApplicationMessages((IvyClientPtr) Tcl_GetHashValue(entry));
for (scan = msgs; *scan; scan++) {
Tcl_AppendElement(interp, *scan);
}
return TCL_OK;
}
static int
IvySendDirectCmd(ClientData clientData,
Tcl_Interp *interp,
int argc,
const char **argv)
{
Tcl_HashEntry *entry;
unsigned long id;
char *end;
if (argc != 4) {
Tcl_AppendResult(interp, "wrong # of args: \"",
argv[0], " appName id msg\"", (char *) NULL);
return TCL_ERROR;
}
entry = Tcl_FindHashEntry(&app_table, argv[1]);
if (!entry) {
Tcl_AppendResult(interp, "Application \"",
argv[1], "\" not connected", (char *) NULL);
return TCL_ERROR;
}
id = strtol(argv[2], &end, 10);
if (*end) {
Tcl_AppendResult(interp, argv[0], " wrong id: \"", argv[2],
(char *) NULL);
return TCL_ERROR;
}
IvySendDirectMsg((IvyClientPtr) Tcl_GetHashValue(entry), id, (char*)argv[3]);
return TCL_OK;
}
static int
IvyBindDirectCmd(ClientData clientData,
Tcl_Interp *interp,
int argc,
const char **argv)
{
static filter_struct *filter = NULL;
if (argc != 2) {
Tcl_AppendResult(interp, "wrong # of args: \"",
argv[0], " script\"", (char *) NULL);
return TCL_ERROR;
}
if (!filter) {
filter = (filter_struct *) ckalloc(sizeof(filter_struct));
filter->interp = interp;
IvyBindDirectMsg(IvyDirectMsgCB, (void *) filter);
}
else {
ckfree(filter->script);
}
filter->script = ckalloc(strlen(argv[1])+1);
strcpy(filter->script, argv[1]);
return TCL_OK;
}
int
Tclivy_Init(Tcl_Interp *interp)
{
Tcl_CreateCommand(interp, "Ivy::init", (Tcl_CmdProc*)IvyInitCmd, NULL, NULL);
Tcl_CreateCommand(interp, "Ivy::start", (Tcl_CmdProc*)IvyStartCmd, NULL, NULL);
Tcl_CreateCommand(interp, "Ivy::bind", (Tcl_CmdProc*)IvyBindCmd, NULL, NULL);
Tcl_CreateCommand(interp, "Ivy::unbind", (Tcl_CmdProc*)IvyUnbindCmd, NULL, NULL);
Tcl_CreateCommand(interp, "Ivy::send", (Tcl_CmdProc*)IvySendCmd, NULL, NULL);
Tcl_CreateCommand(interp, "Ivy::senddirect", (Tcl_CmdProc*)IvySendDirectCmd, NULL, NULL);
Tcl_CreateCommand(interp, "Ivy::binddirect", (Tcl_CmdProc*)IvyBindDirectCmd, NULL, NULL);
Tcl_CreateCommand(interp, "Ivy::applist", (Tcl_CmdProc*)IvyApplicationListCmd, NULL, NULL);
Tcl_CreateCommand(interp, "Ivy::apphost", (Tcl_CmdProc*)IvyApplicationHostCmd, NULL, NULL);
Tcl_CreateCommand(interp, "Ivy::appmsgs", (Tcl_CmdProc*)IvyApplicationMsgsCmd, NULL, NULL);
#ifndef TCL_CHANNEL_INTEGRATION
Tcl_DoWhenIdle(IvyIdleProc,0);
#endif
return TCL_OK;
}
|