#define SIARCVCMDPRIORITY 32 #define SIASENDCMDPRIORITY 42 #define commandport 10005 #define commandsize 128 #include #include #include #include #include #include #include #include #include #include "que.h" #include "panel.h" #include "globals.h" //#define DEBUGGING #define DEBUGERRS #define CMDQUESIZE 160 static TQue sendque; static HANDLE queries_comming; void initsendque(void) { if (!initque(&sendque,CMDQUESIZE)) { printf("Not possible to allocate QUE"); }; }; static u_long timeout = 1000;//timeout soketu THREAD(SIAsendcmd,arg) { int errcode = 0; int bytes = 0; unsigned long ip_server = *(unsigned long *)arg; NutThreadSetPriority(SIASENDCMDPRIORITY); #ifdef DEBUGGING printf("\nsendtogal : started : %s :",inet_ntoa(ip_server)); #endif unsigned char *buff; buff = NutHeapAlloc(commandsize); if (buff == NULL) { #ifdef DEBUGERRS printf("Not available memory for sendtogal thread"); #endif NutThreadSetPriority(255); } //cekat na sokety ve fronte pokud tam neco je vyridit TCPSOCKET *serversock; TCPSOCKET *clientsock=0; unsigned char pombuff[2]; //pomocny buffer pro predavani adresy soketu unsigned int n; for(;;) { //tady cekat na neco ve fronte if (readfromque(pombuff,&n,0,&sendque) == ERRQUE_NOERR) { #ifdef DEBUGERRS printf("\nheap : %d",NutHeapAvailable()); #endif //ziskani serveroveho soketu z fronty serversock = (TCPSOCKET *) ((((unsigned int) (pombuff[1]))<<8) | pombuff[0]); //pokud je neco ve fronte a klientsky soket ja zavreny - otevrit ho if ((clientsock->so_state != TCPS_ESTABLISHED) || (clientsock == 0)) { if (clientsock != 0) { #ifdef DEBUGERRS printf("\nsendcmd : closing client socket"); #endif NutTcpCloseSocket(clientsock); } #ifdef DEBUGERRS printf("\nsendcmd : creating client socket"); #endif if ((clientsock = NutTcpCreateSocket()) == 0) { //neni mozne vytvorit soket #ifdef DEBUGERRS printf("\nsendcmd : can't create client socket"); #endif NutSleep(60000); continue; } //pripojit se k serveru (GALAXY) if (NutTcpConnect(clientsock,ip_server,commandport) != 0) {//nelze se pripojit k serveru - tj.ustredne galaxy #ifdef DEBUGERRS printf("\nsendcmd : can't connect to server : %d",NutTcpError(clientsock)); #endif //smaznout celou frontu prijatych pozadavku NutTcpCloseSocket(clientsock); while (removefromque(pombuff,&n,0,&sendque) == ERRQUE_NOERR) { serversock = (TCPSOCKET *) ((((unsigned int) (pombuff[1]))<<8) | pombuff[0]); NutTcpCloseSocket(serversock); }; NutSleep(150); panel_ledon(ERR_led); continue; }; NutTcpSetSockOpt(clientsock,SO_SNDTIMEO,&timeout,sizeof(timeout)); NutTcpSetSockOpt(clientsock,SO_RCVTIMEO,&timeout,sizeof(timeout)); } //cyklus pro komunikaci se sokety panel_ledon(LAN_led); panel_ledoff(ERR_led); while ((!NutTcpError(serversock)) && (!NutTcpError(clientsock))) { //pokud je neni otevren serverovy soket ukoncit cyklus if (serversock->so_state!=TCPS_ESTABLISHED) { #ifdef DEBUGERRS printf("\nsendcmd : serversock isn't established ..."); #endif break; } if ((bytes= NutTcpReceive(serversock,buff,commandsize))<0) { #ifdef DEBUGGING printf("\nsendcmd : receiving command error ... %d",NutTcpError(serversock)); #endif break; }; #ifdef DEBUGGING printf("\nsendcmd : command received ..."); #ifdef DEBUGTEXTS if (bytes < commandsize) { buff[bytes] = 0; } printf("\nsendcmd bytes : %d",bytes); printf("\nsendcmd : %s",buff); #endif #endif //pokud nebyla chyba soketu zaslat prikaz if ((errcode=NutTcpSend(clientsock,buff,bytes))so_state != TCPS_ESTABLISHED)) { #ifdef DEBUGERRS printf("\nsendcmd : clientsock error : %d",NutTcpError(clientsock)); #endif NutTcpCloseSocket(clientsock); }; delfromque(0,&sendque); NutTcpCloseSocket(serversock); panel_ledoff(LAN_led); } else { printf("\nw"); NutEventWait(&queries_comming,0); } } } THREAD(SIArcvcmd,arg) { NutThreadSetPriority(SIARCVCMDPRIORITY); TCPSOCKET *serversock; unsigned char buff[2]; for(;;) { if ((serversock=NutTcpCreateSocket()) != 0) {//soket byl uspesne vytvoren #ifdef DEBUGERRS printf("\nrcvcmd : socket created"); #endif if( NutTcpAccept(serversock,commandport)==0) { #ifdef DEBUGERRS printf("\nrcvcmd : accept"); #endif //k soketu se někdo připojil -> uložit ukazatel na soket do fronty pro jeho vyřízení buff[0] = (((unsigned int)serversock) & 0x00FF); buff[1] = ((((unsigned int)serversock) & 0xFF00) >> 8); if (savetoque(buff,2,&sendque)==ERRQUE_FULL) { #ifdef DEBUGERRS printf("\nrcvcmd : fullque ..."); #endif //plna fronta server soket neni mozne do ni ulozit NutTcpCloseSocket(serversock); } else { #ifdef DEBUGERRS printf("\nrcvcmd : p"); #endif } //vyslat udalost na vzbuzeni vysilaciho vlakna - to se pokusi frontu vyprazdnit NutEventPostAsync(&queries_comming); } } else { #ifdef DEBUGERRS printf("\nrcvcmd : can't create server socket !!"); #endif } #ifdef DEBUGERRS printf("\nnrcvcmd heap : %d",NutHeapAvailable()); #endif } }