UdpOutScheduler Class Reference

#include <udpoutscheduler.h>

Inheritance diagram for UdpOutScheduler:

RealtimeScheduler List of all members.

Public Member Functions

virtual ~UdpOutScheduler ()

Protected Member Functions

virtual int initializeNetwork ()
 Initialize the network.
virtual void additionalFD ()
 This function is called from main loop if data is accessable from "additional_fd".

Protected Attributes

char * dev

Constructor & Destructor Documentation

UdpOutScheduler::~UdpOutScheduler (  )  [virtual]

00029 {
00030     if (app_fd >= 0) {
00031         close(app_fd);
00032     }
00033     if (additional_fd >= 0) {
00034         close(additional_fd);
00035     }
00036 
00037     delete dev;
00038 }


Member Function Documentation

int UdpOutScheduler::initializeNetwork (  )  [protected, virtual]

Initialize the network.

Implements RealtimeScheduler.

00042 {
00043     dev = new char[IFNAMSIZ];
00044 
00045     // get app port (0 if external app is not used)
00046     int appPort = ev.config()->getAsInt("ExternalApp", "app-port");
00047 
00048     // Initialize TCP socket for App communication if desired
00049     if ( appPort > 0 ) {
00050 
00051         struct sockaddr_in server;
00052         int sock;
00053 
00054         // Waiting for a TCP connection
00055         // WARNING: Will only accept exactly ONE app connecting to the socket
00056         sock = socket( AF_INET, SOCK_STREAM, 0 );
00057         if (sock < 0) {
00058             opp_error("Error creating socket");
00059             return -1;
00060         }
00061 
00062         int on = 1;
00063         setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
00064 
00065         memset( &server, 0, sizeof (server));
00066         server.sin_family = AF_INET;
00067         server.sin_addr.s_addr = htonl( INADDR_ANY );
00068         server.sin_port = htons( appPort );
00069 
00070         if(bind( sock, (struct sockaddr*)&server, sizeof( server)) < 0) {
00071             opp_error("Error binding to app socket");
00072             return -1;
00073         }
00074         if( listen( sock, 5 ) == -1 ) {
00075             opp_error("Error listening on app socket");
00076             return -1;
00077         }
00078         // Set additional_fd so we will be called if data
00079         // (i.e. connection requests) is available at sock
00080         additional_fd = sock;
00081     }
00082 
00083     // Open UDP port
00084     if (netw_fd > 0) {
00085         // Port is already open, reuse it...
00086         return 0;
00087     }
00088 
00089     sockaddr_in addr;
00090     netw_fd = socket( AF_INET, SOCK_DGRAM, 0 );
00091     memset( &addr, 0, sizeof(addr) );
00092     addr.sin_family = AF_INET;
00093     addr.sin_addr.s_addr = htonl( INADDR_ANY );
00094     addr.sin_port = htons(
00095             simulation.moduleByPath("SingleHostNetwork.singleHost.overlay")->par("localPort").longValue()
00096     );
00097     if(bind( netw_fd, (sockaddr*)&addr, sizeof(addr)) < 0) {
00098         opp_error("Error binding to UDP socket");
00099         return -1;
00100     }
00101 
00102 
00103     return 0;
00104 }

void UdpOutScheduler::additionalFD (  )  [protected, virtual]

This function is called from main loop if data is accessable from "additional_fd".

This FD can be set in initializeNetwork by concrete implementations.

Reimplemented from RealtimeScheduler.

00106                                    {
00107     int new_sock = accept( additional_fd, 0, 0 );
00108     if (app_fd >= 0) {
00109         // We already have a connection to an application
00110         // "reject" connection
00111         close(new_sock);
00112         ev << "Rejecting new app connection (FD: " << new_sock << ")\n";
00113     } else {
00114         app_fd = new_sock;
00115         ev << "Accepting new app connection (FD: " << app_fd << ")\n";
00116         if (app_fd < 0) {
00117             opp_warning("Error connecting to remote app");
00118             app_fd = -1;
00119         }
00120     }
00121 
00122 }


Member Data Documentation

char* UdpOutScheduler::dev [protected]


The documentation for this class was generated from the following files:
Generated on Tue Jul 24 16:51:19 2007 for ITM OverSim by  doxygen 1.5.1