00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00038
00042
00044
00045
00046 #ifndef __FIFO_TERM_H
00047 #define __FIFO_TERM_H
00048
00049 #ifdef MIP_HOST_APPLE
00050 #include <applePatch.h>
00051 #include <sys/types.h>
00052 #include <sys/stat.h>
00053 #endif
00054
00055 #include <sys/types.h>
00056 #include <sys/stat.h>
00057 #include <stdio.h>
00058 #include <fcntl.h>
00059 #include <unistd.h>
00060 #include <stdlib.h>
00061 #include <signal.h>
00062 #include <assert.h>
00063 #include <string>
00064 #include <cstring>
00065
00066 using namespace std;
00067
00068
00069 #define MAX_FIFO 1000
00070 #define DEF_BUFSIZE 10000
00071
00072 #define MAX_STRING_LENGHT 200
00073
00077 typedef struct {
00078 long type;
00079 char fifo_response[MAX_STRING_LENGHT];
00080 } request;
00081
00085 typedef enum{
00086 FIFO_CLEAR,
00087 FIFO_FLUSH,
00088 FIFO_LINE_ERASE,
00089 FIFO_CLOSE,
00090 FIFO_CMDS
00091 } fifoCmds;
00092
00096 extern char startOfText;
00097 extern char endOfText;
00098 extern char formFeed;
00099 extern char endOfTransmission;
00100
00104 extern char fifoCmdsChar[FIFO_CMDS];
00105
00112 fifoCmds fifoParse(char charCmd);
00113
00115 #define ESC_LINE_ERASE "\033[2K"
00116 #define ESC_CURS_TOP_LEFT "\033[0;0f"
00117 #define ESC_SCREEN_CLEAR "\033[2J"
00118 #define ESC_FORW_SCREEN_CLEAR "\033[0J"
00119 #define ESC_BACK_SCREEN_CLEAR "\033[1J"
00120 #define ESC_HIDE_CURS "?25l"
00121 #define ESC_UNHIDE_CURS "?25h"
00122
00123
00131 class FifoServer{
00132
00133 private:
00134
00135 char *buffer;
00136 unsigned int bufSize;
00138 char fifoName[MAX_STRING_LENGHT];
00139 request r;
00140 bool active;
00141
00142 void ffSetFifoName(char* name){
00143 strcpy(fifoName, name);
00144 }
00145
00146 void ffCreateFifo(){
00147 if (mkfifo(fifoName, O_CREAT | 0666) == -1 )
00148 printf("Errore nella chiamata mkfifo \n");
00149 return;
00150 }
00151
00152 void inline fifoAppend(const char* message){
00153 if(strlen(message) < bufSize){
00154 if(strlen(buffer) + strlen(message) < bufSize-1){
00155 strcat(buffer,message);
00156 }
00157 else{
00158 printf(ESC_CURS_TOP_LEFT);
00159 printf("%s", buffer);
00160 buffer[0] = '\0';
00161 strcat(buffer,message);
00162 }
00163 }
00164 else{
00165 printf("FifoServer: messaggio troppo lungo!\n");
00166 }
00167 }
00168
00175 fifoCmds fifoParse(char charCmd){
00176 fifoCmds cmd;
00177 for(unsigned int i=0U;i<FIFO_CMDS;i++){
00178 cmd = (fifoCmds) i;
00179 if(fifoCmdsChar[cmd]==charCmd)
00180 return cmd;
00181 }
00182 return FIFO_CMDS;
00183 }
00184
00185
00186
00187 void inline fifoExec(char* message){
00188 switch(fifoParse(*message)){
00189 case FIFO_CLEAR:
00190 printf(ESC_SCREEN_CLEAR);
00191 printf(ESC_CURS_TOP_LEFT);
00192 break;
00193 case FIFO_FLUSH:
00194 printf(ESC_CURS_TOP_LEFT);
00195 printf("%s", buffer);
00196 buffer[0] = '\0';
00197 break;
00198 case FIFO_LINE_ERASE:
00199 fifoAppend(ESC_LINE_ERASE);
00200 break;
00201 case FIFO_CLOSE:
00202 active = false;
00203 break;
00204 default:
00205 fifoAppend(message);
00206 }
00207 }
00208
00209 public:
00210
00211
00216 void inline ffReceive(int sleepTime = 1000000){
00217 active = true;
00218 int fd = open(fifoName,O_RDWR);
00219 while(active) {
00220 if (read(fd, &r, sizeof(request)) != 0) {
00221 fifoExec(r.fifo_response);
00222 }
00223 usleep(sleepTime);
00224 }
00225 }
00226
00230 void ffUnlinkFifo(){
00231 unlink(fifoName);
00232 }
00233
00240 FifoServer(char* name, unsigned int bufSize);
00241
00246 ~FifoServer(){
00247
00248 signal(SIGINT,SIG_DFL);
00249 ffUnlinkFifo();
00250 delete buffer;
00251
00252 printf(ESC_UNHIDE_CURS);
00253 return;
00254 }
00255 };
00256
00257
00258
00265 class FifoClient {
00266
00267 private:
00268 bool fifoSet;
00269 char fifoName[MAX_STRING_LENGHT];
00270 request r;
00271
00272 public:
00273
00276 void ffSetFifoName(char* name){
00277 strcpy(fifoName, name);
00278 fifoSet = true;
00279 }
00280
00283 void ffSendMessage(char* message){
00284 if(fifoSet){
00285 int fd;
00286 if ((fd = open(fifoName,1)) == -1 ) {
00287 return;
00288 }
00289 strcpy(r.fifo_response, message);
00290
00291
00292 close(fd);
00293 }
00294 else{
00295
00296 printf("FifoClient: fifo non set yet\n");
00297 }
00298 }
00299
00304 void ffSendCmd(fifoCmds cmd){
00305 if(fifoSet){
00306 int fd;
00307 if ((fd = open(fifoName,1)) == -1 ) {
00308 return;
00309 }
00310 r.fifo_response[0] = fifoCmdsChar[cmd];
00311 r.fifo_response[1] = '\0';
00312
00313
00314 close(fd);
00315 }
00316 else{
00317 printf("FifoClient: fifo non set yet\n");
00318 }
00319 }
00320
00325 FifoClient(){
00326 fifoSet = false;
00327 return;
00328 }
00329 };
00330
00331
00332
00343 class FifoTerm{
00344 private:
00345
00346 FifoClient fifoclient;
00347 char fifoName[10];
00348 pid_t serverPid;
00349
00350 public:
00351
00358 void print(char* message){
00359 if (strlen(message) > MAX_STRING_LENGHT) printf("fifo: impossible to send the message, string lenght > %d", MAX_STRING_LENGHT);
00360 else fifoclient.ffSendMessage(message);
00361 }
00362
00367 void print(string message){
00368 unsigned int size = strlen(message.c_str());
00369 if (size > MAX_STRING_LENGHT){
00370 printf("fifo: impos to send the message, string lenght %d > %d\n",size, MAX_STRING_LENGHT);
00371 }else{
00372 fifoclient.ffSendMessage((char*) message.c_str());
00373 }
00374 }
00375
00381 void lineErase(void){
00382 fifoclient.ffSendCmd(FIFO_LINE_ERASE);
00383 }
00384
00390 void clear(void){
00391 fifoclient.ffSendCmd(FIFO_CLEAR);
00392 }
00393
00400 void flush(void){
00401 fifoclient.ffSendCmd(FIFO_FLUSH);
00402 }
00403
00410 void close(void){
00411 fifoclient.ffSendCmd(FIFO_CLOSE);
00412 }
00413
00418 FifoTerm(char *title, unsigned int bufSize=DEF_BUFSIZE){
00419
00420 struct stat sts;
00421 unsigned int i=0;
00422 for(i=0;i<MAX_FIFO;i++){
00423 sprintf(fifoName,"fifo%03d\0",i);
00424 if((stat (fifoName, &sts)) == -1) break;
00425
00426 }
00427 if(i==MAX_FIFO){
00428 printf("fifo disponibili esaurite!!!\n");
00429 }
00430
00431 if ((serverPid = fork()) == 0){
00432
00433 char bufS[10];
00434 char command[100]="\0";
00435
00436 strcat(command,"./FifoServer ");
00437 strcat(command,fifoName);
00438 sprintf(bufS," %d\0",bufSize);
00439 strcat(command,bufS);
00440 printf("%s\n",command);
00441 if ( (execl(
00442 #ifdef MIP_HOST_APPLE
00443 "/usr/X11/bin/xterm", "/usr/X11/bin/xtermxterm",
00444 #else
00445 "/usr/bin/xterm", "/usr/bin/xtermxterm",
00446 #endif
00447 "-l", "-lf", "FifoServer.log", "-title", title, "-bg", "white", "-fs", "10", "-geometry", "100x30",
00448 "-e", command, (char *) 0)) < 0) {
00449 printf("FifoServer execution error\n");
00450 sleep(10);
00451 }
00452 }
00453 else{
00454
00455 fifoclient.ffSetFifoName(fifoName);
00456 }
00457 }
00458
00463
00464
00465
00466
00467
00468 ~FifoTerm(){
00469 close();
00470 }
00471 };
00472
00473
00474 #endif
00475
00476
00480
00481
00482