Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simon 0:350011bf8be7 1
simon 0:350011bf8be7 2 /*
simon 0:350011bf8be7 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
simon 0:350011bf8be7 4
simon 0:350011bf8be7 5 Permission is hereby granted, free of charge, to any person obtaining a copy
simon 0:350011bf8be7 6 of this software and associated documentation files (the "Software"), to deal
simon 0:350011bf8be7 7 in the Software without restriction, including without limitation the rights
simon 0:350011bf8be7 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
simon 0:350011bf8be7 9 copies of the Software, and to permit persons to whom the Software is
simon 0:350011bf8be7 10 furnished to do so, subject to the following conditions:
simon 0:350011bf8be7 11
simon 0:350011bf8be7 12 The above copyright notice and this permission notice shall be included in
simon 0:350011bf8be7 13 all copies or substantial portions of the Software.
simon 0:350011bf8be7 14
simon 0:350011bf8be7 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
simon 0:350011bf8be7 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
simon 0:350011bf8be7 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
simon 0:350011bf8be7 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
simon 0:350011bf8be7 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
simon 0:350011bf8be7 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
simon 0:350011bf8be7 21 THE SOFTWARE.
simon 0:350011bf8be7 22 */
simon 0:350011bf8be7 23
simon 0:350011bf8be7 24 #include "net.h"
simon 0:350011bf8be7 25
simon 0:350011bf8be7 26 #include "host.h"
simon 0:350011bf8be7 27 #include "ipaddr.h"
simon 0:350011bf8be7 28 #include "netservice.h"
simon 0:350011bf8be7 29 #include "if/net/netif.h"
simon 0:350011bf8be7 30 #include "if/net/nettcpsocket.h"
simon 0:350011bf8be7 31 #include "if/net/netudpsocket.h"
simon 0:350011bf8be7 32 #include "if/net/netdnsrequest.h"
simon 0:350011bf8be7 33
simon 0:350011bf8be7 34 //#define __DEBUG
simon 0:350011bf8be7 35 #include "dbg/dbg.h"
simon 0:350011bf8be7 36
simon 0:350011bf8be7 37 Net::Net() : m_defaultIf(NULL), m_lpIf(), m_lpNetTcpSocket(), m_lpNetUdpSocket()
simon 0:350011bf8be7 38 {
simon 0:350011bf8be7 39
simon 0:350011bf8be7 40 }
simon 0:350011bf8be7 41
simon 0:350011bf8be7 42 Net::~Net()
simon 0:350011bf8be7 43 {
simon 0:350011bf8be7 44
simon 0:350011bf8be7 45 }
simon 0:350011bf8be7 46
simon 0:350011bf8be7 47
simon 0:350011bf8be7 48 void Net::poll()
simon 0:350011bf8be7 49 {
simon 0:350011bf8be7 50 //DBG("\r\nNet : Services polling\r\n");
simon 0:350011bf8be7 51
simon 0:350011bf8be7 52 //Poll Services
simon 0:350011bf8be7 53 NetService::servicesPoll();
simon 0:350011bf8be7 54
simon 0:350011bf8be7 55 //DBG("\r\nNet : Interfaces polling\r\n");
simon 0:350011bf8be7 56
simon 0:350011bf8be7 57 //Poll Interfaces
simon 0:350011bf8be7 58 list<NetIf*>::iterator pIfIt;
simon 0:350011bf8be7 59
simon 0:350011bf8be7 60 for ( pIfIt = net().m_lpIf.begin() ; pIfIt != net().m_lpIf.end(); pIfIt++ )
simon 0:350011bf8be7 61 {
simon 0:350011bf8be7 62 (*pIfIt)->poll();
simon 0:350011bf8be7 63 }
simon 0:350011bf8be7 64
simon 0:350011bf8be7 65 //DBG("\r\nNet : Sockets polling\r\n");
simon 0:350011bf8be7 66
simon 0:350011bf8be7 67 //Poll Tcp Sockets
simon 0:350011bf8be7 68 list<NetTcpSocket*>::iterator pNetTcpSocketIt;
simon 0:350011bf8be7 69
simon 0:350011bf8be7 70 for ( pNetTcpSocketIt = net().m_lpNetTcpSocket.begin() ; pNetTcpSocketIt != net().m_lpNetTcpSocket.end(); )
simon 0:350011bf8be7 71 {
simon 0:350011bf8be7 72 (*pNetTcpSocketIt)->poll();
simon 0:350011bf8be7 73
simon 0:350011bf8be7 74 if( (*pNetTcpSocketIt)->m_closed && !((*pNetTcpSocketIt)->m_refs) )
simon 0:350011bf8be7 75 {
simon 0:350011bf8be7 76 (*pNetTcpSocketIt)->m_removed = true;
simon 0:350011bf8be7 77 delete (*pNetTcpSocketIt);
simon 0:350011bf8be7 78 (*pNetTcpSocketIt) = NULL;
simon 0:350011bf8be7 79 pNetTcpSocketIt = net().m_lpNetTcpSocket.erase(pNetTcpSocketIt);
simon 0:350011bf8be7 80 }
simon 0:350011bf8be7 81 else
simon 0:350011bf8be7 82 {
simon 0:350011bf8be7 83 pNetTcpSocketIt++;
simon 0:350011bf8be7 84 }
simon 0:350011bf8be7 85 }
simon 0:350011bf8be7 86
simon 0:350011bf8be7 87 //Poll Udp Sockets
simon 0:350011bf8be7 88 list<NetUdpSocket*>::iterator pNetUdpSocketIt;
simon 0:350011bf8be7 89
simon 0:350011bf8be7 90 for ( pNetUdpSocketIt = net().m_lpNetUdpSocket.begin() ; pNetUdpSocketIt != net().m_lpNetUdpSocket.end(); )
simon 0:350011bf8be7 91 {
simon 0:350011bf8be7 92 (*pNetUdpSocketIt)->poll();
simon 0:350011bf8be7 93
simon 0:350011bf8be7 94 if( (*pNetUdpSocketIt)->m_closed && !((*pNetUdpSocketIt)->m_refs) )
simon 0:350011bf8be7 95 {
simon 0:350011bf8be7 96 (*pNetUdpSocketIt)->m_removed = true;
simon 0:350011bf8be7 97 delete (*pNetUdpSocketIt);
simon 0:350011bf8be7 98 (*pNetUdpSocketIt) = NULL;
simon 0:350011bf8be7 99 pNetUdpSocketIt = net().m_lpNetUdpSocket.erase(pNetUdpSocketIt);
simon 0:350011bf8be7 100 }
simon 0:350011bf8be7 101 else
simon 0:350011bf8be7 102 {
simon 0:350011bf8be7 103 pNetUdpSocketIt++;
simon 0:350011bf8be7 104 }
simon 0:350011bf8be7 105 }
simon 0:350011bf8be7 106
simon 0:350011bf8be7 107
simon 0:350011bf8be7 108 }
simon 0:350011bf8be7 109
simon 0:350011bf8be7 110 NetTcpSocket* Net::tcpSocket(NetIf& netif) {
simon 0:350011bf8be7 111 NetTcpSocket* pNetTcpSocket = netif.tcpSocket();
simon 0:350011bf8be7 112 pNetTcpSocket->m_refs++;
simon 0:350011bf8be7 113 return pNetTcpSocket;
simon 0:350011bf8be7 114 }
simon 0:350011bf8be7 115
simon 0:350011bf8be7 116 NetTcpSocket* Net::tcpSocket() { //NetTcpSocket on default if
simon 0:350011bf8be7 117 if ( net().m_defaultIf == NULL )
simon 0:350011bf8be7 118 return NULL;
simon 0:350011bf8be7 119 NetTcpSocket* pNetTcpSocket = net().m_defaultIf->tcpSocket();
simon 0:350011bf8be7 120 pNetTcpSocket->m_refs++;
simon 0:350011bf8be7 121 return pNetTcpSocket;
simon 0:350011bf8be7 122 }
simon 0:350011bf8be7 123
simon 0:350011bf8be7 124 void Net::releaseTcpSocket(NetTcpSocket* pNetTcpSocket)
simon 0:350011bf8be7 125 {
simon 0:350011bf8be7 126 pNetTcpSocket->m_refs--;
simon 0:350011bf8be7 127 if(!pNetTcpSocket->m_closed && !pNetTcpSocket->m_refs)
simon 0:350011bf8be7 128 pNetTcpSocket->close();
simon 0:350011bf8be7 129 }
simon 0:350011bf8be7 130
simon 0:350011bf8be7 131 NetUdpSocket* Net::udpSocket(NetIf& netif) {
simon 0:350011bf8be7 132 NetUdpSocket* pNetUdpSocket = netif.udpSocket();
simon 0:350011bf8be7 133 pNetUdpSocket->m_refs++;
simon 0:350011bf8be7 134 return pNetUdpSocket;
simon 0:350011bf8be7 135 }
simon 0:350011bf8be7 136
simon 0:350011bf8be7 137 NetUdpSocket* Net::udpSocket() { //NetTcpSocket on default if
simon 0:350011bf8be7 138 if ( net().m_defaultIf == NULL )
simon 0:350011bf8be7 139 return NULL;
simon 0:350011bf8be7 140 NetUdpSocket* pNetUdpSocket = net().m_defaultIf->udpSocket();
simon 0:350011bf8be7 141 pNetUdpSocket->m_refs++;
simon 0:350011bf8be7 142 return pNetUdpSocket;
simon 0:350011bf8be7 143 }
simon 0:350011bf8be7 144
simon 0:350011bf8be7 145 void Net::releaseUdpSocket(NetUdpSocket* pNetUdpSocket)
simon 0:350011bf8be7 146 {
simon 0:350011bf8be7 147 pNetUdpSocket->m_refs--;
simon 0:350011bf8be7 148 if(!pNetUdpSocket->m_closed && !pNetUdpSocket->m_refs)
simon 0:350011bf8be7 149 pNetUdpSocket->close();
simon 0:350011bf8be7 150 }
simon 0:350011bf8be7 151
simon 0:350011bf8be7 152 NetDnsRequest* Net::dnsRequest(const char* hostname, NetIf& netif) {
simon 0:350011bf8be7 153 return netif.dnsRequest(hostname);
simon 0:350011bf8be7 154 }
simon 0:350011bf8be7 155
simon 0:350011bf8be7 156 NetDnsRequest* Net::dnsRequest(const char* hostname) { //Create a new NetDnsRequest object from default if
simon 0:350011bf8be7 157 if ( net().m_defaultIf == NULL )
simon 0:350011bf8be7 158 return NULL;
simon 0:350011bf8be7 159 return net().m_defaultIf->dnsRequest(hostname);
simon 0:350011bf8be7 160 }
simon 0:350011bf8be7 161
simon 0:350011bf8be7 162 NetDnsRequest* Net::dnsRequest(Host* pHost, NetIf& netif)
simon 0:350011bf8be7 163 {
simon 0:350011bf8be7 164 return netif.dnsRequest(pHost);
simon 0:350011bf8be7 165 }
simon 0:350011bf8be7 166
simon 0:350011bf8be7 167 NetDnsRequest* Net::dnsRequest(Host* pHost) //Creats a new NetDnsRequest object from default if
simon 0:350011bf8be7 168 {
simon 0:350011bf8be7 169 if ( net().m_defaultIf == NULL )
simon 0:350011bf8be7 170 return NULL;
simon 0:350011bf8be7 171 return net().m_defaultIf->dnsRequest(pHost);
simon 0:350011bf8be7 172 }
simon 0:350011bf8be7 173
simon 0:350011bf8be7 174 void Net::setDefaultIf(NetIf& netif) {
simon 0:350011bf8be7 175 net().m_defaultIf = &netif;
simon 0:350011bf8be7 176 }
simon 0:350011bf8be7 177
simon 0:350011bf8be7 178 void Net::setDefaultIf(NetIf* pIf)
simon 0:350011bf8be7 179 {
simon 0:350011bf8be7 180 net().m_defaultIf = pIf;
simon 0:350011bf8be7 181 }
simon 0:350011bf8be7 182
simon 0:350011bf8be7 183 NetIf* Net::getDefaultIf() {
simon 0:350011bf8be7 184 return net().m_defaultIf;
simon 0:350011bf8be7 185 }
simon 0:350011bf8be7 186
simon 0:350011bf8be7 187 void Net::registerIf(NetIf* pIf)
simon 0:350011bf8be7 188 {
simon 0:350011bf8be7 189 net().m_lpIf.push_back(pIf);
simon 0:350011bf8be7 190 }
simon 0:350011bf8be7 191
simon 0:350011bf8be7 192 void Net::unregisterIf(NetIf* pIf)
simon 0:350011bf8be7 193 {
simon 0:350011bf8be7 194 if( net().m_defaultIf == pIf )
simon 0:350011bf8be7 195 net().m_defaultIf = NULL;
simon 0:350011bf8be7 196 net().m_lpIf.remove(pIf);
simon 0:350011bf8be7 197 }
simon 0:350011bf8be7 198
simon 0:350011bf8be7 199 void Net::registerNetTcpSocket(NetTcpSocket* pNetTcpSocket)
simon 0:350011bf8be7 200 {
simon 0:350011bf8be7 201 net().m_lpNetTcpSocket.push_back(pNetTcpSocket);
simon 0:350011bf8be7 202 DBG("\r\nNetTcpSocket [ + %p ] %d\r\n", (void*)pNetTcpSocket, net().m_lpNetTcpSocket.size());
simon 0:350011bf8be7 203 }
simon 0:350011bf8be7 204
simon 0:350011bf8be7 205 void Net::unregisterNetTcpSocket(NetTcpSocket* pNetTcpSocket)
simon 0:350011bf8be7 206 {
simon 0:350011bf8be7 207 DBG("\r\nNetTcpSocket [ - %p ] %d\r\n", (void*)pNetTcpSocket, net().m_lpNetTcpSocket.size() - 1);
simon 0:350011bf8be7 208 if(!pNetTcpSocket->m_removed)
simon 0:350011bf8be7 209 net().m_lpNetTcpSocket.remove(pNetTcpSocket);
simon 0:350011bf8be7 210 }
simon 0:350011bf8be7 211
simon 0:350011bf8be7 212 void Net::registerNetUdpSocket(NetUdpSocket* pNetUdpSocket)
simon 0:350011bf8be7 213 {
simon 0:350011bf8be7 214 net().m_lpNetUdpSocket.push_back(pNetUdpSocket);
simon 0:350011bf8be7 215 DBG("\r\nNetUdpSocket [ + %p ] %d\r\n", (void*)pNetUdpSocket, net().m_lpNetUdpSocket.size());
simon 0:350011bf8be7 216 }
simon 0:350011bf8be7 217
simon 0:350011bf8be7 218 void Net::unregisterNetUdpSocket(NetUdpSocket* pNetUdpSocket)
simon 0:350011bf8be7 219 {
simon 0:350011bf8be7 220 DBG("\r\nNetUdpSocket [ - %p ] %d\r\n", (void*)pNetUdpSocket, net().m_lpNetUdpSocket.size() - 1);
simon 0:350011bf8be7 221 if(!pNetUdpSocket->m_removed)
simon 0:350011bf8be7 222 net().m_lpNetUdpSocket.remove(pNetUdpSocket);
simon 0:350011bf8be7 223 }
simon 0:350011bf8be7 224
simon 0:350011bf8be7 225 Net& Net::net()
simon 0:350011bf8be7 226 {
simon 0:350011bf8be7 227 static Net* pInst = new Net(); //Called only once
simon 0:350011bf8be7 228 return *pInst;
simon 0:350011bf8be7 229 }