📄 threadedechohandler.java
字号:
// ThreadedEchoHandler.java
// Andrew Davison, ad@fivedots.coe.psu.ac.th, August 2005
/* A threaded handler, called by EchoServer to deal with a client.
When a message comes in, it is sent back converted to uppercase.
The handler also communicates with the top-level MIDlet (ecm)
to add and decrement itself from the count of handlers, and
to display a received message.
closeDown() terminates the handler.
ThreadedEchoHandler uses the same readData() and sendMessage()
methods as EchoClient.
*/
import java.io.*;
import javax.microedition.io.*;
import javax.bluetooth.*;
public class ThreadedEchoHandler extends Thread
{
private EchoServerMIDlet ecm; // top-level MIDlet
private StreamConnection conn; // client connection
private InputStream in;
private OutputStream out;
private boolean isRunning = false;
private String clientName;
public ThreadedEchoHandler(StreamConnection conn, EchoServerMIDlet ecm)
{
this.conn = conn;
this.ecm = ecm;
System.out.println("Client Handler spawned");
// store the name of the connected client
try {
RemoteDevice rd = RemoteDevice.getRemoteDevice(conn);
clientName = getDeviceName(rd);
System.out.println("Client name: " + clientName);
}
catch(Exception e) {}
} // end of ThreadedEchoHandler()
private String getDeviceName(RemoteDevice dev)
/* Return the 'friendly' name of the device being examined,
or "Device ??" */
{
String devName;
try {
devName = dev.getFriendlyName(false); // to reduce connections
}
catch (IOException e)
{ devName = "Device ??"; }
return devName;
} // end of getDeviceName()
public void run()
/* Get an InputStream and OutputStream from the stream connection,
and start processing client messages. */
{
ecm.incrCount(); // tell top-level MIDlet there's a new handler
try {
// Get I/O streams from the stream connection
in = conn.openInputStream();
out = conn.openOutputStream();
processClient();
}
catch(Exception e)
{ System.out.println(e); }
ecm.decrCount(); // remove this handler from the top-level count
} // end of run()
private void processClient()
/* When a client message comes in, echo it back in uppercase.
If the client sends "bye$$", or there's a problem, then
terminate processing, and the handler.
*/
{
isRunning = true;
String line;
while (isRunning) {
if((line = readData()) == null)
isRunning = false;
else { // there was some input
System.out.println(clientName + " sent: " + line);
if (line.trim().equals("bye$$"))
isRunning = false;
else {
ecm.showMessage(clientName + ": " + line); // show at top-level
String upper = line.trim().toUpperCase();
if (isRunning)
sendMessage(upper);
}
}
}
System.out.println("Handler finished");
} // end of processClient()
public void closeDown()
// close down the handler
{
System.out.println("Close down echo handler");
isRunning = false;
try {
if (conn != null) {
in.close();
out.close();
conn.close();
}
}
catch (IOException e)
{ System.out.println(e); }
} // end of closeDown()
// --------------- IO methods ---------------------------
// Same as the methods in EchoClient
private String readData()
/* Read a message in the form "<length> msg".
The length allows us to know exactly how many bytes to read
to get the complete message. Only the message part (msg) is
returned, or null if there's been a problem.
*/
{ byte[] data = null;
try {
int len = in.read(); // get the message length
if (len <= 0) {
System.out.println("Message Length Error");
return null;
}
data = new byte[len];
len = 0;
// read the message, perhaps requiring several read() calls
while (len != data.length) {
int ch = in.read(data, len, data.length - len);
if (ch == -1) {
System.out.println("Message Read Error");
return null;
}
len += ch;
}
}
catch (IOException e)
{ System.out.println("readData(): " + e);
return null;
}
// System.out.println("readData: " + new String(data));
return new String(data); // convert byte[] to String
} // end of readData()
private boolean sendMessage(String msg)
// the message format is "<length> msg" in byte form
{
System.out.println("sendMessage: " + msg);
try {
out.write(msg.length());
out.write(msg.getBytes());
return true;
}
catch (Exception e)
{ System.out.println("sendMessage(): " + e);
return false;
}
} // end of sendMessage()
} // end of ThreadedEchoHandler class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -