#include <udpoutscheduler.h>
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 |
UdpOutScheduler::~UdpOutScheduler | ( | ) | [virtual] |
00029 { 00030 if (additional_fd >= 0) { 00031 close(additional_fd); 00032 } 00033 00034 delete[] dev; 00035 }
int UdpOutScheduler::initializeNetwork | ( | ) | [protected, virtual] |
Initialize the network.
Implements RealtimeScheduler.
00039 { 00040 dev = new char[IFNAMSIZ]; 00041 00042 // get app port (0 if external app is not used) 00043 int appPort = ev.config()->getAsInt("ExternalApp", "app-port"); 00044 00045 // Initialize TCP socket for App communication if desired 00046 if ( appPort > 0 ) { 00047 00048 struct sockaddr_in server; 00049 int sock; 00050 00051 // Waiting for a TCP connection 00052 // WARNING: Will only accept exactly ONE app connecting to the socket 00053 sock = socket( AF_INET, SOCK_STREAM, 0 ); 00054 if (sock < 0) { 00055 opp_error("Error creating socket"); 00056 return -1; 00057 } 00058 00059 int on = 1; 00060 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); 00061 00062 memset( &server, 0, sizeof (server)); 00063 server.sin_family = AF_INET; 00064 server.sin_addr.s_addr = htonl( INADDR_ANY ); 00065 server.sin_port = htons( appPort ); 00066 00067 if(bind( sock, (struct sockaddr*)&server, sizeof( server)) < 0) { 00068 opp_error("Error binding to app socket"); 00069 return -1; 00070 } 00071 if( listen( sock, 5 ) == -1 ) { 00072 opp_error("Error listening on app socket"); 00073 return -1; 00074 } 00075 // Set additional_fd so we will be called if data 00076 // (i.e. connection requests) is available at sock 00077 additional_fd = sock; 00078 FD_SET(additional_fd, &all_fds); 00079 if( additional_fd > maxfd ) maxfd = additional_fd; 00080 } 00081 00082 // Open UDP port 00083 if (netw_fd > 0) { 00084 // Port is already open, reuse it... 00085 FD_SET(netw_fd, &all_fds); 00086 if( netw_fd> maxfd ) maxfd = netw_fd; 00087 return 0; 00088 } 00089 00090 sockaddr_in addr; 00091 netw_fd = socket( AF_INET, SOCK_DGRAM, 0 ); 00092 memset( &addr, 0, sizeof(addr) ); 00093 addr.sin_family = AF_INET; 00094 addr.sin_addr.s_addr = htonl( INADDR_ANY ); 00095 addr.sin_port = htons( 00096 simulation.moduleByPath("SingleHostNetwork.singleHost.overlay")->par("localPort").longValue() 00097 ); 00098 if(bind( netw_fd, (sockaddr*)&addr, sizeof(addr)) < 0) { 00099 opp_error("Error binding to UDP socket"); 00100 return -1; 00101 } 00102 00103 00104 FD_SET(netw_fd, &all_fds); 00105 if( netw_fd> maxfd ) maxfd = netw_fd; 00106 return 0; 00107 }
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.
00109 { 00110 int new_sock = accept( additional_fd, 0, 0 ); 00111 if (new_sock < 0) { 00112 opp_warning("Error connecting to remote app"); 00113 return; 00114 } 00115 if ( appConnectionLimit ) { 00116 int count = 0; 00117 for ( int fd = 0; fd <= maxfd; fd++ ) { 00118 if( fd == netw_fd ) continue; 00119 if( fd == additional_fd ) continue; 00120 if( FD_ISSET(fd, &all_fds)) count++; 00121 } 00122 if( count > appConnectionLimit ) { 00123 // We already have too many connections to external applications 00124 // "reject" connection 00125 close(new_sock); 00126 ev << "[UdpOutScheduler::additionalFD()]\n" 00127 << " Rejecting new app connection (FD: " << new_sock << ")" 00128 << endl; 00129 return; 00130 } 00131 } 00132 00133 FD_SET(new_sock, &all_fds); 00134 if( new_sock > maxfd ) maxfd = new_sock; 00135 00136 // Inform app about new connection 00137 appPacketBuffer->push_back(PacketBufferEntry(0, 0, PacketBufferEntry::FD_NEW, new_sock)); 00138 sendNotificationMsg(appNotificationMsg, appModule); 00139 00140 ev << "[UdpOutScheduler::additionalFD()]\n" 00141 << " Accepting new app connection (FD: " << new_sock << ")" 00142 << endl; 00143 00144 }
char* UdpOutScheduler::dev [protected] |