File size: 2,755 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
// Copyright (C) 2010 - 2013 by Pedro Mendes, Virginia Tech Intellectual 
// Properties, Inc., University of Heidelberg, and The University 
// of Manchester. 
// All rights reserved. 

// Copyright (C) 2008 - 2009 by Pedro Mendes, Virginia Tech Intellectual 
// Properties, Inc., EML Research, gGmbH, University of Heidelberg, 
// and The University of Manchester. 
// All rights reserved. 

// Copyright (C) 2001 - 2007 by Pedro Mendes, Virginia Tech Intellectual 
// Properties, Inc. and EML Research, gGmbH. 
// All rights reserved. 

package Server;

/*
 * ServerSocketReader.java
 */

import java.io.*;
import java.net.*;
import java.util.*;
import Common.*;

/**The Reader class runs as a seperate thread and 
 * informs the ServerManager if the
 * client has sent a message. 
 * It remains blocked on a socket read for most of the time. 
 * It gets unblocked when it recieves a message from the server and calls the 
 * gotMessageFromClient(Message msg) of the ServerNetwork.
 */
public class ServerSocketReader extends Thread
{
	/**ClientConnection object (containing the socket) from where 
	 * this Reader will read.
	 */
	protected ClientConnection connection;
	
	/**Build the Reader using the ClientConnection object.
	* @param c This is the ClientConnection object 
	* (containing the socket) from which this Reader is to be built.
	*/
	public ServerSocketReader(ClientConnection c)
	{
		connection = c;
		this.start();
	}

	/**New Reader thread, gets blocked while reading from the socket.
	 * Unblocks when the client sends a message.
	 */
	public synchronized void run() 
	{
		try 
		{
	       	while (true)
	      	{
		        try
		        {
		        	
		        	showMessage(connection.toString());
		        	showMessage(connection.input.toString());
		        	
		        	connection.message.readDataObject(connection.input);
		        	//connection.message = (Message) connection.input.readObject();
		        }
		        catch(java.lang.ClassNotFoundException cnfe)
		        {
		        	showMessage("A ClassNotFoundException occured. Details...");
		        	cnfe.printStackTrace();
		        }
		        
		        connection.update = true;
	        
	        	synchronized (connection.manager) 
	        	{
	          		connection.manager.notify();
	        	}
	      	}
    	}
    	catch(IOException e) 
    	{
    		showMessage("An input or output exception occured while reading from socket");
    		System.exit(1);
    	}
    	catch(NullPointerException npe)
		{
			npe.getMessage();
		    npe.printStackTrace();
		}
  	}
  	
	/**A small function to display messages.
	* @param msg The message string to show.
	*/
  	private void showMessage(String msg)
  	{
  		System.err.println("ServerSocketReader: " + msg);
  	}
}