summaryrefslogtreecommitdiff
path: root/sources/core/VSMessage.java
blob: a16d35bf726f3a3c43eef617fb3ec80f47e392c5 (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
/*
 * VS is (c) 2008 by Paul C. Buetow
 * vs@dev.buetow.org
 */
package core;

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

// TODO: Auto-generated Javadoc
/**
 * This class represents a message which is send from one process to another process in the simulation.
 */
public class VSMessage extends VSPrefs {
    
    /** Each message belongs to a specific protocol. This variable defined the class name of the protocol being used. */
    private String protocolClassname;
    
    /** The default application preferences. */
    private VSPrefs prefs;
    
    /** A reference to the process who sent this message. */
    private VSProcess sendingProcess;
    
    /** The vector time of the sending process after sending. The receiver process will use this vector time in order to update the local vector time. */
    private VSVectorTime vectorTime;
    
    /** The lamport time of the sending process after sending. The receiver process will use this lamport time in order to update the local vector time. */
    private long lamportTime;
    
    /** Each message has its own unique ID. The ID will show up in the logging window of the simulator as well. */
    private long messageID;
    
    /** This counter is used in order to generate unique message ID's. */
    private static long messageCounter;

    /**
     * The constructor of the message.
     * 
     * @param protocolClassname The classname of the protocol this message
     * belongs to.
     */
    public VSMessage(String protocolClassname) {
        this.protocolClassname = protocolClassname;
        this.messageID = ++messageCounter;
    }

    /**
     * Initialized the message.
     * 
     * @param process The sending process of this message.
     */
    public void init(VSProcess process) {
        this.sendingProcess = process;
        this.prefs = process.getPrefs();
        lamportTime = sendingProcess.getLamportTime();
        vectorTime = sendingProcess.getVectorTime().getCopy();
    }

    /**
     * Getter method.
     * 
     * @return The protocol name of the message.
     */
    public String getName() {
        return VSRegisteredEvents.getName(getProtocolClassname());
    }

    /**
     * Getter method.
     * 
     * @return The protocol classname of the message.
     */
    public String getProtocolClassname() {
        return protocolClassname;
    }

    /**
     * Getter method.
     * 
     * @return The ID of the message.
     */
    public long getMessageID() {
        return messageID;
    }

    /**
     * Getter method.
     * 
     * @return The process which sent this message.
     */
    public VSProcess getSendingProcess() {
        return sendingProcess;
    }

    /**
     * Getter method.
     * 
     * @return The lamport time of the sending process.
     */
    public long getLamportTime() {
        return lamportTime;
    }

    /**
     * Getter method.
     * 
     * @return The vector time of the sending process.
     */
    public VSVectorTime getVectorTime() {
        return vectorTime;
    }

    /**
     * String representation of the message object.
     * 
     * @return String representation of the message object.
     */
    public String toString() {
        StringBuffer buffer = new StringBuffer();

        buffer.append("ID: ");
        buffer.append(messageID);
        buffer.append("; ");
        buffer.append(prefs.getString("lang.protocol"));
        buffer.append(": ");
        buffer.append(VSRegisteredEvents.getShortname(getProtocolClassname()));

        return buffer.toString();
    }

    /**
     * Extended string representation of the message object.
     * 
     * @return Extended string representation of the message object.
     */
    public String toStringFull() {
        return toString() + "; " + super.toString();
    }

    /**
     * Compares two messages.
     * 
     * @param message The message to compare with.
     * 
     * @return true, if the messages have the same ID. Otherwise false.
     */
    public boolean equals(VSMessage message) {
        return messageID == message.getMessageID();
    }

    /**
     * For logging in the simulator's logging window!.
     * 
     * @param message The message to logg.
     */
    public void logg(String message) { }
}