Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

Platform/Aperios1.3.2/UDPEndpoint.cpp

Go to the documentation of this file.
00001 /** 
00002 * @file  Platform/Aperios1.3.2/UDPEndpoint.cpp
00003 *
00004 * Implementation for IP Communication Classes
00005 * @attention this is the Aperios implementation
00006 *
00007 * @author <A href=mailto:robocup@m-wachter.de>Michael Wachter</A>
00008 * 
00009 */
00010 
00011 #include "UDPEndpoint.h"
00012 #include <stdio.h>
00013 #include <iostream.h>
00014 
00015 
00016 UDPEndpoint::UDPEndpoint(int sendBufferSize, int receiveBufferSize)
00017 :IPEndpoint(sendBufferSize,receiveBufferSize)
00018 {
00019    cout << "UDPEndpoint: Creating Endpoint \n";
00020 
00021    antEnvCreateEndpointMsg udpCreateMsg( EndpointType_UDP, sharedSendBufferSize+2048);
00022    udpCreateMsg.Call(ipStackRef, sizeof(udpCreateMsg));
00023 
00024    if (udpCreateMsg.error != ANT_SUCCESS) 
00025    {
00026      cout << "UDPEndpoint: Can't create Endpoint, error " << udpCreateMsg.error << "\n";
00027      return;
00028    }
00029    endpoint = udpCreateMsg.moduleRef;
00030 }
00031 
00032 
00033 UDPEndpoint::~UDPEndpoint()
00034 {
00035   // 
00036   close();
00037 }
00038 
00039 int UDPEndpoint::bind(int port)
00040 {
00041   cout << "UDPEndpoint: bind to port " << port << "\n";
00042   listenPort = port;
00043   UDPEndpointBindMsg bindMsg(endpoint, IP_ADDR_ANY, listenPort);
00044   bindMsg.Call(ipStackRef, sizeof(bindMsg));
00045   
00046   if(bindMsg.error != 0)
00047   {
00048       cout << "UDPEndpoint::bind() Error binding " << 
00049         getErrorString(bindMsg.error) << "\n";
00050   }
00051 
00052   startReceiving();
00053   return(0);
00054 }
00055 
00056 int UDPEndpoint::bind(Port lPort, IPAddress addr, Port sPort)
00057 {
00058   cout << "UDPEndpoint: set sending-addr to " << addr << ":" << sPort << "\n";
00059   sendPort = sPort;
00060   address = addr;
00061   bind(lPort);
00062   return(0);
00063 }
00064 
00065 int UDPEndpoint::bind(int lPort, char* addr, int sPort)
00066 {
00067   cout << "UDPEndpoint: set sending-addr to " << addr << ":" << sPort << "\n";
00068   sendPort = sPort,
00069   address = addr;
00070   bind(lPort);
00071   return(0);
00072 }
00073 
00074 int UDPEndpoint::send(void* data,int size)
00075 {
00076   // Copy data to shared buffer
00077   memcpy(sharedSendBuffer,data,size);
00078   send(size);
00079   return(0);
00080 }
00081 
00082 int UDPEndpoint::send(int size)
00083 {
00084   UDPEndpointSendMsg sendMsg(endpoint,address,sendPort,sharedSendBuffer,size);
00085   sendMsg.continuation = this;
00086   sendMsg.Send(ipStackRef, *myOID_, *sendContSelector, sizeof(sendMsg));
00087   return(0);
00088 }
00089 
00090 int UDPEndpoint::send(int size, char* addr, int sPort)
00091 {
00092    address = addr;
00093    sendPort = sPort;
00094    send(size);
00095    return(0);
00096 }
00097 
00098 
00099 int UDPEndpoint::send(void* data, int size, char* addr,int sPort)
00100 {
00101   address = addr;
00102   sendPort = sPort;
00103   send(data, size);
00104   return(0);
00105 }
00106 
00107 int UDPEndpoint::send(void* data, int size, IPAddress addr,int sPort)
00108 {
00109   address = addr;
00110   sendPort = sPort;
00111   send(data, size);
00112   return(0);
00113 }
00114 
00115 void UDPEndpoint::sendCont(antEnvMsg* msg)
00116 {
00117     // cout << "UDPEndpoint::SendCont \n";
00118     UDPEndpointSendMsg* sendMsg = (UDPEndpointSendMsg*) msg;
00119 
00120     if (sendMsg->error != UDP_SUCCESS)
00121     {
00122       cout << "UDPEndpoint::SendCont Error : " << 
00123         getErrorString(sendMsg->error) << "\n";
00124       return;
00125     }
00126     onSendingDone();
00127    
00128 }
00129 
00130 void UDPEndpoint::onSendingDone()
00131 {
00132 }
00133 
00134 void UDPEndpoint::onReceive(void* data,int size)
00135 {
00136   // cout << "UDPEndpoint::receive Size= " << size << "\n";
00137   return;
00138 }
00139 
00140 void UDPEndpoint::startReceiving()
00141 { 
00142     // cout << "UDPEndpoint::startReceiving()\n";
00143     
00144     UDPEndpointReceiveMsg receiveMsg(endpoint,sharedReceiveBuffer,sharedReceiveBufferSize);
00145     receiveMsg.continuation = this;
00146     receiveMsg.Send(ipStackRef, *myOID_, *receiveContSelector, sizeof(receiveMsg));
00147 }
00148 
00149 void UDPEndpoint::receiveCont(antEnvMsg* msg)
00150 {
00151     // cout << "UDPEndpoint::receiveCont \n";
00152     UDPEndpointReceiveMsg* receiveMsg = (UDPEndpointReceiveMsg*) msg;
00153     if (receiveMsg->error != UDP_SUCCESS)
00154     {
00155        cout << "UDPEndpoint: Error receiving " << 
00156          getErrorString(receiveMsg->error) << "\n";
00157        return;
00158     }
00159     ipOfLastPackage = receiveMsg->address;
00160     onReceive(sharedReceiveBuffer,receiveMsg->size);
00161     startReceiving();
00162 }
00163        
00164 void UDPEndpoint::close()
00165 {
00166     // cout << "UDPEndpoint::close() \n";
00167     onClose();
00168     UDPEndpointCloseMsg closeMsg(endpoint);
00169     closeMsg.continuation = this;
00170     closeMsg.Send( ipStackRef, *myOID_, *closeContSelector, sizeof(closeMsg));
00171 }
00172 
00173 void UDPEndpoint::onClose()
00174 {
00175 }
00176 
00177 void UDPEndpoint::closeCont(antEnvMsg* msg)
00178 {
00179    // cout << "UDPEndpoint::CloseCont \n";
00180    //UDPEndpointCloseMsg closeMsg = (UDPEndpointCloseMsg*) msg;
00181    return;
00182 }
00183  
00184 const char* UDPEndpoint::getErrorString(UDPEndpointError error)
00185 {
00186    switch (error)
00187     {
00188     case UDP_ADDRESSERROR: return "UDP_ADDRESSERROR (IP/Port unvalid)";
00189     case UDP_ADDRESSINUSE: return "UDP_ADDRESSINUSE (IP/Port in used by another endpoint)";
00190     case UDP_BUFFER_INVALID: return "UDP_BUFFER_INVALID (Address not in shared memory)";
00191     case UDP_CONNECTION_BUSY: return "UDP_CONNECTION_BUSY (Endpoint does something else)";
00192     case UDP_CONNECTION_CLOSED: return "UDP_CONNECTION_CLOSED (Endpoint not open)";
00193     case UDP_FAIL: return "UDP_FAIL (no more information is avialble)";
00194     case UDP_HOST_UNREACHABLE: return "UDP_HOST_UNREACHABLE";
00195     case UDP_MESSAGE_TOO_LONG: return "UDP_MESSAGE_TOO_LONG (Paket too long for an intermediate router)";
00196     case UDP_NETWORK_UNREACHABLE: return "UDP_NETWORK_UNREACHABLE";
00197     case UDP_OPERATION_INVALID: return "UDP_OPERATION_INVALID (Operation not allowed in current state of endpoint)";
00198     case UDP_OPERATION_UNKNOWN: return "UDP_OPERATION_UNKNOWN";
00199     case UDP_PORT_UNREACHABLE: return "UDP_PORT_UNREACHABLE";
00200     case UDP_PROTOCOL_UNREACHABLE: return "UDP_PROTOCOL_UNREACHABLE (Target host does not run UDP)";
00201     case UDP_SUCCESS: return "UDP_SUCCESS";
00202     case UDP_TIME_EXCEEDED: return "UDP_TIME_EXCEEDED";
00203     case UDP_TTL_EXCEEDED: return "UDP_TTL_EXCEEDED";
00204           
00205     default: return "UCP_UNKNOWN_ERROR please edit UCPEndpoint::getErrorSring";
00206     }
00207 }
00208 
00209 
00210 /*
00211  * Change log :
00212  * 
00213  * $Log: UDPEndpoint.cpp,v $
00214  * Revision 1.1.1.1  2004/05/22 17:23:41  cvsadm
00215  * created new repository GT2004_WM
00216  *
00217  * Revision 1.6  2004/01/26 13:44:07  wachter
00218  * shared-memory-buffers now have variable sizes
00219  *
00220  * Revision 1.5  2004/01/21 17:33:09  wachter
00221  * UDP Team-communication now working with packets<1400 bytes.
00222  * Not activated at the moment.
00223  *
00224  * Revision 1.4  2004/01/20 14:21:41  wachter
00225  * - Added sender-number to NetSender
00226  * - worked on with Team-Communication
00227  *
00228  * Revision 1.3  2004/01/09 15:44:30  wachter
00229  * Worked on with the Dog-Discovery-Protocol
00230  *
00231  * Revision 1.2  2003/12/11 15:02:37  wachter
00232  * Low-level TCP and UDP functions for aperios now working.
00233  *
00234  * Revision 1.1  2003/12/03 14:21:52  wachter
00235  * Splitted IPEndpoint.*
00236  *
00237  *
00238  */
00239 

Generated on Thu Sep 23 19:57:34 2004 for GT2004 by doxygen 1.3.6