summaryrefslogtreecommitdiff
path: root/sources/protocols/VSProtocol.java
blob: b66f034c323a107450aa8e31fccd44e24a2df3e4 (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
121
122
123
124
125
package protocols;

import prefs.VSPrefs;
import events.VSEvent;
import core.*;

abstract public class VSProtocol extends VSPrefs implements VSEvent {
    protected VSPrefs prefs;
    private String protocolClassname;
    private boolean isServer;
    private boolean isClient;
    protected VSProcess process;
    private boolean currentContextIsServer;
    private boolean lamportIncreased;

    public VSProtocol() {
    }

    protected final void setProtocolClassname(String protocolClassname) {
        if (protocolClassname.startsWith("class "))
            protocolClassname = protocolClassname.substring(6);

        this.protocolClassname = protocolClassname;
    }

    public final String getProtocolClassname() {
        return protocolClassname;
    }

    public final String getProtocolName() {
        return RegisteredProtocols.getProtocolName(protocolClassname);
    }

    public final VSProcess getProcess() {
        return process;
    }

    protected void sendMessage(VSMessage message) {
        process.setLamportTime(process.getLamportTime()+1);
        message.setSendingProcess(process);
        process.sendMessage(message);
    }

    private final boolean isIncorrectProtocol(VSMessage message) {
        return !message.getProtocolClassname().equals(getProtocolClassname());
    }

    public final void onStart() {
        if (isClient) {
            onClientStart();
            currentContextIsServer = false;
        }
    }

    public final void onMessageRecv(VSMessage message) {
        if (isIncorrectProtocol(message))
            return;

        if (isServer) {
            currentContextIsServer = true;
            onServerRecv(message);
        }

        if (isClient) {
            currentContextIsServer = false;
            onClientRecv(message);
        }
    }

    public final void isServer(boolean isServer) {
        this.isServer = isServer;
    }

    public final void isClient(boolean isClient) {
        this.isClient = isClient;
    }

    public final void setVSPrefs(VSPrefs prefs) {
        this.prefs = prefs;
    }

    public final void setProcess(VSProcess process) {
        this.process = process;
    }

    public void reset() {
        if (isServer) {
            currentContextIsServer = true;
            onServerReset();
        }

        if (isClient) {
            currentContextIsServer = false;
            onClientReset();
        }
    }

    abstract protected void onClientStart();
    abstract protected void onClientReset();
    abstract protected void onClientRecv(VSMessage message);

    abstract protected void onServerReset();
    abstract protected void onServerRecv(VSMessage message);

    protected void logg(String message) {
        process.logg(toString() + "; " + message);
    }

    public boolean equals(VSProtocol protocol) {
        return protocol.getID() == getID();
    }

    public String toString() {
        String type = new String();

        if (currentContextIsServer)
            type += prefs.getString("lang.server");

        else
            type += prefs.getString("lang.client");

        return prefs.getString("lang.protocol") + ": "
               + RegisteredProtocols.getProtocolName(getProtocolClassname()) + " " + type;// + "; ID: " + getID();
    }
}