m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/UDUP.java
blob: e4a7962cce5bc56f34880e3bf613a51d8ee09c34 (plain)
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
package pl.edu.mimuw.cloudatlas.agent.modules;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.google.common.primitives.Bytes;
import pl.edu.mimuw.cloudatlas.agent.EventBus;
import pl.edu.mimuw.cloudatlas.agent.messages.UDUPMessage;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.HashMap;
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
 */

public class UDUP extends Module implements Runnable {
    public class InvalidConversation extends Exception {
        public InvalidConversation(String message) { super(message); }
    }

    public class InvalidContact extends Exception {
        public InvalidContact(String message) { super(message); }
    }

    private UDUPClient client;
    private UDUPServer server;
    private HashMap<String, UDUPMessage> currentConversations; // TODO find blocking one
    private final AtomicBoolean running;

    public UDUP(ModuleType moduleType,
                InetAddress serverAddr,
                int serverPort,
                int retryTimeout,
                int retriesCount,
                int bufferSize) {
        super(moduleType);
        this.currentConversations = new HashMap<>();
        this.running = new AtomicBoolean(true);
        try {
            this.client = new UDUPClient(this, serverPort, retryTimeout, retriesCount, bufferSize);
            this.server = new UDUPServer(this, serverAddr, serverPort, bufferSize);
        } catch (SocketException | UnknownHostException e) {
            e.printStackTrace();
            this.client.close();
            this.server.close();
            this.running.getAndSet(false);
        }
    }

    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());
        this.client.sendMessage(event);
    }

    // also used for updating
    public void addConversation(UDUPMessage msg) {
        this.currentConversations.put(msg.getConversationId(), msg);
    }

    public UDUPMessage fetchConversation(String conversationId) throws InvalidConversation {
        UDUPMessage ret = this.currentConversations.get(conversationId);
        if (ret == null) {
            throw new InvalidConversation("Conversation does not exist");
        } else {
            return ret;
        }
    }

    // TODO add conversation removal
    public void removeConversation(String conversationId) {
        this.currentConversations.remove(conversationId);
    }

    public UDUPMessage deserialize(ByteArrayInputStream in) {
        Kryo kryo = new Kryo();
        Input kryoInput = new Input(in);
        UDUPMessage msg = kryo.readObject(kryoInput, UDUPMessage.class);
        return msg;
    }

    public void serialize(ByteArrayOutputStream out, UDUPMessage msg) {
        Kryo kryo = new Kryo();
        Output kryoOut = new Output(out);
        kryo.writeObject(kryoOut, msg);
        kryoOut.flush();
    }

}