Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
106 changes: 0 additions & 106 deletions pom.xml-unused

This file was deleted.

13 changes: 13 additions & 0 deletions src/main/java/dev/oxoo2a/sim4da/Clock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package dev.oxoo2a.sim4da;

public interface Clock {
int getTimeStamp();
void increment();
public String printTimeStamp();
public void update(int senderTime, Object ... args);
String getTimeVector();
}




47 changes: 47 additions & 0 deletions src/main/java/dev/oxoo2a/sim4da/ControlMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package dev.oxoo2a.sim4da;

import java.util.ArrayList;
import java.util.List;

public class ControlMessage {

private int round;
protected int received;
protected int sent;
protected boolean isActive;
protected int id;

protected ControlMessageType type;

public ControlMessage(int round, ControlMessageType type, int id)
{
this.round = round;
this.type = type;
this.id = id;
}

public ControlMessage(ControlMessageType type, int id, boolean status, int r, int s, int round)
{
this.round = round;
this.type = type;
this.id = id;
this.isActive = status;
this.received = r;
this.sent = s;
}

public boolean isActive() {
return isActive;
}

public int getId() {
return id;
}

public int getRound() { return round;}

public int getReceived() { return received;}

public int getSent() { return sent;}

}
51 changes: 51 additions & 0 deletions src/main/java/dev/oxoo2a/sim4da/ControlMessageQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package dev.oxoo2a.sim4da;


import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Semaphore;


public class ControlMessageQueue
{

public ControlMessageQueue()
{
queue = new ArrayList<ControlMessage>();

}
public synchronized void put ( ControlMessage r )
{
synchronized (queue) {
queue.add(r);
}
}
public ControlMessage get(int id) {
while(true)
{
try {
synchronized (queue) {
ControlMessage result = null;
for (ControlMessage cm : queue) {
if (cm.getId() == id) {
result = cm;
break;
}
}
if (result != null) {
queue.remove(result);
return result;
} else {
return null;
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}

}

protected static ArrayList<ControlMessage> queue;
}
3 changes: 3 additions & 0 deletions src/main/java/dev/oxoo2a/sim4da/ControlMessageType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package dev.oxoo2a.sim4da;

public enum ControlMessageType {REQUEST, RESPONSE}
141 changes: 141 additions & 0 deletions src/main/java/dev/oxoo2a/sim4da/DoubleCountTerminator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package dev.oxoo2a.sim4da;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Semaphore;

import static dev.oxoo2a.sim4da.ControlMessageType.REQUEST;
import static java.lang.Thread.sleep;

public class DoubleCountTerminator{

public static int broadcast_run = 0;
private final int n_Nodes;
public Thread thread;
private int numberSent;
static int counter;
static int numberReceived;
private final Semaphore await_message;
protected ArrayList<ControlMessage> received_array;
private Node2Simulator simulator;
static boolean stop;

public DoubleCountTerminator(int numberOfNodes)
{
n_Nodes = numberOfNodes;
numberSent = 0;
received_array = new ArrayList<ControlMessage>();
await_message = new Semaphore(0);
thread = new Thread(this::main);
thread.setName("Observer");

}
public void setSimulator(Node2Simulator s ) {
this.simulator = s;
}

public void broadcastControlMessage() {
broadcast_run++;
for(int i=0;i<n_Nodes;i++)
{
ControlMessage cm = new ControlMessage(broadcast_run, REQUEST, i);
simulator.sendControlMessage(cm);
numberSent++;
}

}
synchronized void update(ControlMessage controlMessage)
{
received_array.add(controlMessage);
numberReceived++;
if(numberReceived==numberSent)
{
counter++;
await_message.release();
}
}
public void main()
{
while (broadcast_run<=2) {

if (stop) break;

if (counter <2 && broadcast_run<2)
{
broadcastControlMessage();

try {
await_message.acquire();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
simulator.emit("number sent %d", "doublecount", numberSent);
simulator.emit("received %d", "doublecount", numberReceived);
numberReceived= 0;
numberSent = 0;
}
else if (counter==2 && checkReceivedQueue(received_array)) {

simulator.updateStatus();
simulator.emit("TERMINATING DOUBLE COUNT", "doublecount","after 2 runs");
System.exit(0);
break;
}
else if (counter==2 && !checkReceivedQueue(received_array))
{
simulator.emit("DID 2 RUNS, DIDN'T TERMINATE", "doublecount");
break;
}

}
}
public void start () {

thread.start();
}

public void stop () {

stop = true;
await_message.release();

}
public void end() throws InterruptedException {
thread.join();
}

public synchronized static boolean checkReceivedQueue(ArrayList<ControlMessage> arr) {
int round1_received = 0;
int round2_received = 0;
int round1_sent = 0;
int round2_sent = 0;
boolean[] status = new boolean[arr.size()];

for(int i=0; i<arr.size(); i++)
{
if(arr.get(i).getRound() == 1)
{
round1_received += 1;
round1_sent += 1;

}
else
{
round2_received += 1;
round2_sent += 1;
status[i] = arr.get(i).isActive();
}
}

boolean allFalse = true;
for (boolean value : status) {
if (value) {
allFalse = false;
break;
}
}
return allFalse && (round1_received == round1_sent && round1_sent == round2_received && round2_received == round2_sent);
}

}
Loading