Berckvens Michiel & Basteyns Jonas

Dependencies:   DS1307 TextLCD mbed

Committer:
Michielber
Date:
Thu Dec 11 16:07:34 2014 +0000
Revision:
5:6878defcfeb9
Parent:
0:f615d151a72c
Project updated belastingslijn

Who changed what in which revision?

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