Ad van der Weiden / Mbed 2 deprecated tcpft

Dependencies:   mbed ConfigFile

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ftserver.cpp Source File

ftserver.cpp

00001 #include "mbed.h"
00002 #include "EthernetNetIf.h"
00003 #include "HTTPServer.h"
00004 #include "ft.h"
00005 #include "ftserver.h"
00006 #define MBED
00007 #include "ftlib.h "
00008 
00009 
00010 ftServer::ftServer(IpAddr host, unsigned short port) {
00011     err = listeningSock.bind(Host(host, port));
00012     if (err) {
00013         printf("Binding failed!\n");
00014         errorMsg();
00015         return;
00016     }
00017     printf("Bound to host\n");
00018     printf("listening on port %d\n", port);
00019     listeningSock.setOnEvent(this, &ftServer::onTCPEvent);
00020     index = 0;
00021     isMsg = false;
00022     text = 0;
00023 }
00024 
00025 void ftServer::errorMsg() {
00026     switch (err) {
00027         case __TCPSOCKET_MIN:
00028             printf("Err: MIN");
00029             break;
00030         case   TCPSOCKET_SETUP:
00031             printf("Err:TCPSocket not properly configured");
00032             break;
00033         case   TCPSOCKET_TIMEOUT:
00034             printf("Err:Connection timed out");
00035             break;
00036         case   TCPSOCKET_IF:
00037             printf("Err:Interface has problems, does not exist or is not initialized");
00038             break;
00039         case   TCPSOCKET_MEM:
00040             printf("Err:Not enough mem");
00041             break;
00042         case   TCPSOCKET_INUSE:
00043             printf("Err:Interface / Port is in use");
00044             break;
00045         case   TCPSOCKET_EMPTY:
00046             printf("Err:Connections queue is empty");
00047             break;
00048         case   TCPSOCKET_RST:
00049             printf("Err:Connection was reset by remote host");
00050             break;
00051         case   TCPSOCKET_OK:
00052             printf("Err:Success");
00053             break;
00054         default:
00055             printf("Err:Unknown");
00056     }
00057     printf("\r\n");
00058 }
00059 
00060 void ftServer::onTCPEvent(TCPSocketEvent ev) {
00061     Host client;
00062     TCPSocket *pConnectedSock;
00063     switch (ev) {
00064         case TCPSOCKET_ACCEPT:
00065             printf("Accept event\r\n");
00066             err = listeningSock.accept(&client, &pConnectedSock);
00067             if (err) {
00068                 printf("accept failed!\r\n");
00069                 errorMsg();
00070                 return; //Could not accept client
00071             }
00072             printf("Accepted\r\n");
00073             queue.push_back(new connection(this, client, pConnectedSock));
00074             break;
00075         default:
00076             printf("listenSocket other event\r\n");
00077             break;
00078     }
00079 }
00080 
00081 void ftServer::remove(connection *c) {
00082     queue.remove(c);
00083     delete c;
00084     int n = 0;
00085     char s[40];
00086     for (list<connection*>::iterator i = queue.begin(); i != queue.end(); i++) {
00087         sprintf(s, "You are in position %d in the queue", n++);
00088         (*i)->writeStream(s);
00089     }
00090 }
00091 
00092 bool ftServer::startServer() {
00093     err = listeningSock.listen(); //Starts listening
00094     if (err) {
00095         printf("listen failed!\n");
00096         errorMsg();
00097         return false;
00098     }
00099     printf("listen OK\t");
00100     return true;
00101 }
00102 /*
00103 void ftServer::pollServer() {
00104     Net::poll();
00105 }
00106 */
00107 char* ftServer::render() {
00108     static char text[40];
00109     sprintf(text, "%02X %02X %02X %02X %02X %02X %02X; ", msg[0],msg[1],msg[2],msg[3],msg[4],msg[5],msg[6]);
00110     return text;
00111 }
00112 
00113 #if 1
00114 bool ftServer::readStream(TCPSocket *pConnectedSock) {
00115     char c;
00116     if (pConnectedSock != queue.front()->socket()) {
00117         char flush[7];
00118         pConnectedSock->recv(flush, sizeof(flush));
00119         return false;
00120     }
00121     while (!isMsg) {
00122         if (pConnectedSock->recv(&c, 1) == 0) {
00123             printf("NoData ");
00124             return false; //no data available
00125         }
00126         isMsg = c == 0x90;//start of message
00127         if (isMsg) {//potentially we are at the start of a message
00128             msg[0] = c; //0x90
00129             printf(" SOM ");
00130             index = 1;
00131             if (text) {
00132                 printf(text);
00133                 delete[] text;
00134                 text = 0;
00135             }
00136             break;
00137         }
00138         //text
00139         if (text == 0) {
00140             textlen = c;//potential length of the string
00141             text = new char[textlen+1];
00142             index = 0;
00143             printf("STX ");
00144         } else {
00145             text[index++] = c;
00146             printf("+");
00147         }
00148         if (index == textlen) { //assume the entire string was read
00149             text[index] = '\0';
00150             printf(text);
00151             printf(" ETX ");
00152 //                        if (s == "QUIT")
00153 //                            return false;//in-band signalling, not used
00154             delete[] text;
00155             text = 0;
00156             return false;//text messages are ignored
00157         }
00158     }
00159     index += pConnectedSock->recv(msg+index, sizeof(msg) - index);
00160     if (index >= sizeof(msg)) {
00161         printf(render());
00162         isMsg = false; //start over on the presumption of text
00163         printf(" EOM\r\n");
00164         return true; //message is ready
00165     }
00166     printf("-");
00167     return false;
00168 }
00169 #else
00170 bool ftServer::readStream() {
00171     char c;
00172     while (pConnectedSock->recv(&c, 1) == 1) {
00173         char txt[20];
00174         sprintf(txt, "%02X ", c);
00175         printf(txt);
00176     }
00177     printf(";");
00178     return false;
00179 }
00180 #endif
00181 
00182 bool ftServer::writeStream(char *s) {
00183     for (list<connection*>::iterator i = queue.begin(); i != queue.end(); i++)
00184         (*i)->writeStream(s);
00185     return true;
00186 }
00187 
00188 bool ftServer::writeStream(SMESSAGE *m) {
00189     for (list<connection*>::iterator i = queue.begin(); i != queue.end(); i++)
00190         (*i)->writeStream(m);
00191     return true;
00192 }
00193 
00194 void connection::onConnectedTCPSocketEvent(TCPSocketEvent ev) {//called as part of NetPoll
00195     switch (ev) {
00196         case TCPSOCKET_CONNECTED:
00197             printf("connectedSocket: Connected to host\n\r");//did not happen
00198             return;
00199          case   TCPSOCKET_ACCEPT://should not happen
00200             printf("connectedSocket: Client is connected, must call accept() to get a new Socket\n\r");
00201             return;
00202         case   TCPSOCKET_READABLE: //this happens when data is received
00203             //printf("connectedSocket: Data in buf\n\r");
00204             if (server->readStream(pConnectedSock)) {
00205                 //writeStream("received the message");
00206                 //writeStream((SMESSAGE*)(msg+1));
00207                 server->handleMessage();
00208             }
00209             return;
00210         case   TCPSOCKET_WRITEABLE://this happens
00211             //printf("connectedSocket: Can write data to buf\n\r");
00212             return;
00213         case   TCPSOCKET_CONTIMEOUT:
00214             printf("connectedSocket: Connection timed out\n\r");
00215             break;
00216         case   TCPSOCKET_CONRST:
00217             printf("connectedSocket: Connection was reset by remote host\n\r");
00218             break;
00219         case   TCPSOCKET_CONABRT:
00220             printf("connectedSocket: Connection was aborted\n\r");
00221             break;
00222         case   TCPSOCKET_ERROR:
00223             printf("connectedSocket: Unknown error\n\r");
00224             break;
00225         case   TCPSOCKET_DISCONNECTED://this happens
00226             printf("connectedSocket: Disconnected\n\r");
00227             break;
00228         default:
00229             printf("connectedSocket: Unknown event\r\n");
00230             return;
00231     }
00232     //disconnected socket
00233     server->remove(this);
00234 }
00235 
00236 bool connection::writeStream(char *s) {//I'm not sure this works for large or rapid messages because NetPoll is not called, when I call it it may be recursively
00237     if (!pConnectedSock)
00238         return false;
00239     char l = strlen(s);
00240     while (pConnectedSock->send(&l, 1)==0);
00241     while (strlen(s))
00242         s += pConnectedSock->send(s, strlen(s));
00243     return true;
00244 }
00245 
00246 bool connection::writeStream(SMESSAGE *m) {//I'm not sure this works for large or rapid messages because NetPoll is not called, when I call it it may be recursively
00247     if (!pConnectedSock)
00248         return false;
00249     char header = 0x90;
00250     char l = 6;
00251     char i = 0;
00252     while (pConnectedSock->send(&header, 1)==0);
00253     while (i<l)
00254         i += pConnectedSock->send((char*)m+i, l-i);
00255     return true;
00256 }