-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabsnetworkinterface.h
More file actions
284 lines (242 loc) · 8.29 KB
/
absnetworkinterface.h
File metadata and controls
284 lines (242 loc) · 8.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
Copyright (c) 2013, <copyright holder> <email>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY <copyright holder> <email> ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <copyright holder> <email> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef ABSNETWORKINTERFACE_H_
#define ABSNETWORKINTERFACE_H_
#include <stdint.h>
#include <sys/types.h>
#include <unistd.h>
#include <exception>
#include <map>
#include <sstream>
#include <string>
#include <vector>
#include "callback.h"
using namespace std;
namespace sim_comm {
/* forward declarations */
class Message;
/**
* Generic excpetion generated by the network.
*/
class NetworkException : public exception {
public:
NetworkException(
const char *file,
const char *func,
int line,
const char *description) throw () {
ostringstream s;
s << "[" << getpid() << "] " << file << ":" << line << ":" << func
<< ": Exception `" << description << "'" << endl;
this->message = s.str();
}
~NetworkException() throw() {}
virtual const char* what() const throw() {
return message.c_str();
}
private:
string message;
};
/* for brevity */
#define NETWORK_EXCEPTION(DESC) \
throw NetworkException(__FILE__,__func__,__LINE__,(DESC))
class AbsNetworkInterface {
protected:
vector<string> myObjects;
bool registrationsAreFinalized;
CallBack<void,Message*,empty,empty,empty> *messageCallBack;
bool canFork,killOnTerm;
virtual void notifyFork() = 0;
public:
/**
* Constructs.
*/
AbsNetworkInterface(bool forkCapable=false);
/**
* Child classes should provide a copy constructor
* for creating a new context (from the existing context)
*/
AbsNetworkInterface(const AbsNetworkInterface &that);
/**
* Destroys.
*/
virtual ~AbsNetworkInterface();
/**
* Injects a message into the network.
*
* @param[in] message the Message instance
* @throw NetworkException if the send operation fails
*/
virtual void send(Message *message) =0;
/**
* Sends the message to every network endpoint.
*
* @param[in] message the Message instance
* @throw NetworkException if the send operation fails
*/
virtual uint64_t broadcast(Message *message) =0;
/**
* Returns the first available Message, or NULL.
*
* This method is for callers which actively pull Message instances from
* the network, as opposed to registering a callback function when a
* Message arrives.
*
* @throw NetworkException if the receive operation fails
* @return the Message instance, or NULL
*/
virtual Message* receive() =0;
/**
* Returns all available messages.
*
* This method is for callers which actively pull Message instances from
* the network, as opposed to registering a callback function when a
* Message arrives.
*
* @throw NetworkException if the receive operation fails
* @return all Message instances, or an empty vector
*/
virtual vector<Message*> receiveAll();
/**
* Registers a callback function to invoke when a Message is received.
*/
void setMessageCallBack(CallBack<void,Message*,empty,empty,empty> *messageCallBack) {
this->messageCallBack = messageCallBack;
}
/**
* Reduces min time operation.
*
* @throw NetworkException when the reduce operation fails
* @return the min time
*/
virtual uint64_t reduceMinTime(uint64_t myTime) =0;
/**
* Reduces min time operation with message-was-sent notification.
*
* @throw NetworkException when the reduce operation fails
* @return the min time
*/
virtual uint64_t reduceMinTimeAndSleep(uint64_t myTime, bool hadMessage) =0;
/**
* Aggregate reduce operation used for reduction of the
* next time step and parent process action. This operation
* is used by the optimistic algorithm.
*
* @throw NetworkException when the reduce operation fails
* @param timeAction an array of 2 elements, [0] is next time, [1] is action
* @return pointer to timeAction array with [0] the min next time, [1] the min action
*/
virtual uint64_t* reduceMinTimeAndAction(uint64_t *timeAction) =0;
/**
* Reduces total sent and received counts.
*
* @throw NetworkException when the reduce operation fails
*/
virtual uint64_t reduceTotalSendReceive(uint64_t sent, uint64_t received) =0;
/**
* Registers an object for Message delivery.
*
* @param[in] name TODO
* @param[in] object TODO
*/
virtual void registerObject(string name);
/**
* Indicates that communication object registrations have completed.
*
* This method is collective across all AbsNetworkInterface instances in
* order to efficiently exchange metadata.
*/
virtual void finalizeRegistrations();
/**
* Indicates that registrations have been completed.
*/
inline bool isAcceptingRegistrations() {
return !this->registrationsAreFinalized;
}
/**
* Barier function.
*/
virtual void barier() =0;
/**
* Sleep function.
*/
virtual bool sleep() =0;
/**
* Used for gathering the next times of the
* simulators.
*/
virtual uint64_t* getNextTimes(uint64_t nextTime,uint32_t &worldSize) =0;
/**
* Duplicates the current interfce, creates a new interface that
* has the same connections as the previous.
*/
virtual AbsNetworkInterface* duplicateInterface() =0;
/**
* Signals that we are finished.
*/
virtual void sendFinishedSignal();
/**
* Clean up function
*/
virtual void cleanup() = 0;
/**
* Send notification to other processes in the
* context, the failed signal.
*/
virtual void sendFailed() =0;
/**
* Send notification to other processes in the context
* a succeed signal.
*/
virtual void sendSuceed() =0;
/**
* Returns true if this instance of networkinterface
* works with forks.
*/
bool supportsFork(){ return this->canFork; }
/**
* Prepares the networkinterface for fork.
* Throws expcetion if interface does not support forking.
*/
void prepareFork();
/**
* Blocks the execution, used by sync algorithms
* to block execution until a signal is received
*/
virtual void block() =0;
/**
* Defines the behavior on term signal
* True - kill all simulators, the interface instance will terminate all others
* False - only the current instance is terminated.
*/
void setKillOnTerm(bool state=true);
/**
* Returns true if the interface is set to kill the co-simulation on term
* False if it only terminates the current process.
*/
bool doKillOnTerm() { return this->killOnTerm; }
};
} /* end namespace sim_comm */
#endif /* ABSNETWORKINTERFACE_H_ */