OverSim
realtimescheduler.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 
24 #ifndef __REALTIMESCHEDULER_H__
25 #define __REALTIMESCHEDULER_H__
26 
27 #define WANT_WINSOCK2
28 #include <platdep/sockets.h>
29 #include <omnetpp.h>
30 #include <list>
31 #include <climits>
32 
33 
35 public:
36  char* data;
37  int32_t length;
38  sockaddr* addr;
39  socklen_t addrlen;
40  enum fdCommand {
45  } func;
46  SOCKET fd;
47  PacketBufferEntry(char* buf, int32_t len) :
48  data(buf), length(len), addr(0), addrlen(0), func(PACKET_DATA), fd(0) {};
49  PacketBufferEntry(char* buf, int32_t len, sockaddr* ad, socklen_t al) :
50  data(buf), length(len), addr(ad), addrlen(al), func(PACKET_DATA), fd(0) {};
51  PacketBufferEntry(char* buf, int32_t len, fdCommand fc, int _fd) :
52  data(buf), length(len), addr(0), addrlen(0), func(fc), fd(_fd) {};
53  PacketBufferEntry(char* buf, int32_t len, sockaddr* ad, socklen_t al,
54  fdCommand fc, int _fd) :
55  data(buf), length(len), addr(ad), addrlen(al), func(fc), fd(_fd) {};
56 };
57 
58 typedef std::list<PacketBufferEntry> PacketBuffer;
59 
65 class RealtimeScheduler : public cScheduler
66 {
67 
68 
69 protected:
70  class SocketContext {
71  public:
73  SocketContext(cModule* mod, cMessage* notifMsg, PacketBuffer* buffer,
74  size_t mtu) : mod(mod), notifMsg(notifMsg), buffer(buffer),
75  mtu(mtu) {};
76 
77  cModule* mod;
78  cMessage* notifMsg;
80  size_t mtu;
81  };
82 
83  std::map<SOCKET, SocketContext> socketContextMap;
84 
85  // FD set with all file descriptors
86  fd_set all_fds;
87  SOCKET maxfd;
88 
89  // Buffer, module and FD for network communication
90  SOCKET netw_fd;
91  SOCKET apptun_fd;
92  cModule *module;
93  cMessage *notificationMsg;
95  size_t buffersize;
96 
97  // ... for realworldapp communication
98  cModule *appModule;
99  cMessage *appNotificationMsg;
103 
104  SOCKET additional_fd; // Can be used by concrete implementations of this class
105 
106  // state
107  timeval baseTime;
108 
111  virtual int initializeNetwork() = 0;
112 
117  virtual void additionalFD() {};
118 
125  virtual bool receiveWithTimeout(long usec);
126 
133  virtual int receiveUntil(const timeval& targetTime);
134 
135 public:
140 
144  virtual ~RealtimeScheduler();
145 
149  virtual void startRun();
150 
154  virtual void endRun();
155 
159  virtual void executionResumed();
160 
172  virtual void setInterfaceModule(cModule *module,
173  cMessage *notificationMsg,
174  PacketBuffer* buffer,
175  int mtu,
176  bool isApp = false);
177 
178  void registerSocket(SOCKET fd, cModule *mod, cMessage *notifMsg,
179  PacketBuffer* buffer, int mtu);
180 
181 
185  virtual cMessage *getNextEvent();
186 
193  void sendNotificationMsg(cMessage* msg, cModule* mod);
194 
208  virtual ssize_t sendBytes(const char *buf,
209  size_t numBytes,
210  sockaddr* addr = 0,
211  socklen_t addrlen = 0,
212  bool isApp = false,
213  SOCKET fd = INVALID_SOCKET);
214 
218  void closeAppSocket(SOCKET fd);
219 
225  virtual SOCKET getAppTunFd() { return apptun_fd; };
226 
227 };
228 
229 #endif
230