diff options
author | Marcin Chrzanowski <marcin.j.chrzanowski@gmail.com> | 2020-01-06 20:34:06 +0100 |
---|---|---|
committer | Marcin Chrzanowski <marcin.j.chrzanowski@gmail.com> | 2020-01-06 20:34:06 +0100 |
commit | 7b6d9a490cd07a6ffaf4b69df501a72c538621de (patch) | |
tree | b9a636da41f8e23d84a9ef93961758ea45268f3b /src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/UDUP.java | |
parent | 4aaa6218b853873c632aba0ed8696f29640041d1 (diff) | |
parent | 2feff1aa41c41008fcda2dd60c718cf09deb3fa1 (diff) |
Merge branch 'master' into gossip-girl
Diffstat (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/UDUP.java')
-rw-r--r-- | src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/UDUP.java | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/UDUP.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/UDUP.java new file mode 100644 index 0000000..e616c93 --- /dev/null +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/UDUP.java @@ -0,0 +1,73 @@ +package pl.edu.mimuw.cloudatlas.agent.modules; + +import pl.edu.mimuw.cloudatlas.agent.messages.UDUPMessage; + +import java.io.IOException; +import java.net.InetAddress; +import java.net.SocketException; +import java.util.concurrent.atomic.AtomicBoolean; + +/** + * Communication over UDP + * + * Client-server exchange pattern: + * server: init message + * client: ack message + * + * retry count + * retry timeout after which retry happens + ** + * udp sends initiator module success/failure information + * + * we have udps on different addresses with the same ports + * due to ValueContact design + */ + +// TODO set server port in global config - must be the same everywhere +// TODO same with buffer size + +public class UDUP extends Module implements Runnable { + private UDUPClient client; + private UDUPServer server; + private final AtomicBoolean running; + + public UDUP(InetAddress serverAddr, + int serverPort, + int timeout, + int bufferSize) { + super(ModuleType.UDP); + this.running = new AtomicBoolean(false); + try { + this.client = new UDUPClient(this, serverPort, bufferSize); + this.server = new UDUPServer(this, serverAddr, serverPort, bufferSize); + this.running.getAndSet(true); + } catch (SocketException e) { + e.printStackTrace(); + this.client.close(); + this.server.close(); + } + } + + public void run() { + System.out.println("UDP server running"); + while(this.running.get()) { + try { + this.server.acceptMessage(); + } catch (IOException | InterruptedException e) { + e.printStackTrace(); + this.running.getAndSet(false); + this.server.close(); + } + } + } + + public void handleTyped(UDUPMessage event) throws InterruptedException { + System.out.println("UDP sending message " + event.getContent().getMessageId()); + try { + this.client.sendMessage(event); + } catch (IOException e) { + System.out.println("UDP send message failed"); + e.printStackTrace(); + } + } +} |