HTTPClient using static IP

Dependencies:   mbed

Committer:
mr_q
Date:
Mon May 30 11:53:37 2011 +0000
Revision:
0:d8f2f7d5f31b
v0.01 Draft

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mr_q 0:d8f2f7d5f31b 1
mr_q 0:d8f2f7d5f31b 2 /*
mr_q 0:d8f2f7d5f31b 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
mr_q 0:d8f2f7d5f31b 4
mr_q 0:d8f2f7d5f31b 5 Permission is hereby granted, free of charge, to any person obtaining a copy
mr_q 0:d8f2f7d5f31b 6 of this software and associated documentation files (the "Software"), to deal
mr_q 0:d8f2f7d5f31b 7 in the Software without restriction, including without limitation the rights
mr_q 0:d8f2f7d5f31b 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
mr_q 0:d8f2f7d5f31b 9 copies of the Software, and to permit persons to whom the Software is
mr_q 0:d8f2f7d5f31b 10 furnished to do so, subject to the following conditions:
mr_q 0:d8f2f7d5f31b 11
mr_q 0:d8f2f7d5f31b 12 The above copyright notice and this permission notice shall be included in
mr_q 0:d8f2f7d5f31b 13 all copies or substantial portions of the Software.
mr_q 0:d8f2f7d5f31b 14
mr_q 0:d8f2f7d5f31b 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
mr_q 0:d8f2f7d5f31b 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
mr_q 0:d8f2f7d5f31b 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
mr_q 0:d8f2f7d5f31b 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
mr_q 0:d8f2f7d5f31b 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
mr_q 0:d8f2f7d5f31b 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
mr_q 0:d8f2f7d5f31b 21 THE SOFTWARE.
mr_q 0:d8f2f7d5f31b 22 */
mr_q 0:d8f2f7d5f31b 23
mr_q 0:d8f2f7d5f31b 24 #ifndef NETUDPSOCKET_H
mr_q 0:d8f2f7d5f31b 25 #define NETUDPSOCKET_H
mr_q 0:d8f2f7d5f31b 26
mr_q 0:d8f2f7d5f31b 27 #include "net.h"
mr_q 0:d8f2f7d5f31b 28 #include "host.h"
mr_q 0:d8f2f7d5f31b 29
mr_q 0:d8f2f7d5f31b 30 #include <queue>
mr_q 0:d8f2f7d5f31b 31 using std::queue;
mr_q 0:d8f2f7d5f31b 32
mr_q 0:d8f2f7d5f31b 33 //Implements a Berkeley-like socket if
mr_q 0:d8f2f7d5f31b 34 //Can be interfaced either to lwip or a Telit module
mr_q 0:d8f2f7d5f31b 35
mr_q 0:d8f2f7d5f31b 36 enum NetUdpSocketErr
mr_q 0:d8f2f7d5f31b 37 {
mr_q 0:d8f2f7d5f31b 38 __NETUDPSOCKET_MIN = -0xFFFF,
mr_q 0:d8f2f7d5f31b 39 NETUDPSOCKET_SETUP, //NetUdpSocket not properly configured
mr_q 0:d8f2f7d5f31b 40 NETUDPSOCKET_IF, //If has problems
mr_q 0:d8f2f7d5f31b 41 NETUDPSOCKET_MEM, //Not enough mem
mr_q 0:d8f2f7d5f31b 42 NETUDPSOCKET_INUSE, //If/Port is in use
mr_q 0:d8f2f7d5f31b 43 //...
mr_q 0:d8f2f7d5f31b 44 NETUDPSOCKET_OK = 0
mr_q 0:d8f2f7d5f31b 45 };
mr_q 0:d8f2f7d5f31b 46
mr_q 0:d8f2f7d5f31b 47 enum NetUdpSocketEvent //Only one lonely event here... but who knows, maybe some day there'll be another one!
mr_q 0:d8f2f7d5f31b 48 {
mr_q 0:d8f2f7d5f31b 49 NETUDPSOCKET_READABLE, //Data in buf
mr_q 0:d8f2f7d5f31b 50 };
mr_q 0:d8f2f7d5f31b 51
mr_q 0:d8f2f7d5f31b 52
mr_q 0:d8f2f7d5f31b 53 class NetUdpSocket
mr_q 0:d8f2f7d5f31b 54 {
mr_q 0:d8f2f7d5f31b 55 public:
mr_q 0:d8f2f7d5f31b 56 NetUdpSocket();
mr_q 0:d8f2f7d5f31b 57 virtual ~NetUdpSocket(); //close()
mr_q 0:d8f2f7d5f31b 58
mr_q 0:d8f2f7d5f31b 59 virtual NetUdpSocketErr bind(const Host& me) = 0;
mr_q 0:d8f2f7d5f31b 60
mr_q 0:d8f2f7d5f31b 61 virtual int /*if < 0 : NetUdpSocketErr*/ sendto(const char* buf, int len, Host* pHost) = 0;
mr_q 0:d8f2f7d5f31b 62 virtual int /*if < 0 : NetUdpSocketErr*/ recvfrom(char* buf, int len, Host* pHost) = 0;
mr_q 0:d8f2f7d5f31b 63
mr_q 0:d8f2f7d5f31b 64 /* TODO NTH : printf / scanf helpers that call send/recv */
mr_q 0:d8f2f7d5f31b 65
mr_q 0:d8f2f7d5f31b 66 virtual NetUdpSocketErr close() = 0;
mr_q 0:d8f2f7d5f31b 67
mr_q 0:d8f2f7d5f31b 68 virtual NetUdpSocketErr poll() = 0;
mr_q 0:d8f2f7d5f31b 69
mr_q 0:d8f2f7d5f31b 70 class CDummy;
mr_q 0:d8f2f7d5f31b 71 //Callbacks
mr_q 0:d8f2f7d5f31b 72 template<class T>
mr_q 0:d8f2f7d5f31b 73 //Linker bug : Must be defined here :(
mr_q 0:d8f2f7d5f31b 74 void setOnEvent( T* pItem, void (T::*pMethod)(NetUdpSocketEvent) )
mr_q 0:d8f2f7d5f31b 75 {
mr_q 0:d8f2f7d5f31b 76 m_pCbItem = (CDummy*) pItem;
mr_q 0:d8f2f7d5f31b 77 m_pCbMeth = (void (CDummy::*)(NetUdpSocketEvent)) pMethod;
mr_q 0:d8f2f7d5f31b 78 }
mr_q 0:d8f2f7d5f31b 79
mr_q 0:d8f2f7d5f31b 80 void resetOnEvent(); //Disable callback
mr_q 0:d8f2f7d5f31b 81
mr_q 0:d8f2f7d5f31b 82 protected:
mr_q 0:d8f2f7d5f31b 83 void queueEvent(NetUdpSocketEvent e);
mr_q 0:d8f2f7d5f31b 84 void discardEvents();
mr_q 0:d8f2f7d5f31b 85 void flushEvents(); //to be called during polling
mr_q 0:d8f2f7d5f31b 86
mr_q 0:d8f2f7d5f31b 87 Host m_host;
mr_q 0:d8f2f7d5f31b 88 Host m_client;
mr_q 0:d8f2f7d5f31b 89
mr_q 0:d8f2f7d5f31b 90 friend class Net;
mr_q 0:d8f2f7d5f31b 91 int m_refs;
mr_q 0:d8f2f7d5f31b 92
mr_q 0:d8f2f7d5f31b 93 bool m_closed;
mr_q 0:d8f2f7d5f31b 94 bool m_removed;
mr_q 0:d8f2f7d5f31b 95
mr_q 0:d8f2f7d5f31b 96 private:
mr_q 0:d8f2f7d5f31b 97 //We do not want to execute user code in interrupt routines, so we queue events until the server is polled
mr_q 0:d8f2f7d5f31b 98 //If we port this to a multithreaded OS, we could avoid this (however some functions here are not thread-safe, so beware ;) )
mr_q 0:d8f2f7d5f31b 99 void onEvent(NetUdpSocketEvent e); //To be called on poll
mr_q 0:d8f2f7d5f31b 100 CDummy* m_pCbItem;
mr_q 0:d8f2f7d5f31b 101 void (CDummy::*m_pCbMeth)(NetUdpSocketEvent);
mr_q 0:d8f2f7d5f31b 102 queue<NetUdpSocketEvent> m_events;
mr_q 0:d8f2f7d5f31b 103
mr_q 0:d8f2f7d5f31b 104 };
mr_q 0:d8f2f7d5f31b 105
mr_q 0:d8f2f7d5f31b 106 #endif