summaryrefslogtreecommitdiff
path: root/sources/core/VSTask.java
blob: a476e35ee3b4ebefaf96c408fe217664e9825f51 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package core;

import events.*;
import events.implementations.*;
import prefs.VSPrefs;
import protocols.VSProtocol;
import simulator.*;

public class VSTask implements Comparable {
    public final static boolean LOCAL = true;
    public final static boolean GLOBAL = false;
    private long taskTime;
    private VSEvent event;
    private VSProcess process;
    private VSPrefs prefs;
    private boolean isProgrammed;
    private boolean isGlobalTimed;

    public VSTask(long taskTime, VSProcess process, VSEvent event, boolean isLocal) {
        this.process = process;
        this.taskTime = taskTime > 0 ? taskTime : 0;
        this.event = event;
        this.prefs = process.getPrefs();
        this.isGlobalTimed = !isLocal;
    }

    public void isProgrammed(boolean isProgrammed) {
        this.isProgrammed = isProgrammed;
    }

    public boolean isProgrammed() {
        return isProgrammed;
    }

    public boolean isMessage() {
        return event instanceof VSMessage;
    }

    public boolean isProcessRecoverEvent() {
        return event instanceof events.implementations.ProcessRecoverEvent;
    }

    public boolean isProtocol(VSProtocol protocol) {
        if (event instanceof VSProtocol)
            return ((VSProtocol) event).equals(protocol);

        return false;
    }

    public boolean equals(VSTask task) {
        return event.equals(task.getEvent())
               && taskTime == task.getTaskTime()
               && isGlobalTimed == task.isGlobalTimed()
               && isProgrammed == task.isProgrammed;
    }

    public boolean isProcess(VSProcess process) {
        return this.process.equals(process);
    }

    public boolean isGlobalTimed() {
        return isGlobalTimed;
    }

    public VSProcess getProcess() {
        return process;
    }

    public void run() {
        if (event instanceof VSMessage) {
            onMessageRecv();

        } else if (event instanceof VSProtocol) {
            /* Lamport time will get incremented by the VSProtocol class */
            onProtocolStart();

        } else if (event instanceof VSEvent) {
            onEventStart();

        } else {
            onDummy();
        }
    }

    private void onDummy() {
        logg(prefs.getString("lang.process.task"));
    }

    /**
     * If the process recv a message, check if the message's protocol is activated
     * by the process. If yes, run the protocol on the message! If not, just logg
     * that the process does not support this protocol! The process will ignore the
     * message!
     */
    private void onMessageRecv() {
        final VSMessage message = (VSMessage) event;
        final String eventName = message.getName();
        final String protocolClassname = message.getProtocolClassname();

        process.updateLamportTime(message.getLamportTime()+1);
        process.updateVectorTime(message.getVectorTime());

        Object protocolObj;

        if (process.objectExists(protocolClassname))
            protocolObj = process.getObject(protocolClassname);
        else
            protocolObj = null;

        StringBuffer buffer = new StringBuffer();
        buffer.append(prefs.getString("lang.message.recv"));
        buffer.append("; ");
        buffer.append(prefs.getString("lang.protocol"));
        buffer.append(": " );
        buffer.append(eventName);
        buffer.append("; ");
        buffer.append(prefs.getString("lang.message"));
        buffer.append(" ");
        buffer.append(message);;

        if (protocolObj == null) {
            logg(buffer.toString());

        } else {
            final VSProtocol protocol = (VSProtocol) protocolObj;
            logg(buffer.toString());
            protocol.onMessageRecv(message);
        }
    }

    private void onProtocolStart() {
        ((VSProtocol) event).onStart();
    }

    private void onEventStart() {
        event.onStart();
    }

    public long getTaskTime() {
        return taskTime;
    }

    public VSEvent getEvent() {
        return event;
    }

    private void logg(String message) {
        process.logg(message);
    }

    public String toString() {
        StringBuffer buffer = new StringBuffer();
        buffer.append(prefs.getString("lang.task"));
        buffer.append(" ");
        buffer.append(getTaskTime());

        if (event instanceof VSMessage) {
            buffer.append("; ");
            buffer.append(((VSMessage)event).toString());

        } else if (event instanceof VSProtocol) {
            buffer.append("; ");
            buffer.append(((VSProtocol)event).toString());
        }

        return buffer.toString();
    }

    public int compareTo(Object object) {
        if (object instanceof VSTask) {
            final VSTask task = (VSTask) object;

            if (taskTime < task.getTaskTime())
                return -1;

            else if (taskTime > task.getTaskTime())
                return 1;

			boolean a = event instanceof ProtocolEvent;
			boolean b = task.getEvent() instanceof ProtocolEvent;

			if (a && b)
				return 0;

			if (a)
				return -1;

			if (b)
				return 1;
        }

        return 0;
    }
}