summaryrefslogtreecommitdiff
path: root/src/main/java/events/VSAbstractEvent.java
blob: 1bbfc051a3032419c946abc3a6c12de686fef23b (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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package events;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import core.VSAbstractProcess;
import core.VSInternalProcess;
import exceptions.VSEventNotCopyableException;
import prefs.VSPrefs;
import prefs.VSSerializablePrefs;
import serialize.VSSerialize;

/**
 * The class VSAbstractEvent. This abstract class defines the basic framework
 * of each event. an event is used to fullfill a specific task. An event object
 * will get stored in a VSTask object.
 *
 * @author Paul C. Buetow
 */
abstract public class VSAbstractEvent extends VSSerializablePrefs {
    /** Event priority constants for task ordering */
    public static final int PRIORITY_HIGHEST = -3;  // Process recover events
    public static final int PRIORITY_HIGH = -2;     // Process crash events  
    public static final int PRIORITY_MEDIUM = -1;   // Protocol events
    public static final int PRIORITY_NORMAL = 0;    // All other events
    
    /** Class name prefix used by Java reflection */
    private static final String CLASS_PREFIX = "class ";
    private static final int CLASS_PREFIX_LENGTH = 6;
    
    /** The prefs. */
    public VSPrefs prefs;

    /** The process. */
    public VSAbstractProcess process;

    /** The event shortname. */
    private String eventShortname;

    /** The event classname. */
    private String eventClassname;

    /**
     * Checks if this event is an internal event.
     * Internal events are system events that don't directly correspond to user actions.
     * 
     * @return true if this is an internal event, false otherwise
     */
    public boolean isInternalEvent() {
        return false;
    }
    
    /**
     * Checks if this event can be serialized for saving/loading simulations.
     * Most events are serializable, but some runtime-only events may not be.
     * 
     * @return true if this event can be serialized, false otherwise
     */
    public boolean isSerializable() {
        return true;
    }
    
    /**
     * Checks if this event represents receiving a message.
     * Message receive events are triggered when a process receives a message.
     * 
     * @return true if this is a message receive event, false otherwise
     */
    public boolean isMessageReceiveEvent() {
        return false;
    }
    
    /**
     * Checks if this event represents a process recovery.
     * Process recover events restore a crashed process to operational state.
     * 
     * @return true if this is a process recover event, false otherwise
     */
    public boolean isProcessRecoverEvent() {
        return false;
    }
    
    /**
     * Checks if this event represents a process crash.
     * Process crash events simulate process failures in the distributed system.
     * 
     * @return true if this is a process crash event, false otherwise
     */
    public boolean isProcessCrashEvent() {
        return false;
    }
    
    /**
     * Checks if this event is a protocol-related event.
     * Protocol events manage protocol activation/deactivation.
     * 
     * @return true if this is a protocol event, false otherwise
     */
    public boolean isProtocolEvent() {
        return false;
    }
    
    /**
     * Determines if executing this event should increase the process's timestamps.
     * Most events increase timestamps, but some internal events may not.
     * This affects both Lamport and vector clocks based on preferences.
     * 
     * @return true if timestamps should be increased when this event executes
     */
    public boolean shouldIncreaseTimestamps() {
        return true;
    }
    
    /**
     * Get the priority of this event for ordering in VSTask comparisons.
     * Lower values have higher priority.
     * 
     * @return the event priority
     */
    public int getEventPriority() {
        return PRIORITY_NORMAL;
    }
    
    /**
     * Check if this event is copyable.
     * 
     * @return true if this event can be copied
     */
    public boolean isCopyable() {
        return this instanceof VSCopyableEvent;
    }

    /**
     * Creates a copy of the event and using a new process.
     *
     * @param theProcess The new process
     * @return The copy
     */
    final public VSAbstractEvent getCopy(VSInternalProcess theProcess)
    throws VSEventNotCopyableException {

        if (theProcess == null)
            theProcess = (VSInternalProcess) process;

        if (!isCopyable())
            throw new VSEventNotCopyableException(
                eventShortname + " (" + eventClassname + ")");

        VSAbstractEvent copy =
            VSRegisteredEvents.createEventInstanceByClassname(
                eventClassname, theProcess);

        ((VSCopyableEvent) this).initCopy(copy);
        copy.setShortname(eventShortname);

        return copy;
    }

    /**
     * Creates a copy of the event.
     *
     * @return The copy
     */
    final public VSAbstractEvent getCopy() throws VSEventNotCopyableException {
        return getCopy(null);
    }

    /**
     * Inits the event.
     *
     * @param process the process
     */
    public void init(VSInternalProcess process) {
        if (this.process == null) {
            this.process = process;
            this.prefs = process.getPrefs();
            init();
        }
    }

    /**
     * Inits the event without setting the processes and prefs variables
     * of the object.
     */
    public void init() {
        onInit();
    }

    /**
     * Sets the classname.
     *
     * @param eventClassname the new classname
     */
    public final void setClassname(String eventClassname) {
        if (eventClassname.startsWith(CLASS_PREFIX))
            eventClassname = eventClassname.substring(CLASS_PREFIX_LENGTH);

        this.eventClassname = eventClassname;
    }

    /**
     * Gets the classname.
     *
     * @return the classname
     */
    public String getClassname() {
        return eventClassname;
    }

    /**
     * Gets the name.
     *
     * @return the name
     */
    public String getName() {
        return VSRegisteredEvents.getNameByClassname(eventClassname);
    }

    /**
     * Sets the shortname.
     *
     * @param eventShortname the new shortname
     */
    public void setShortname(String eventShortname) {
        this.eventShortname = eventShortname;
    }

    /**
     * Gets the shortname.
     *
     * @return the shortname
     */
    public String getShortname() {
        if (eventShortname == null)
            return VSRegisteredEvents.getShortnameByClassname(eventClassname);

        return eventShortname;
    }

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

    /**
     * Logg a specific message.
     *
     * @param message the loging message
     */
    public void log(String message) {
        process.log(/*toString() + "; " + */message);
    }

    /**
     * Checks if the event equals to another event..
     *
     * @param event the event to compare against.
     *
     * @return true, if the events are the same (have the same event id)
     */
    public boolean equals(VSAbstractEvent event) {
        return super.getID() == event.getID();
    }

    /**
     * Every event has its own initialize method.
     */
    abstract public void onInit();

    /**
     * Every event can get started. This method get's executed if the event
     * takes place.
     */
    abstract public void onStart();

    /**
     * Every event has to be able to set its own shortname
     *
     * @param shortName The saved short name. May be overwritten due wrong lang
     *
     * @return The event's shortname
     */
    abstract protected String createShortname(String savedShortname);

    /* (non-Javadoc)
     * @see serialize.VSSerializable#serialize(serialize.VSSerialize,
     *	java.io.ObjectOutputStream)
     */
    public synchronized void serialize(VSSerialize serialize,
                                       ObjectOutputStream objectOutputStream)
    throws IOException {
        super.serialize(serialize, objectOutputStream);

        if (VSSerialize.DEBUG)
            System.out.println("Serializing: VSAbstractEvent; id="+getID());

        /** For later backwards compatibility, to add more stuff */
        objectOutputStream.writeObject(Boolean.valueOf(false));

        objectOutputStream.writeObject(Integer.valueOf(super.getID()));
        objectOutputStream.writeObject(eventShortname);
        objectOutputStream.writeObject(eventClassname);

        /** For later backwards compatibility, to add more stuff */
        objectOutputStream.writeObject(Boolean.valueOf(false));
    }

    /* (non-Javadoc)
     * @see serialize.VSSerializable#deserialize(serialize.VSSerialize,
     *	java.io.ObjectInputStream)
     */
    public synchronized void deserialize(VSSerialize serialize,
                                         ObjectInputStream objectInputStream)
    throws IOException, ClassNotFoundException {
        super.deserialize(serialize, objectInputStream);

        if (VSSerialize.DEBUG)
            System.out.print("Deserializing: VSAbstractEvent ");

        /** For later backwards compatibility, to add more stuff */
        objectInputStream.readObject();

        int id = ((Integer) objectInputStream.readObject()).intValue();
        String savedEventShortname = (String) objectInputStream.readObject();
        this.eventClassname = (String) objectInputStream.readObject();
        
        // Always use current localization strings
        this.eventShortname = VSRegisteredEvents.getShortnameByClassname(eventClassname);
        if (this.eventShortname == null) {
            this.eventShortname = createShortname(savedEventShortname);
        }

        if (VSSerialize.DEBUG) {
            System.out.println("eventClassname: " + eventClassname);
            System.out.println("eventShortname: " + eventShortname);
        }

        serialize.setObject(id, "event", this);

        /** For later backwards compatibility, to add more stuff */
        objectInputStream.readObject();
    }
}