Bonjour/Zerconf library

Dependencies:   mbed

Committer:
dirkx
Date:
Sat Aug 14 15:54:31 2010 +0000
Revision:
5:8e53abda9900
Parent:
0:355018f44c9f

        

Who changed what in which revision?

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