Show AgentService.java syntax highlighted
/*
* Copyright (c) 2006
* Helsinki Institute of Physics
* see LICENSE file for details
*
* AgentService.java
* Created on Jan 22, 2005
*/
package fi.hip.gb.bluetooth;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.Vector;
import javax.microedition.io.StreamConnection;
/**
* Bluetooth service for communication with GBAgent servers.
*
* @author Juho Karppinen
*/
public class AgentService extends Service {
/**
* Vector of packets pending to be sent to remote service. When message is
* sent, it is removed from the vector.
*/
private Vector msgs = new Vector();
public AgentService(EndPoint endpt, String url) {
super(endpt, url);
}
public AgentService(EndPoint endpt, StreamConnection c) {
super(endpt, c);
}
public void readMessages() {
this.con = null;
try {
openConnection();
} catch (Exception e) {
BTService.getInstance().log(
"cannot open connection to " + this.url + " : "
+ e.getMessage());
}
}
/**
* Put the message on the queue, pending to be sent by Sender thread
*
* @param signal
* @param str
* a string to be sended
*/
public synchronized void putString(int signal, String str) {
GPSResult res = new GPSResult();
res.setName(str);
putMessage(signal, res);
}
/**
* Put the message on the queue, pending to be sent by Sender thread
*
* @param signal
* @param msg
* a message to be sended
*/
public synchronized void putMessage(int signal, GPSResult msg) {
if (con == null) {
// if connection doesn't exist, create it and initiate the
// handshake protocol
try {
openConnection();
putString(BTService.SIGNAL_HANDSHAKE, BTService.localName);
} catch (Exception e) {
BTService.getInstance().log(
"cannot open connection to " + url + " : "
+ e.getMessage());
// MIDui.showException("cannot open bluetooth connection", e,
// null);
}
}
// btnet.log("invoke putMessage " + signal + " " + msg.getName());
msg.setType(signal);
msgs.addElement(msg);
synchronized (sender) {
// tell sender that there is a message pending to be sent
sender.notify();
}
}
/**
* Gets a topmost message from the queue
*
* @return message package, or null if no messages exists
*/
public synchronized GPSResult popMessage() {
if (msgs.size() > 0) {
// if there are message pending, return it and remove it from the
// vector
GPSResult res = (GPSResult) msgs.firstElement();
msgs.removeElementAt(0);
return res;
}
// if there is no message pending. return null
return null;
}
public synchronized boolean peekString() {
return (msgs.size() > 0);
}
/**
* Reader thread for the agent service.
*/
class Reader implements Runnable {
/*
* @see java.lang.Runnable#run()
*/
public void run() {
try {
DataInputStream datain = AgentService.this.con
.openDataInputStream();
while (!done) {
// endpt.btnet.log("Waiting for next signal from " +
// endpt.remoteName);
// read in the next signal (an integer)
// this will block until there is data to read
// int signal = datain.readInt();
GPSResult msg = null;// new LiteResult(datain);
int signal = msg.getType();
if (signal == BTService.SIGNAL_MESSAGE) {
/*
* String filename = datain.readUTF(); String
* description = datain.readUTF(); int length =
* datain.readInt(); endpt.btnet.log("length " +
* length); byte[] data = new byte[length];
* datain.read(data, 0, length);
*/
// LiteStatus message = new LiteStatus(filename,
// description, data);
BTService.getInstance().log(
"Read in message '" + msg.getName() + "' from "
+ endpt.getName()); // + ", " +
// description + " "
// + length + " b"
// emit RECEIVED event to BTListener implementation
BTService.getInstance().fireEvent(
BTListener.EVENT_RECEIVED, endpt, msg);
} else if (signal == BTService.SIGNAL_HANDSHAKE) {
// String s = datain.readUTF();
BTService.getInstance().log(
"Read in HANDSHAKE name " + msg.getName()
+ " from " + endpt.getName());
// update the remote user nick name
// endpt.remoteName = msg.getDescription().getJobname();
// echo acknowledgment and local user friendly name back
// to remote device
putString(BTService.SIGNAL_HANDSHAKE_ACK,
BTService.localName);
BTService.getInstance().fireEvent(
BTListener.EVENT_JOIN, endpt, null);
} else if (signal == BTService.SIGNAL_HANDSHAKE_ACK) {
// the string data is the remote user nick name
// String s = datain.readUTF();
BTService.getInstance().log(
"Read in HANDSHAKE_ACK name " + msg.getName()
+ " from " + endpt.getName());
// endpt.remoteName = msg.getDescription().getJobname();
BTService.getInstance().fireEvent(
BTListener.EVENT_JOIN, endpt, null);
} else if (signal == BTService.SIGNAL_TERMINATE) {
BTService.getInstance().log(
"Read in TERMINATE from " + endpt.getName());
putString(BTService.SIGNAL_TERMINATE_ACK, "end");
// clean up end point resources and associated
// connections
closeConnection();
} else if (signal == BTService.SIGNAL_TERMINATE_ACK) {
BTService.getInstance()
.log(
"read in TERMINATE_ACK from "
+ endpt.getName());
// doesn't do anything, just wake up from readInt() so
// that the
// thread can stop
} else {
BTService
.getInstance()
.log(
"Read unknown signal, probably connection closed");
}
}
datain.close();
} catch (Exception e) {
BTService.getInstance().log(
"failed to read from " + endpt + " " + e.getMessage());
// MIDui.showException("cannot read over bluetooth from " +
// endPoint.remoteName, e, null);
}
// remove the endpoint
// endPoint.btnet.callback.handleAction(BTListener.EVENT_LEAVE,
// endPoint, null);
// endPoint.btnet.cleanupRemoteEndPoint(endPoint);
BTService.getInstance().log(
"Reader thread exit for "
+ AgentService.this.endpt.getName());
}
}
/**
* Sender thread for agent service.
*/
public class AgentSender implements Runnable {
/*
* @see java.lang.Runnable#run()
*/
public void run() {
try {
DataOutputStream dataout = AgentService.this.con.openDataOutputStream();
while (!done) {
// check to see if there are any message to send.
// if not, then wait for 5 second
if (!peekString()) {
synchronized (this) {
this.wait(5000);
}
}
// wake up and get next string
GPSResult msg = popMessage();
if (msg != null) {
// if there is a message to send, send it now
BTService.getInstance().log(
"Sending signal " + msg.getType() + " name '"
+ msg.getName() + "' to "
+ endpt.getName());
//msg.store(dataout);
/*
dataout.writeInt(msg.getState());
dataout.writeUTF(msg.getName());
if(msg.getState() == BTService.SIGNAL_MESSAGE) {
dataout.writeUTF(msg.getDescription());
dataout.writeInt(msg.getSize());
dataout.write(msg.readBytes(), 0, msg.getSize());
}
*/
dataout.flush();
if (msg.getType() == BTService.SIGNAL_TERMINATE) {
// if the message is a TERMINATE signal, then break the run loop as
// well
closeConnection();
}
}
}
// close the output stream
dataout.close();
} catch (Exception e) {
BTService.getInstance().log(
"failed to send to " + endpt + " " + e.getMessage());
//MIDui.showException("failed to send over bluetooth to "
// + endpt.remoteName, e, null);
}
BTService.getInstance().log(
"Sender thread exit for "
+ AgentService.this.endpt.getName());
BTService.getInstance().fireEvent(BTListener.EVENT_LEAVE,
AgentService.this.endpt, null);
//endpt.btnet.cleanupRemoteEndPoint(endpt);
}
}
}
See more files for this project here