Main Page | Modules | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members

client/client.cpp

Go to the documentation of this file.
00001 // gluicli.cpp : Defines the entry point for the console application.
00002 //
00003 
00004 #include <unistd.h>
00005 #include "../trucknet/trucknet.h"
00006 #include "../trucknet/unp.h"
00007 #include "../trucknet/TCPmsg.h"
00008 #include "../trucknet/msg.h"
00009 #include <string>
00010 using std::string;
00011 
00012 #define BUFLEN 10
00013 
00014 
00015 int quit = 0;
00016 
00017 int processNET(TCPmsg &m);
00018 int processSTDIN(TCPmsg &m);
00019 
00020 void usage(void) {
00021         printf("(F)orward, (B)ackward, (L)eft, (R)ight, (S)top, Start\n");
00022         printf("encoder (T)icks, (M)ove\n");
00023         printf("(C)ontroller, (K)ill Controller, (Q)uit\n");
00024 }
00025 
00026 int main(int argc, char* argv[])
00027 {       
00028         //unsigned char buf[BUFLEN];
00029 
00030         //messaging system setup
00031         TCPmsg m(HOST_IP, GUI_PORT);
00032         //      set_assigns(m);
00033 
00034         int STDIN = fileno(stdin);      
00035         int netFD = m.getFD();
00036         int maxFD = STDIN > netFD ? STDIN : netFD;
00037         fd_set readSet, tmpSet;
00038         FD_ZERO(&readSet);
00039         FD_SET(netFD, &readSet);
00040         FD_SET(STDIN, &readSet);
00041 
00042         usage();
00043 
00044         //user interface loop
00045         while(!quit) {
00046 
00047                 tmpSet = readSet; //have to reset 'set' on each loop
00048                 int result = ::select(maxFD+1, &tmpSet, NULL, NULL, NULL);                              
00049 
00050                 // check for timeout
00051                 if (result == 0) {
00052                         cerr << "main - select timed-out, but no timeout was given" << endl;
00053                         return -1;
00054                 }
00055 
00056                 // check for error
00057                 else if (result == -1) {
00058                         cerr << "select() - err with select : " << strerror(errno) << endl;
00059                         return -1;
00060                 }
00061 
00062                 // one or more files are ready - try each
00063                 else {                          
00064                         if (FD_ISSET(STDIN, &tmpSet))
00065                                 processSTDIN(m);
00066 
00067                         if (FD_ISSET(netFD, &tmpSet))
00068                                 processNET(m);
00069                 }                       
00070         }
00071 }
00072 
00081 int processNET(TCPmsg &m) {
00082         if (m.recv() < 0) {
00083                 cerr << "processNET() : error with TCPmsg.recv()" << endl;
00084                 exit(EXIT_FAILURE);
00085                 //return -1;
00086         }
00087 
00088         if(m.dispatch())
00089         {       
00090                 cout << "Recieved messge : '" << m.type();
00091                 while(m) 
00092                 {       
00093                         cout << ' ' << m;
00094                 }
00095                 cout << '\'' << endl;
00096         }
00097         return 0;
00098 }
00099 
00108 int processSTDIN(TCPmsg &m) {
00109         int c, trash;
00110         c = getchar();
00111         while ( (trash = getchar()) != '\n' && trash != EOF);
00112                         
00113         switch(c) {     
00114         case 'f':
00115         case 'F': 
00116                 cout << "Sending forward..." << endl;
00117                 m << startmsg("forward") << endmsg; break;
00118         case 'b':
00119         case 'B': 
00120                 cout << "Sending back..." << endl;
00121                 m << startmsg("back") << endmsg; break;
00122         case 'l':
00123         case 'L': 
00124                 cout << "Sending left..." << endl;
00125                 m << startmsg("left") << endmsg;break;
00126         case 'r':
00127         case 'R': 
00128                 cout << "Sending right..." << endl;
00129                 m << startmsg("right") << endmsg;break;
00130         case 's':
00131         case 'S': 
00132                 cout << "Sending stop..." << endl;
00133                 m << startmsg("stop") << endmsg;break;
00134         case 'c':
00135         case 'C': 
00136                 cout << "Starting controller..." << endl;
00137                 m << startmsg("run") << endmsg;break;
00138         case 'k':
00139         case 'K': 
00140                 cout << "Killing controller..." << endl;
00141                 m << startmsg("kill") << endmsg;break;
00142         case 't':
00143         case 'T': cout << "Asking for Ticks..." << endl; 
00144                 m << startmsg("ticks") << endmsg;break;
00145                 return 0;
00146         case 'm':
00147         case 'M': cout << "Sending 'move 1.0'..." << endl; 
00148                 m << startmsg("move") << 1.0f << endmsg; break;
00149                 return 0;               
00150         case 'q':
00151         case 'Q': cout << "Quiting..." << endl; 
00152                 quit = 1;
00153                 return 0;               
00154         default: cout << "Invalid command: \"" << (char)c \
00155                                                                 << "\" (" << c << ")" << endl;
00156                 usage();
00157         }
00158 
00159         return 1;
00160 }
00161         
00162 
00163         
00164 

Generated on Fri Sep 1 14:25:44 2006 for Raptor by  doxygen 1.4.4