|
Revision 197, 1.7 kB
(checked in by ludo, 2 years ago)
|
new client/server protocol for the UI (from r3948)
|
- Property svn:eol-style set to
native
- Property svn:keywords set to
Id Revision
|
| Line | |
|---|
| 1 |
/* |
|---|
| 2 |
* Functions to send commands to the user interface. |
|---|
| 3 |
*/ |
|---|
| 4 |
|
|---|
| 5 |
#include <stdio.h> |
|---|
| 6 |
#include <stdlib.h> |
|---|
| 7 |
#include <string.h> |
|---|
| 8 |
#include <limits.h> |
|---|
| 9 |
#include <unistd.h> |
|---|
| 10 |
#include <signal.h> |
|---|
| 11 |
#include <sys/types.h> |
|---|
| 12 |
#include <sys/resource.h> |
|---|
| 13 |
#include <sys/wait.h> |
|---|
| 14 |
#include <stdarg.h> |
|---|
| 15 |
|
|---|
| 16 |
#include "easy_sock.h" |
|---|
| 17 |
|
|---|
| 18 |
/* return string */ |
|---|
| 19 |
char uisendtmpbuf[1024]; |
|---|
| 20 |
|
|---|
| 21 |
/** |
|---|
| 22 |
* Handles reading errors |
|---|
| 23 |
*/ |
|---|
| 24 |
static void |
|---|
| 25 |
onError (int err) |
|---|
| 26 |
{ |
|---|
| 27 |
if (err != 0) |
|---|
| 28 |
printf (easy_sock_err_msg); |
|---|
| 29 |
} |
|---|
| 30 |
|
|---|
| 31 |
/** |
|---|
| 32 |
* Send a command to the UI. |
|---|
| 33 |
*/ |
|---|
| 34 |
char * |
|---|
| 35 |
ui_send (char *command, int num, ...) |
|---|
| 36 |
{ |
|---|
| 37 |
int port1 = 7001; |
|---|
| 38 |
static int sock = -1; |
|---|
| 39 |
static int cnx = 0; |
|---|
| 40 |
char *s = ""; |
|---|
| 41 |
va_list ap; |
|---|
| 42 |
|
|---|
| 43 |
/* Set error handler */ |
|---|
| 44 |
easy_error (onError); |
|---|
| 45 |
|
|---|
| 46 |
signal (SIGPIPE, SIG_IGN); |
|---|
| 47 |
|
|---|
| 48 |
if (sock == -1) |
|---|
| 49 |
{ |
|---|
| 50 |
sock = easy_tcp_connect ("127.0.0.1", port1); |
|---|
| 51 |
if (sock == -1) |
|---|
| 52 |
{ |
|---|
| 53 |
printf ("Can't connect ! "); |
|---|
| 54 |
printf (easy_sock_err_msg); |
|---|
| 55 |
sock = -2; |
|---|
| 56 |
} |
|---|
| 57 |
else |
|---|
| 58 |
{ |
|---|
| 59 |
cnx = 1; |
|---|
| 60 |
} |
|---|
| 61 |
} |
|---|
| 62 |
if (cnx) |
|---|
| 63 |
{ |
|---|
| 64 |
/* write the called function */ |
|---|
| 65 |
write_string (sock, command); |
|---|
| 66 |
/* number of arguments */ |
|---|
| 67 |
write_int (sock, num); |
|---|
| 68 |
} |
|---|
| 69 |
else |
|---|
| 70 |
{ |
|---|
| 71 |
printf ("DATA: %s %d", command, num); |
|---|
| 72 |
} |
|---|
| 73 |
|
|---|
| 74 |
va_start (ap, num); |
|---|
| 75 |
while (num) |
|---|
| 76 |
{ |
|---|
| 77 |
s = va_arg (ap, char *); |
|---|
| 78 |
if (cnx) |
|---|
| 79 |
{ |
|---|
| 80 |
write_string (sock, s); |
|---|
| 81 |
} |
|---|
| 82 |
else |
|---|
| 83 |
{ |
|---|
| 84 |
printf (" '%s'", s); |
|---|
| 85 |
} |
|---|
| 86 |
num--; |
|---|
| 87 |
} |
|---|
| 88 |
va_end (ap); |
|---|
| 89 |
|
|---|
| 90 |
uisendtmpbuf[0] = 0; |
|---|
| 91 |
if (cnx) |
|---|
| 92 |
{ |
|---|
| 93 |
s = read_string (sock); |
|---|
| 94 |
if (s) |
|---|
| 95 |
{ |
|---|
| 96 |
/* close the connection to the UI */ |
|---|
| 97 |
// if (strcmp(command, "close") == 0) { |
|---|
| 98 |
close (sock); |
|---|
| 99 |
sock = -1; |
|---|
| 100 |
cnx = 0; |
|---|
| 101 |
// } |
|---|
| 102 |
/* not optimal and thread safe */ |
|---|
| 103 |
strncpy (uisendtmpbuf, s, 1023); |
|---|
| 104 |
free (s); |
|---|
| 105 |
} |
|---|
| 106 |
} |
|---|
| 107 |
else |
|---|
| 108 |
{ |
|---|
| 109 |
printf ("\n"); |
|---|
| 110 |
} |
|---|
| 111 |
return uisendtmpbuf; |
|---|
| 112 |
} |
|---|