#include #include #include "tcptool.h" #include "tcptool.m" #include #include #include static HELP_FILE help_session ("tcptool","session"); static void session_go ( int fd, POPENFD &pop, PRIVATE_MESSAGE &msg_input, const char *host, const char *port) { DIALOG dia; dia.setcontext (""); dia.waitfor (msg_input); SSTRING command; dia.newf_str (MSG_U(F_COMMAND,"Command"),command,70); const int NB_USER_BUTTON = 12; PRIVATE_MESSAGE msg_send,msg_list,msg_user[NB_USER_BUTTON]; SSTRING userstr[NB_USER_BUTTON]; dia.new_button (MSG_U(B_SEND,"Send") ,MSG_U(I_SEND,"Transmit the command"),msg_send,true); dia.newline(); dia.gui_passthrough (P_Form,"buttons"); FIELD_BUTTON_TEXT *tbbut[NB_USER_BUTTON]; for (int i=0; iaddrecord (blank); memset (blank,'-',sizeof(blank)-1); clist->setrecord (0,blank); } int nof = 0; int linelist = 0; SSTRING title; title.setfromf (MSG_U(T_TCPSESSION,"Host %s/Port %s"),host,port); while (1){ clist->setcursor (linelist > 0 ? linelist-1 : 0,false); MENU_STATUS code = dia.edit (title.get(),"",help_session,nof ,MENUBUT_CANCEL); SSTRING tosend; if (code == MENU_CANCEL || code == MENU_ESCAPE){ break; }else if (code == MENU_MESSAGE){ dia.save(); UISTATE uistate; diajava_lastmousestate(uistate); if (dialog_testmessage(msg_send)){ if (command.is_empty()){ xconf_error (MSG_U(E_EMPTYCMD,"No command to send")); }else{ tosend.setfrom (command); command.setfrom (""); dia.reload (0); } }else if (dialog_testmessage (msg_list)){ const char *line = clist->getline(sel); if (line != NULL){ int len = strlen(MSG_R(I_SENDLINE)); if (strncmp(line,MSG_R(I_SENDLINE),len)==0){ line = str_skip(line+len); } if (uistate.middleb){ command.setfrom (line); dia.reload (0); }else{ tosend.setfrom (line); } } }else if (dialog_testmessage(msg_input)){ char line[1000]; while (pop.readout (line,sizeof(line)-1)!=-1){ strip_end (line); clist->setrecordf (linelist++,"%s %s" ,MSG_U(I_RECEIVEDLINE,"RECEIVED:"),line); } }else{ for (int i=0; isettext (btitle.get()); userstr[i].setfrom (line); tosend.setfrom (line); SSTRING key; key.setfromf ("btitle-%s-%d",port,i); linuxconf_replace (K_TCPTOOL,key.get(),btitle); key.setfromf ("bdata-%s-%d",port,i); linuxconf_replace (K_TCPTOOL,key.get(),line); linuxconf_save(); } }else{ tosend.setfrom (s); } break; } } } } if (tosend.is_filled()){ const char *s = tosend.get(); clist->setrecordf (linelist++,"%s %s",MSG_U(I_SENDLINE,"SEND:"),s); pop.send (s); pop.send ("\n"); pop.flush (); } } pop.seteof(); } struct SESSION_WAIT{ int fd; POPENFD *pop; PRIVATE_MESSAGE msg; SESSION_WAIT (int _fd){ fd = _fd; pop = new POPENFD (_fd,_fd); } ~SESSION_WAIT (){ delete pop; } }; static void fctwait (void *p) { SESSION_WAIT *w = (SESSION_WAIT*)p; while (1){ int ret = diagui_sync (*w->pop,1); if (ret > 0){ // fprintf (stderr,"fctwait receive\n"); dialog_sendmessage(w->msg); }else if (ret < 0){ break; }else{ // Weird way to test if the socket is still open int nfd = dup(w->fd); if (nfd != -1){ close (nfd); }else{ break; } } } // fprintf (stderr,"fctwait end\n"); delete w; } static void session_go (const char *host, const char *port) { int fd = cmdsock_connect (host,port,20,2); if (fd != -1){ SESSION_WAIT *data = new SESSION_WAIT (fd); uithread (fctwait,data); session_go (fd,*data->pop,data->msg,host,port); } } struct SESSION_DATA { // Use to communicate with the new thread SSTRING host; SSTRING port; SESSION_DATA(const char *_host, const char *_port) { host.setfrom (_host); port.setfrom (_port); } }; static void fct (void *data) { SESSION_DATA *p = (SESSION_DATA*)data; session_go (p->host.get(),p->port.get()); delete p; } void session (const char *host, const char *port) { if (dialog_mode != DIALOG_GUI){ xconf_error (MSG_U(E_ONLYGUI,"Only available in graphical mode")); }else{ SESSION_DATA *data = new SESSION_DATA (host,port); uithread (fct,data); } }