00001 package org.jaebi.midlet.bt.requestProcessing;
00002 import com.sun.midp.io.BufferedConnectionAdapter;
00003 import java.io.DataInputStream;
00004 import java.io.DataOutputStream;
00005 import java.io.IOException;
00006 import java.io.OutputStream;
00007 import javax.microedition.io.StreamConnection;
00008 import org.jaebi.midlet.bt.btException.BtIOException;
00009 import org.kxml.parser.XmlParser;
00010 import org.jaebi.midlet.bt.responseHandling.ServiceResponseHandler;
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00045 public abstract class RequestProcessor implements Runnable {
00046
00047 private Thread processorThread;
00048 protected ServiceResponseHandler handler = null;
00049 protected DataOutputStream streamToServer = null;
00050 protected DataInputStream streamFromServer = null;
00051
00052
00053
00054
00058 protected void sendRequest(String toSend) throws BtIOException{
00059
00060 System.out.println("RequestProcessor::sendRequest toSend = " + toSend);
00061
00062 try{
00063 OutputStream out = this.streamToServer;
00064 out.write(toSend.getBytes());
00065 } catch (IOException e){
00066 throw new BtIOException(e.getMessage());
00067 }
00068 }
00069
00073 private String getResponse(String endOfResponseMarker) throws BtIOException{
00074
00075 String received = "";
00076 try{
00077 DataInputStream input = this.streamFromServer;
00078 byte buffer[] = new byte[endOfResponseMarker.length()];
00079 int byteReaded;
00080 String temp;
00081 boolean isEnded = false;
00082
00083 do{
00084 byteReaded = input.read(buffer);
00085 temp = new String(buffer);
00086 System.out.println("RequestProcessor::sto ascoltando");
00087
00088
00089 System.out.println("REquest processor:: " + byteReaded);
00090
00091 received += temp.substring(0, byteReaded);
00092
00093 System.out.println("REquest processor:: received " + received);
00094
00095
00096
00097
00098
00099 if (received.length()> endOfResponseMarker.length() ){
00100 int startIndex = received.length()- endOfResponseMarker.length();
00101
00102
00103 if (received.substring(startIndex, received.length()).equalsIgnoreCase(endOfResponseMarker))
00104 isEnded = true;
00105 }
00106 }
00107
00108 while( !isEnded);
00109
00110 } catch (IOException e){
00111 throw new BtIOException(e.getMessage());
00112 }
00113
00114
00115 System.out.println("REquest processor:: " + received);
00116
00117 System.out.println("REquest processor:: stampa!" );
00118
00119 return received;
00120
00121 }
00122
00123 protected String getResponseContent(String beginMarker, String endMarker) throws BtIOException{
00124 String out = getResponse(endMarker);
00125
00126 out = out.substring(beginMarker.length(),out.length());
00127 int endIndex = out.length() - endMarker.length();
00128 out = out.substring(0, endIndex);
00129 return out;
00130 }
00131
00132
00136 public void processRequest(ServiceResponseHandler handler, DataInputStream streamFromServer, DataOutputStream streamToServer){
00137 this.handler = handler;
00138 this.streamFromServer = streamFromServer;
00139 this.streamToServer = streamToServer;
00140 this.processorThread = new Thread(this);
00141 processorThread.start();
00142 }
00143
00144 }