
Dependencies: EthernetNetIf mbed NetServicesMin
message.c@0:be9ccd3a915d, 2011-09-21 (annotated)
- Committer:
- alpsayin
- Date:
- Wed Sep 21 15:54:53 2011 +0000
- Revision:
- 0:be9ccd3a915d
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
alpsayin | 0:be9ccd3a915d | 1 | |
alpsayin | 0:be9ccd3a915d | 2 | #include "message.h" |
alpsayin | 0:be9ccd3a915d | 3 | |
alpsayin | 0:be9ccd3a915d | 4 | void (*getMessageHandler)(TCPSocket* pConnectedSocket, char* msg, int len) = defaultMessageHandler; |
alpsayin | 0:be9ccd3a915d | 5 | void (*postMessageHandler)(TCPSocket* pConnectedSocket,char* msg, int len) = defaultMessageHandler; |
alpsayin | 0:be9ccd3a915d | 6 | |
alpsayin | 0:be9ccd3a915d | 7 | void defaultMessageHandler(TCPSocket* pConnectedSocket, char* msg, int len) |
alpsayin | 0:be9ccd3a915d | 8 | { |
alpsayin | 0:be9ccd3a915d | 9 | printf(msg); |
alpsayin | 0:be9ccd3a915d | 10 | vSendMessage(pConnectedSocket, "DEFAULT\r\n", 9); |
alpsayin | 0:be9ccd3a915d | 11 | } |
alpsayin | 0:be9ccd3a915d | 12 | void vSendMessage(TCPSocket* pConnectedSocket, char* msg, int len) |
alpsayin | 0:be9ccd3a915d | 13 | { |
alpsayin | 0:be9ccd3a915d | 14 | pConnectedSocket->send("RESPONSE\r\n",10); |
alpsayin | 0:be9ccd3a915d | 15 | pConnectedSocket->send(msg,len); |
alpsayin | 0:be9ccd3a915d | 16 | pConnectedSocket->send("\r\n\r\n",4); |
alpsayin | 0:be9ccd3a915d | 17 | } |
alpsayin | 0:be9ccd3a915d | 18 | void vGetMessage(TCPSocket* pConnectedSocket) |
alpsayin | 0:be9ccd3a915d | 19 | { |
alpsayin | 0:be9ccd3a915d | 20 | char line[BUFFER_LENGTH]; |
alpsayin | 0:be9ccd3a915d | 21 | int len; |
alpsayin | 0:be9ccd3a915d | 22 | bool endOfMessage = false; |
alpsayin | 0:be9ccd3a915d | 23 | while(!endOfMessage) |
alpsayin | 0:be9ccd3a915d | 24 | { |
alpsayin | 0:be9ccd3a915d | 25 | len = iGetLine(pConnectedSocket, line, BUFFER_LENGTH); |
alpsayin | 0:be9ccd3a915d | 26 | if(strncmp(line,"GET\r\n", 5)==0) |
alpsayin | 0:be9ccd3a915d | 27 | { |
alpsayin | 0:be9ccd3a915d | 28 | len = iGetLine(pConnectedSocket, line, BUFFER_LENGTH); |
alpsayin | 0:be9ccd3a915d | 29 | getMessageHandler(pConnectedSocket, line, len); |
alpsayin | 0:be9ccd3a915d | 30 | } |
alpsayin | 0:be9ccd3a915d | 31 | else if(strncmp(line,"POST\r\n",6)==0) |
alpsayin | 0:be9ccd3a915d | 32 | { |
alpsayin | 0:be9ccd3a915d | 33 | len = iGetLine(pConnectedSocket, line, BUFFER_LENGTH); |
alpsayin | 0:be9ccd3a915d | 34 | postMessageHandler(pConnectedSocket, line, len); |
alpsayin | 0:be9ccd3a915d | 35 | } |
alpsayin | 0:be9ccd3a915d | 36 | else if(strncmp(line, "\r\n", 2)==0) |
alpsayin | 0:be9ccd3a915d | 37 | { |
alpsayin | 0:be9ccd3a915d | 38 | endOfMessage = true; |
alpsayin | 0:be9ccd3a915d | 39 | } |
alpsayin | 0:be9ccd3a915d | 40 | } |
alpsayin | 0:be9ccd3a915d | 41 | } |
alpsayin | 0:be9ccd3a915d | 42 | int iGetLine(TCPSocket* pConnectedSocket, char* buf, int len) |
alpsayin | 0:be9ccd3a915d | 43 | { |
alpsayin | 0:be9ccd3a915d | 44 | int counter = 0; |
alpsayin | 0:be9ccd3a915d | 45 | while(counter < len) |
alpsayin | 0:be9ccd3a915d | 46 | { |
alpsayin | 0:be9ccd3a915d | 47 | if(pConnectedSocket->recv(&(buf[counter]),1) > 0) |
alpsayin | 0:be9ccd3a915d | 48 | { |
alpsayin | 0:be9ccd3a915d | 49 | if(counter > 0) |
alpsayin | 0:be9ccd3a915d | 50 | { |
alpsayin | 0:be9ccd3a915d | 51 | if(buf[counter]=='\n' && buf[counter-1]=='\r') |
alpsayin | 0:be9ccd3a915d | 52 | { |
alpsayin | 0:be9ccd3a915d | 53 | return counter+1; |
alpsayin | 0:be9ccd3a915d | 54 | } |
alpsayin | 0:be9ccd3a915d | 55 | } |
alpsayin | 0:be9ccd3a915d | 56 | counter++; |
alpsayin | 0:be9ccd3a915d | 57 | } |
alpsayin | 0:be9ccd3a915d | 58 | } |
alpsayin | 0:be9ccd3a915d | 59 | return -1; |
alpsayin | 0:be9ccd3a915d | 60 | } |
alpsayin | 0:be9ccd3a915d | 61 | void vSetMessageHandler(int messageType, void (*messageHandler)(TCPSocket* pConnectedSocket,char* msg, int len)) |
alpsayin | 0:be9ccd3a915d | 62 | { |
alpsayin | 0:be9ccd3a915d | 63 | switch(messageType) |
alpsayin | 0:be9ccd3a915d | 64 | { |
alpsayin | 0:be9ccd3a915d | 65 | case TYPE_GET: getMessageHandler = messageHandler; break; |
alpsayin | 0:be9ccd3a915d | 66 | case TYPE_POST: postMessageHandler = messageHandler; break; |
alpsayin | 0:be9ccd3a915d | 67 | default: break; |
alpsayin | 0:be9ccd3a915d | 68 | } |
alpsayin | 0:be9ccd3a915d | 69 | } |