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