File size: 7,141 Bytes
7e59e4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package Common;

import java.lang.*;
import java.util.*;
import java.io.*;

/**The data members of this class are the fields which 
 * are to be passed between the client and the server.
 */
public class Message implements Serializable
{
	/**The ID of this message. */
	public int uniqueID;
	/**Running object ID. */
	public int runningObject;
	/**Type of message*/
	public int messageType;
	/**Number of Elements that the vector (of doubles) contains.*/
	public int noOfElements;
	/**The vector containing the doubles which would be 
	 * passed as parameters to the corresponding function.
	 */
	public Vector parameters;
	/**The full simulation specification.*/
	public String fullSimulationSpec;
	/**The partial simulation specification.*/
	public String partialSimulationSpec;
	/**Error message if there is an error*/
	public String errorMessage;
	

	/**No argument constructor. Sets up the default values for the
	 * different fields.
	 */
	public Message()
	{
		
        uniqueID = Constants.MESSAGE_START;
		runningObject = 0;
		messageType = 0;
		noOfElements = 0;
		parameters = new Vector();
		fullSimulationSpec = null;
		String partialSimulationSpec = null;
		String errorMessage = null;
				
	}
	

 	/**Writes the fields of this message to the socket through
 	 * a DataOutputStream. Locks the DataOutputStream before
 	 * writing so that some other thread does not corrupt the
 	 * stream.
 	 * @param out The DataOutputStream constructed from the 
 	 * socket output stream. 
 	 */
	public void writeDataObject(java.io.DataOutputStream out) throws IOException
 	{
 			
 		synchronized (out)
 		{
	 		out.writeInt(uniqueID);
			out.writeInt(runningObject);
			out.writeInt(messageType);
			
			out.writeInt(noOfElements);
			for (int i = 0; i < noOfElements; i++)
			{
				Double tmpDouble = (Double) this.parameters.get(i);
				out.writeDouble(tmpDouble.doubleValue());
			}
			
			out.writeInt(fullSimulationSpec.length());
			out.writeBytes(fullSimulationSpec);
			
			out.writeInt(partialSimulationSpec.length());
			out.writeBytes(partialSimulationSpec);
	 		
	 		
	 		out.writeInt(errorMessage.length());
	 		//showMessage("Writing Length: "  + errorMessage.length());
	 		out.writeBytes(errorMessage);
	 	}
 		 		
  	}
 	
 	
 	/**Reads the fields for this message from the socket through
 	 * a DataInputStream. Locks the DataInputStream before
 	 * writing so that some other thread does not corrupt the
 	 * stream.
 	 * @param in The DataInputStream constructed from the 
 	 * socket input stream. 
 	 */
	public void readDataObject(java.io.DataInputStream in) throws IOException, ClassNotFoundException
 	{
 		
 		synchronized (in)
 		{
	 		this.uniqueID = in.readInt();
			this.runningObject = in.readInt();
			this.messageType = in.readInt();
			
			this.noOfElements = in.readInt();
			this.parameters = new Vector(noOfElements);
			for (int i = 0; i < noOfElements; i++)
			{
				Double tmpDouble = new Double(in.readDouble());
				this.parameters.add(tmpDouble);
			}
				
			int tmpSize;
			byte[] byteString = null;
					
			tmpSize = in.readInt();
			byteString = new byte[tmpSize];
	 		for (int i = 0; i < tmpSize; i++)
	 		{
	 			byteString[i] = in.readByte();
	 		}
	 		this.fullSimulationSpec = new String(byteString);
	 		
	 		
	 		tmpSize = in.readInt();
			byteString = new byte[tmpSize];
	 		for (int i = 0; i < tmpSize; i++)
	 		{
	 			byteString[i] = in.readByte();
	 		}
	 		this.partialSimulationSpec = new String(byteString);
	 				
	 		
	 		
	 		tmpSize = in.readInt();
			byteString = new byte[tmpSize];
	 		for (int i = 0; i < tmpSize; i++)
	 		{
	 			byteString[i] = in.readByte();
	 		}
	 		this.errorMessage = new String(byteString);
	 	}
		
 	}
 		
 
	/**Makes a string containing the values of all the fields 
	 * of this message. 
	 * @return The string containing the values of all the fields
	 * of this message.
	 */
	public String printMessageDetails()
	{
		StringBuffer temp = new StringBuffer();
		temp.append("\nThe message fields are as follows: ");
		temp.append("\nUnique ID: " + this.uniqueID);
		temp.append("\nRunning Object: " + this.runningObject);
		temp.append("\nMessage Type: " + this.messageType);
		temp.append("\nNumber Of Elements in Vector: " + this.noOfElements);
		temp.append("\nVector Elements are: ");
		for (int i = 0; i < this.parameters.size(); i++)
		{
			Double tmpDouble = (Double) this.parameters.get(i);
			temp.append("\n\tParameter " + i + ": " + tmpDouble.toString());
		}
		
		temp.append("\nFull Simulation Specification: " + this.fullSimulationSpec);
		temp.append("\nPartial Simulation Specification: " + this.partialSimulationSpec);
		temp.append("\nError Message: " + this.errorMessage);
	
		return temp.toString();
	}
	
	
	/**Reads the values from the keyboard 
	 * and assigns them to the corresponding 
	 * fields of this message.
	 */
	public void readMessageFromKeyboard()
	{
		String line = null;
      	
		showMessage("\nEnter the message fields: ");
		System.out.println("Unique ID: ");
		line = 	getLine();
		try
		{
			this.uniqueID = Integer.parseInt(line);
		}
		catch(NumberFormatException nfe)
		{
			showMessage("Unique ID not proper Integer");
			uniqueID = 0;
		}
		
		System.out.println("Running Object: ");
		line = 	getLine();
		try
		{
			this.runningObject = Integer.parseInt(line);
		}
		catch(NumberFormatException nfe)
		{
			showMessage("Running Object not proper Integer");
			this.runningObject = 0;
		}
		
		System.out.println("Message Type: ");
		line = 	getLine();
		try
		{
			this.messageType = Integer.parseInt(line);
		}
		catch(NumberFormatException nfe)
		{
			showMessage("Message Type not proper Integer");
			this.messageType = 0;
		}
		
				
		System.out.println("Enter Vector Elements (Doubles): ");
		String choice = "Y";
		int i = 1;
		while (choice.trim().equals("y") || choice.trim().equals("Y"))
		{
			double tmpDouble = 0.0;
			System.out.println("Enter Vector Element " + i + ": ");
			line = 	getLine();
			try
			{
				tmpDouble = Double.parseDouble(line);
			}
			catch(NumberFormatException nfe)
			{
				showMessage("Running Object not proper Double");
			}
			
			this.parameters.add(new Double(tmpDouble));
			System.out.println("\tAdd Another (Y/N): " );
			choice = getLine();
			i++;
		}
		this.noOfElements = this.parameters.size();
		
		System.out.println("Full Simulation Specification : ");
		this.fullSimulationSpec = getLine();
		
		System.out.println("Partial Simulation Specification : ");
		this.partialSimulationSpec = getLine();
		
		System.out.println("Error Message: ");
		this.errorMessage  = getLine();
	}

		
	
	/**A small function to display messages.
	 * @param msg The message string to show.
	 */
	private void showMessage(String msg)
	{
		System.out.println("Message: " + msg);
	}
	
	/**Gets a line from the keyboard.
	 * @return The line input by the user from the keyboard
	 */
	private String getLine()
	{
		BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
		String line = null;
		try
		{
			line = keyboard.readLine();
		}
		catch(IOException ioe)
		{
			showMessage("Error reading from keyboard.");
		}
		return line;
	}
}