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

import pl.edu.mimuw.cloudatlas.agent.Executor;
import pl.edu.mimuw.cloudatlas.agent.messages.*;

/*
 * A Module is a (potentially stateful) event handler.
 */
public abstract class Module {
    public class InvalidMessageType extends Exception {
        public InvalidMessageType(String message) {
            super(message);
        }
    }

    protected ModuleType moduleType;
    protected Executor executor;

    public Module(ModuleType moduleType) {
        this.moduleType = moduleType;
    }

    public void handle(AgentMessage event) throws InterruptedException, InvalidMessageType {
        event.callMe(this);
    }

    public void handleTyped(TimerSchedulerMessage message) throws InterruptedException, InvalidMessageType {
        throw new InvalidMessageType("Got a TimerSchedulerMessage in module " + moduleType.toString());
    }

    public void handleTyped(GossipGirlMessage message) throws InterruptedException, InvalidMessageType {
        throw new InvalidMessageType("Got a GossipGirlMessage in module " + moduleType.toString());
    }

    public void handleTyped(QurnikMessage message) throws InterruptedException, InvalidMessageType {
        throw new InvalidMessageType("Got a QurnikMessage in module " + moduleType.toString());
    }

    public void handleTyped(RemikMessage message) throws InterruptedException, InvalidMessageType {
        throw new InvalidMessageType("Got a RemikMessage in module " + moduleType.toString());
    }

    public void handleTyped(StanikMessage message) throws InterruptedException, InvalidMessageType {
        throw new InvalidMessageType("Got a StanikMessage in module " + moduleType.toString());
    }

    public void handleTyped(ResponseMessage message) throws InterruptedException, InvalidMessageType {
        throw new InvalidMessageType("Got a ResponseMessage in module " + moduleType.toString());
    }

    public void handleTyped(UDUPMessage message) throws InterruptedException, InvalidMessageType {
        throw new InvalidMessageType("Got a ResponseMessage in module " + moduleType.toString());
    }

    public void setExecutor(Executor executor) {
        this.executor = executor;
    }

    public void sendMessage(AgentMessage event) throws InterruptedException {
        this.executor.passMessage(event);
    }

    @Override
    public String toString() {
        return moduleType.toString();
    }
}