|
1 | 1 | # sim4da - Simulator for Distributed Algorithms |
2 | 2 |
|
3 | | -The simulator strives to abstract from most of the details in network programming and to ease the development of algorithm simulations. The simulator core is self-contained in a Java module `dev.oxoo2a.sim4da`. The distributed algorithm simulation is controlled by an instance of class `Simulator`: |
| 3 | +The simulator strives to abstract from most of the details in network programming and to ease the development of algorithm simulations. The simulator core is self-contained in a Java module `dev.oxoo2a.sim4da`. |
| 4 | + |
| 5 | +## Writing your own simulation |
| 6 | + |
| 7 | +The distributed algorithm simulation is controlled by an instance of class `Simulator`: |
4 | 8 |
|
5 | 9 | ```Java |
6 | 10 | Simulator s = new Simulator(n_nodes); |
@@ -31,3 +35,36 @@ Inside `main`, the following methods are available: |
31 | 35 | - `sendBroadcast(Message message)`: sending a `HashMap`-like message encoded in JSON to all nodes (except the sender itself) |
32 | 36 | - `stillSimulating()`: returns `true` while the duration for the simulation is not exceeded |
33 | 37 | - `receive()`: blocks until a message is received by the node; an object of type `Network.Message` is returned, which stores `sender_id`, `receiver_id`, send mode (unicast or broadcast), and the `payload` as a string. If a `Message` is expected, this payload must be deserialized from JSON back into an object of type `Message` by calling `Message.fromJson(payload)`. |
| 38 | + |
| 39 | +An example implementation for a distributed algorithm simulation is available as a test case (`BroadcastNode.java` and `SimulatorTest.java`). |
| 40 | + |
| 41 | +## Building the sim4da jar |
| 42 | + |
| 43 | +`Gradle` is used as the build tool. `gradle build` and `gradle shadowJar` within the root directory of the repository generate the required jar files. The fat jar contains the transitive dependencies to `gson`, `junit` and `log4j2`. |
| 44 | + |
| 45 | +## Logging |
| 46 | + |
| 47 | +The simulator core can create trace files of every simulation thanks to the logging framework `log4j2`. A default configuration file is part of the simulator resources. The default logging configuration is similar to: |
| 48 | + |
| 49 | +```xml |
| 50 | +<?xml version="1.0" encoding="UTF-8"?> |
| 51 | +<Configuration status="WARN"> |
| 52 | + <Appenders> |
| 53 | + <Console name="console" target="SYSTEM_OUT"> |
| 54 | + <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" /> |
| 55 | + </Console> |
| 56 | + |
| 57 | + <File name="logfile" fileName="sim4da-app.log" append="false"> |
| 58 | + <PatternLayout pattern="%msg%n"/> |
| 59 | + </File> |
| 60 | + </Appenders> |
| 61 | + <Loggers> |
| 62 | + <Logger name="sim4da" level="trace" additivity="false"> |
| 63 | + <AppenderRef ref="logfile"/> |
| 64 | + </Logger> |
| 65 | + <Root level="warn"> |
| 66 | + <AppenderRef ref="console"/> |
| 67 | + </Root> |
| 68 | + </Loggers> |
| 69 | +</Configuration> |
| 70 | +``` |
0 commit comments