The text of a manual · part 17 of 18
6950 Enterprise Gateway Server User's Guide, pages 131 to 138
Pages 131 to 138 of 144
The original document is the PDF (617 KB). This is its text as extracted, unedited; tables and drawings read best in the PDF, and each page heading opens it at that page.
For the Norand 6950 EGS
Page 131
SECTION 10
/* ** */
Host Application Program we are connected, lets see if this thing can work printf(”\nnui_sock: Listen on Port %d\n”,ipport); fflush(stdout); process_port();
/* ** ** */ shut down the system (traverse the ”connected” list to shutdown each terminal) curr = head; while(curr != NULL) { /* this will shutdown and reset the terminal */ delete_fd(curr–>fdin); curr = curr–>next; } shutdown(listen_fd,2); close(listen_fd); printf(”\nnui_sock: System Shutdown.”); fflush(stdout); exit(0); }
/*****/ /* ** ** */ main function loop for handling the socket calls from the cradle server int process_port() { int hdrcnt=0; int x=0; int clilen=0; struct timeval seltime; int gotdata=0; struct hostent *host_rec; char host[32]; char *ip; int sockfd; int incount=0; 6950 Enterprise Gateway Server User’s Guide
10-17
Page 132
Host Application Program
/* ** */
SECTION 10 use the select to see if anything is waiting for(;;) { if (shut_down) { break; /* user requested shutdown, so goodbye */ } memcpy((void *)&read_fd_set, (void *)&main_fd_set, sizeof(fd_set)); seltime.tv_usec = 0; seltime.tv_sec = 15; /* 15 second timeout */ gotdata=select(FD_SETSIZE,&read_fd_set,0,0,&seltime); if(gotdata > 0) {
/* ** */ it is a new connection, connect it if(FD_ISSET(listen_fd,&read_fd_set)) { clilen = sizeof(cli_addr); sockfd = accept(listen_fd,(struct sockaddr *)&cli_addr,&clilen); if(sockfd < 0) { printf(”\nnui_sock: Accept error. Port=%d (%d)”,ipport,errno); fflush(stdout); } ip=inet_ntoa(cli_addr.sin_addr); host_rec=gethostbyaddr((char *) &cli_addr.sin_addr,4,AF_INET); strcpy(host,host_rec–>h_name); printf(”\nnui_sock: HEARD <%s:%s>”,host,ip); fflush(stdout); FD_CLR(listen_fd,&read_fd_set); add_fd(sockfd,host,ip); our fd list */
10-18
6950 Enterprise Gateway Server User’s Guide
/* add the connection to
Page 133
SECTION 10
Host Application Program
/* send terminal control parameters */ strcpy(tdata,param); write(sockfd,tdata,strlen(tdata)); sleep(1); /* Wait, don’t send the next one too fast */ /* send first entry prompt */ sprintf(work_buf,”DBSCQDEL0H16P16/%–16.16s%–16.16s”,”Enter data now:”,” ”); strcpy(tdata,work_buf); write(sockfd,tdata,strlen(tdata)); } else { /* ** look for an active descriptor and process accordingly */ curr = head; while(curr != NULL) { if(FD_ISSET(curr–>fdin, &read_fd_set)) { if(!process_socket()) { /* this will shutdown/reset the terminal */ delete_fd(curr–>fdin); curr = NULL; break; } } curr = curr–>next; } } } } return(1); }
6950 Enterprise Gateway Server User’s Guide
10-19
Page 134
Host Application Program
SECTION 10
/* ** This function reads data from the socket (response from the ** terminal) and writes to the socket (sends commands to the ** terminal). */ int process_socket() { int datacnt=0; /* ** read the data from the socket */ memset(tdata,0x00,sizeof(tdata)); datacnt=read(curr–>fdin,tdata,sizeof(tdata)); if (datacnt < 1) { printf(”\nnui_sock: Bad message. fd=%d, datacnt=%d”,curr–>fdin,datacnt); fflush(stdout); return(0); } /* ** we have a data message, process it */ if(tdata[0]==’K’ && tdata[1]==’Q’) /* stop and reset this terminal */ { printf(”\nnui_sock: Terminal requested stop.”); fflush(stdout); return(0); } if(tdata[0]==’K’ && tdata[1]==’X’) { /* Stop this application and reset all connected terminals */ printf(”\nnui_sock: Program stop requested.”); fflush(stdout); shut_down = 1; return(0); } if(tdata[strlen(tdata) – 1] == 13) /* don’t forget to strip cr */ tdata[strlen(tdata) – 1] = 0; 10-20
6950 Enterprise Gateway Server User’s Guide
Page 135
SECTION 10
Host Application Program
/* ** I’m not doing anything with the incomming data but sending it ** back, thus I build my new output message from the input ** message */ sprintf(work_buf,”DBSCQDEL0H16P16/%–16.16s%–16.16s%–16.16s %–16.16s”,”Enter data now:”,” ”,”Previous data:”,&tdata[1]); strcpy(tdata,work_buf); /* ** */ write the data to the socket write(curr–>fdin,tdata,strlen(tdata)); return(1); }
/* ** ** */
This function adds a new socket File Descriptor to our linked list of connected FDs (which are actually terminals).
int add_fd(unsigned int fd, char *host, char *ip) { struct LIST *ptr; ptr = (struct LIST *)malloc(sizeof(struct LIST)); if(ptr == NULL) { printf(”\nnui_sock: add_fd Cannot allocate buf ptr.”); fflush(stdout); return(–1); } ptr–>fdin = fd; strcpy(ptr–>ip,ip); strcpy(ptr–>host,host); ptr–>next = NULL; if(tail != NULL) { tail–>next = ptr; tail=ptr; }
6950 Enterprise Gateway Server User’s Guide
10-21
Page 136
Host Application Program
SECTION 10 else { head = ptr; tail = ptr; } FD_SET(fd, &main_fd_set); return(1); } /* ** ** ** */
This function removes a socket File Descriptor from our linked list and sends a reset to the terminal to shut it down properly through the nui_tlnt program.
int delete_fd(unsigned int fd) { struct LIST *before; struct LIST *ptr; int foundflag = 0; ptr = head; before = NULL; while(ptr != NULL) { if(ptr–>fdin == fd) { foundflag = 1; break; } before = ptr; ptr = ptr–>next; } if(!foundflag) { printf(”\nnui_sock: delete_fd Tried to delete an fd not in list!”); fflush(stdout); return(–1); } FD_CLR(ptr–>fdin, &main_fd_set); if(ptr–>fdin > 0) { 10-22
6950 Enterprise Gateway Server User’s Guide
Page 137
SECTION 10
/* ** ** */
Host Application Program sending a ’g’ will reset the terminal and close connection with nui_tlnt tdata[0]=’g’; /* reset this radio only */ tdata[1]=0; write(ptr–>fdin,tdata,strlen(tdata)); sleep(1); /* don’t shutdown the socket too fast */ shutdown(ptr–>fdin,2); /* shutdown this socket connection */ close(ptr–>fdin); } if(before != NULL) { before–>next = ptr–>next; if(ptr == tail) { tail = before; } } else { head = ptr–>next; if(head == NULL) tail = NULL; } free(ptr); return(1); }
/* ***** last line of program
*/
6950 Enterprise Gateway Server User’s Guide
10-23
Page 138
Host Application Program
10-24
6950 Enterprise Gateway Server User’s Guide
SECTION 10