summaryrefslogtreecommitdiff
path: root/sources/core/VSTask.java
blob: 43d9cb5d3ee036d1cb808811e880a8b38da1646e (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
 * VS is (c) 2008 by Paul C. Buetow
 * vs@dev.buetow.org
 */
package core;

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

// TODO: Auto-generated Javadoc
/**
 * The Class VSTask.
 */
public class VSTask implements Comparable {
    
    /** The Constant LOCAL. */
    public final static boolean LOCAL = true;
    
    /** The Constant GLOBAL. */
    public final static boolean GLOBAL = false;
    
    /** The task time. */
    private long taskTime;
    
    /** The event. */
    private VSEvent event;
    
    /** The process. */
    private VSProcess process;
    
    /** The prefs. */
    private VSPrefs prefs;
    
    /** The is programmed. */
    private boolean isProgrammed;
    
    /** The is global timed. */
    private boolean isGlobalTimed;
    
    /** The task counter. */
    private static int taskCounter;
    
    /** The task num. */
    private int taskNum;

    /**
     * Instantiates a new vS task.
     * 
     * @param taskTime the task time
     * @param process the process
     * @param event the event
     * @param isLocal the is local
     */
    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;
        this.taskNum = ++taskCounter;
    }

    /**
     * Gets the task num.
     * 
     * @return the task num
     */
    public int getTaskNum() {
        return taskNum;
    }

    /**
     * Checks if is programmed.
     * 
     * @param isProgrammed the is programmed
     */
    public void isProgrammed(boolean isProgrammed) {
        this.isProgrammed = isProgrammed;
    }

    /**
     * Checks if is programmed.
     * 
     * @return true, if is programmed
     */
    public boolean isProgrammed() {
        return isProgrammed;
    }

    /**
     * Checks if is message receive event.
     * 
     * @return true, if is message receive event
     */
    public boolean isMessageReceiveEvent() {
        return event instanceof MessageReceiveEvent;
    }

    /**
     * Checks if is process recover event.
     * 
     * @return true, if is process recover event
     */
    public boolean isProcessRecoverEvent() {
        return event instanceof ProcessRecoverEvent;
    }

    /**
     * Checks if is protocol.
     * 
     * @param protocol the protocol
     * 
     * @return true, if is protocol
     */
    public boolean isProtocol(VSProtocol protocol) {
        if (event instanceof VSProtocol)
            return ((VSProtocol) event).equals(protocol);

        return false;
    }

    /**
     * Time over.
     * 
     * @return true, if successful
     */
    public boolean timeOver() {
        if (isGlobalTimed)
            return taskTime < process.getGlobalTime();

        return taskTime < process.getTime();
    }

    /**
     * Equals.
     * 
     * @param task the task
     * 
     * @return true, if successful
     */
    public boolean equals(VSTask task) {
        return taskNum == task.getTaskNum();
        /*
        return event.equals(task.getEvent())
               && taskTime == task.getTaskTime()
               && isGlobalTimed == task.isGlobalTimed()
               && isProgrammed == task.isProgrammed;
        	   */
    }

    /**
     * Checks if is process.
     * 
     * @param process the process
     * 
     * @return true, if is process
     */
    public boolean isProcess(VSProcess process) {
        return this.process.equals(process);
    }

    /**
     * Checks if is global timed.
     * 
     * @return true, if is global timed
     */
    public boolean isGlobalTimed() {
        return isGlobalTimed;
    }

    /**
     * Gets the process.
     * 
     * @return the process
     */
    public VSProcess getProcess() {
        return process;
    }

    /**
     * Run.
     */
    public void run() {
        if (event.getProcess() == null)
            event.init(process);
        event.onStart();
    }

    /**
     * Gets the task time.
     * 
     * @return the task time
     */
    public long getTaskTime() {
        return taskTime;
    }

    /**
     * Gets the event.
     * 
     * @return the event
     */
    public VSEvent getEvent() {
        return event;
    }

    /**
     * Logg.
     * 
     * @param message the message
     */
    private void logg(String message) {
        process.logg(message);
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    public String toString() {
        StringBuffer buffer = new StringBuffer();
        buffer.append(prefs.getString("lang.task"));
        buffer.append(" ");
        buffer.append(getTaskTime());
        buffer.append(event.toString());
        buffer.append("; PID: ");
        buffer.append(process.getProcessID());
        return buffer.toString();
    }

    /* (non-Javadoc)
     * @see java.lang.Comparable#compareTo(java.lang.Object)
     */
    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;

            /* If it's a ProtocolRecover, it should get handled very first */
            boolean a = event instanceof ProcessRecoverEvent;
            boolean b = task.getEvent() instanceof ProcessRecoverEvent;

            if (a && b)
                return 0;

            if (a)
                return -1;

            if (b)
                return 1;


            /* If it's a ProtocolEvent, it should get handled first */
            a = event instanceof ProtocolEvent;
            b = task.getEvent() instanceof ProtocolEvent;

            if (a && b)
                return 0;

            if (a)
                return -1;

            if (b)
                return 1;

        }

        return 0;
    }
}