Example program with HTTPServer and sensor data streaming over TCPSockets, using Donatien Garnier's Net APIs and services code on top of LWIP. Files StreamServer.h and .cpp encapsulate streaming over TCPSockets. Broadcast is done by sendToAll(), and all incoming data is echoed back to the client. Echo code can be replaced with some remote control of the streaming interface. See main() that shows how to periodically send some data to all subscribed clients. To subscribe, a client should open a socket at <mbed_ip> port 123. I used few lines in TCL code to set up a quick sink for the data. HTTP files are served on port 80 concurrently to the streaming.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers net.cpp Source File

net.cpp

00001 
00002 /*
00003 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
00004  
00005 Permission is hereby granted, free of charge, to any person obtaining a copy
00006 of this software and associated documentation files (the "Software"), to deal
00007 in the Software without restriction, including without limitation the rights
00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009 copies of the Software, and to permit persons to whom the Software is
00010 furnished to do so, subject to the following conditions:
00011  
00012 The above copyright notice and this permission notice shall be included in
00013 all copies or substantial portions of the Software.
00014  
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021 THE SOFTWARE.
00022 */
00023 
00024 #include "net.h"
00025 
00026 //#define __DEBUG
00027 #include "dbg/dbg.h"
00028 
00029 Net::Net() : m_defaultIf(NULL), m_lpIf(), m_lpNetTcpSocket(), m_lpNetUdpSocket()
00030 {
00031 
00032 }
00033 
00034 Net::~Net()
00035 {
00036 
00037 }
00038 
00039 
00040 void Net::poll()
00041 {
00042 
00043   //Poll Services
00044   NetService::servicesPoll();     
00045       
00046   //Poll Interfaces
00047   list<NetIf*>::iterator pIfIt;
00048 
00049   for ( pIfIt = net().m_lpIf.begin() ; pIfIt != net().m_lpIf.end(); pIfIt++ )
00050   {
00051     (*pIfIt)->poll();
00052   }
00053   
00054   //Poll Tcp Sockets
00055   list<NetTcpSocket*>::iterator pNetTcpSocketIt;
00056 
00057   for ( pNetTcpSocketIt = net().m_lpNetTcpSocket.begin() ; pNetTcpSocketIt != net().m_lpNetTcpSocket.end(); )
00058   {
00059     (*pNetTcpSocketIt)->poll();
00060     
00061     if( (*pNetTcpSocketIt)->m_closed && !((*pNetTcpSocketIt)->m_refs) )
00062     {
00063       (*pNetTcpSocketIt)->m_removed = true;
00064       delete (*pNetTcpSocketIt);
00065       (*pNetTcpSocketIt) = NULL;
00066       pNetTcpSocketIt = net().m_lpNetTcpSocket.erase(pNetTcpSocketIt);
00067     }
00068     else
00069     {
00070       pNetTcpSocketIt++;
00071     }
00072   }
00073   
00074   //Poll Udp Sockets
00075   list<NetUdpSocket*>::iterator pNetUdpSocketIt;
00076 
00077   for ( pNetUdpSocketIt = net().m_lpNetUdpSocket.begin() ; pNetUdpSocketIt != net().m_lpNetUdpSocket.end(); )
00078   {
00079     (*pNetUdpSocketIt)->poll();
00080     
00081     if( (*pNetUdpSocketIt)->m_closed && !((*pNetUdpSocketIt)->m_refs) )
00082     {
00083       (*pNetUdpSocketIt)->m_removed = true;
00084       delete (*pNetUdpSocketIt);
00085       (*pNetUdpSocketIt) = NULL;
00086       pNetUdpSocketIt = net().m_lpNetUdpSocket.erase(pNetUdpSocketIt);
00087     }
00088     else
00089     {
00090       pNetUdpSocketIt++;
00091     }
00092   }
00093 
00094 
00095 }
00096 
00097 NetTcpSocket* Net::tcpSocket(NetIf& netif) {
00098   NetTcpSocket* pNetTcpSocket = netif.tcpSocket();
00099   pNetTcpSocket->m_refs++;
00100   return pNetTcpSocket; 
00101 }
00102 
00103 NetTcpSocket* Net::tcpSocket() { //NetTcpSocket on default if
00104  if ( net().m_defaultIf == NULL )
00105       return NULL;
00106   NetTcpSocket* pNetTcpSocket = net().m_defaultIf->tcpSocket();
00107   pNetTcpSocket->m_refs++;
00108   return pNetTcpSocket;
00109 }
00110 
00111 void Net::releaseTcpSocket(NetTcpSocket* pNetTcpSocket)
00112 {
00113   pNetTcpSocket->m_refs--;
00114   if(!pNetTcpSocket->m_closed && !pNetTcpSocket->m_refs)
00115     pNetTcpSocket->close();
00116 }
00117 
00118 NetUdpSocket* Net::udpSocket(NetIf& netif) {
00119   NetUdpSocket* pNetUdpSocket = netif.udpSocket();
00120   pNetUdpSocket->m_refs++;
00121   return pNetUdpSocket; 
00122 }
00123 
00124 NetUdpSocket* Net::udpSocket() { //NetTcpSocket on default if
00125  if ( net().m_defaultIf == NULL )
00126       return NULL;
00127   NetUdpSocket* pNetUdpSocket = net().m_defaultIf->udpSocket();
00128   pNetUdpSocket->m_refs++;
00129   return pNetUdpSocket;
00130 }
00131 
00132 void Net::releaseUdpSocket(NetUdpSocket* pNetUdpSocket)
00133 {
00134   pNetUdpSocket->m_refs--;
00135   if(!pNetUdpSocket->m_closed && !pNetUdpSocket->m_refs)
00136     pNetUdpSocket->close();
00137 }
00138 
00139 NetDnsRequest* Net::dnsRequest(const char* hostname, NetIf& netif) {
00140   return netif.dnsRequest(hostname);
00141 }
00142 
00143 NetDnsRequest* Net::dnsRequest(const char* hostname) { //Create a new NetDnsRequest object from default if
00144   if ( net().m_defaultIf == NULL )
00145       return NULL;
00146   return net().m_defaultIf->dnsRequest(hostname);
00147 }
00148 
00149 NetDnsRequest* Net::dnsRequest(Host* pHost, NetIf& netif)
00150 {
00151   return netif.dnsRequest(pHost);
00152 }
00153 
00154 NetDnsRequest* Net::dnsRequest(Host* pHost) //Creats a new NetDnsRequest object from default if
00155 {
00156   if ( net().m_defaultIf == NULL )
00157       return NULL;
00158   return net().m_defaultIf->dnsRequest(pHost);
00159 }
00160 
00161 void Net::setDefaultIf(NetIf& netif) {
00162   net().m_defaultIf = &netif;
00163 }
00164 
00165 void Net::setDefaultIf(NetIf* pIf)
00166 {
00167   net().m_defaultIf = pIf;
00168 }
00169 
00170 NetIf* Net::getDefaultIf() {
00171   return net().m_defaultIf;
00172 }
00173 
00174 void Net::registerIf(NetIf* pIf)
00175 {
00176   net().m_lpIf.push_back(pIf);
00177 }
00178 
00179 void Net::unregisterIf(NetIf* pIf)
00180 {
00181   if( net().m_defaultIf == pIf )
00182     net().m_defaultIf = NULL;
00183   net().m_lpIf.remove(pIf);
00184 }
00185 
00186 void Net::registerNetTcpSocket(NetTcpSocket* pNetTcpSocket)
00187 {
00188   net().m_lpNetTcpSocket.push_back(pNetTcpSocket);
00189   DBG("NetTcpSocket [ + %p ] %d\r\n", (void*)pNetTcpSocket, net().m_lpNetTcpSocket.size());
00190 }
00191 
00192 void Net::unregisterNetTcpSocket(NetTcpSocket* pNetTcpSocket)
00193 {
00194   DBG("NetTcpSocket [ - %p ] %d\r\n", (void*)pNetTcpSocket, net().m_lpNetTcpSocket.size() - 1);
00195   if(!pNetTcpSocket->m_removed)
00196     net().m_lpNetTcpSocket.remove(pNetTcpSocket);
00197 }
00198 
00199 void Net::registerNetUdpSocket(NetUdpSocket* pNetUdpSocket)
00200 {
00201   net().m_lpNetUdpSocket.push_back(pNetUdpSocket);
00202   DBG("NetUdpSocket [ + %p ] %d\r\n", (void*)pNetUdpSocket, net().m_lpNetUdpSocket.size());
00203 }
00204 
00205 void Net::unregisterNetUdpSocket(NetUdpSocket* pNetUdpSocket)
00206 {
00207   DBG("NetUdpSocket [ - %p ] %d\r\n", (void*)pNetUdpSocket, net().m_lpNetUdpSocket.size() - 1);
00208   if(!pNetUdpSocket->m_removed)
00209     net().m_lpNetUdpSocket.remove(pNetUdpSocket);
00210 }
00211 
00212 Net& Net::net()
00213 {
00214   static Net* pInst = new Net(); //Called only once
00215   return *pInst;
00216 }