/Users/detro/workspace-nb/JAEBI-BlueToothJ2MEClient/src/org/jaebi/midlet/bt/requestProcessing/RequestProcessor.java

Vai alla documentazione di questo file.
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  * RequestProcessor.java
00013  *
00014  * Created on 18 giugno 2005, 15.58
00015  *
00016  * To change this template, choose Tools | Options and locate the template under
00017  * the Source Creation and Management node. Right-click the template and choose
00018  * Open. You can then make changes to the template in the Source Editor.
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         //TODO debug
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         //TODO fare anche qui il giochetto dei marker
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                 //TODO debug
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                  * quando è stata ricevuta la stringa che marca la fine del
00097                  * response (endOfResponseMarker) non devo fare più alcuna read
00098                  */
00099                 if (received.length()> endOfResponseMarker.length() ){
00100                     int startIndex = received.length()- endOfResponseMarker.length();
00101                     
00102                     //TODO se received contiene l'endMarker ma a dop questo ci sono dei whitespaces l'if di sotto non funziona, cioè, non si rileva la ricezione dell'endmarker
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         //TODO debug
00115         System.out.println("REquest processor:: " + received);
00116         //TODO debug
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 }

Generato il Thu Jun 23 00:03:00 2005 per JAEBI - BlueTooth J2ME Midlet Client da  doxygen 1.4.3