/Users/detro/workspace-nb/JAEBI-BlueToothJ2MEClient/src/ConnectionProcessor.java

Vai alla documentazione di questo file.
00001 import java.io.DataInputStream;
00002 import java.io.DataOutputStream;
00003 import java.io.IOException;
00004 import java.rmi.RemoteException;
00005 import javax.microedition.io.StreamConnection;
00006 import javax.xml.rpc.Stub;
00007 import org.jaebi.midlet.WSClient.JAEBIWSSEI;
00008 import org.jaebi.midlet.WSClient.JAEBIWSSEI_Stub;
00009 
00010 import org.jaebi.midlet.bt.BtServer;
00011 /*
00012  * ConnectionProcessor.java
00013  *
00014  * Created on 15 giugno 2005, 23.15
00015  *
00016  * Classe incaricata di gestire la logica del message passing tra client e server
00017  * e, in generale, la logica con cui gestire ogni singola connessione
00018  */
00019 
00036 public class ConnectionProcessor implements Runnable{
00037     
00038     private StreamConnection connection = null;
00039     //TODO deve contenere pure la lcasse stub WS
00040     private int sessionId;
00041     
00042     private BtServer server;
00043     
00044     private JAEBIWSSEI service;
00045     
00047     public ConnectionProcessor(StreamConnection connection, BtServer server, String serviceURL) {
00048         this.connection = connection;
00049         this.server = server;
00050         this.service = service = new JAEBIWSSEI_Stub();
00051         
00052         // Initialize the stub/service.
00053         ((JAEBIWSSEI_Stub)service)._setProperty(Stub.ENDPOINT_ADDRESS_PROPERTY,
00054                 serviceURL);
00055          new Thread(this).start();
00056     }
00057     
00058     /*
00059      * Legge dallo stream della midlet client i dati necessari ad inoltrare una
00060      * richiesta al WS, ossia, ad invocare uno dei suoi metodi esposti
00061      * @param input lo stream proveniente dalla midlet client
00062      * @param endOfRequestMarker stringa indicante che sullo stream non è
00063      * presente alcun altra info necesaria all'invocazione di un metodo del WS
00064      */
00065     private String getRequest(DataInputStream input, String endOfRequestMarker) throws IOException{
00066         String temp;
00067         boolean isEnded = false;
00068         int byteReaded;
00069         byte buffer[] = new byte[endOfRequestMarker.length()];
00070         String received = "";
00071         
00072         do{
00073             byteReaded = input.read(buffer);
00074             temp = new String(buffer);
00075             
00076             received += temp.substring(0, byteReaded);
00077             
00078 //            System.out.println("ConnectionProcessor:: " + received);
00079             
00080             
00081                 /*
00082                  * quando è stata ricevuta la stringa che marca la fine della
00083                  * request (endOfRequestMarker) non devo fare più alcuna read
00084                  */
00085             if (received.length()>= endOfRequestMarker.length() ){
00086                 int startIndex = received.length()- endOfRequestMarker.length();
00087                 if (received.substring(startIndex, received.length()).equalsIgnoreCase(endOfRequestMarker))
00088                     isEnded = true;
00089             }
00090         }
00091         while( !isEnded);
00092         
00093         return received;
00094     }
00095     
00096     
00097     private String getRequestContent(String request, String beginMarker, String endMarker){
00098         StringBuffer out = new StringBuffer(request);
00099         out.delete(0, beginMarker.length());
00100         out.delete(out.length()-endMarker.length(), out.length());
00101         return out.toString();
00102     }
00103     
00104     
00105     public void run(){
00106         boolean logout = false;
00107         DataInputStream streamFromClient = null;
00108         DataOutputStream streamToClient  = null;
00109         
00110         try{
00111             streamFromClient = this.connection.openDataInputStream();
00112             streamToClient = this.connection.openDataOutputStream();
00113         } catch(IOException e){
00119             //TODO debug
00120             System.out.println("errore");
00121             
00122             e.printStackTrace();
00123             //se avviene l'eccezione ci sono errori sulla connessione:bisogna rimuoverla
00124             server.removeConnection(this.connection);
00125             return;
00126         }
00127         
00128         StreamConnection conn = this.connection;
00129         int i = 0;
00130         do{
00131             System.out.println("ConnectionProcessor::run è la " + ++i + "volta");
00132             try{
00133                 
00134                 byte buffer[] = new byte[8];
00135                 
00136                 String request ="";
00137                 String response ="";
00138                 String responseBeginMarker = "";
00139                 String responseEndMarker = "";
00140                 
00141                 int result  = -1;
00142                 int loginBeginMarkerLength = 7;
00143                 int logoutBeginMarkerLength = 8;
00144                 
00145                 //flag se indica se il metodo da invocare è executeRequest
00146                 boolean executeRequest = true;
00147                 
00148                 /*******************************************************************
00149                  * Leggo i prmi 8 caratteri che ha inviato il client per capire
00150                  * qual'è la richiesta che si desidera inoltrare al WS, ossia, quale
00151                  * dei suoi metodi invocare.
00152                  *******************************************************************
00153                  */
00154                 streamFromClient.read(buffer);
00155                 request = new String(buffer);
00156                 
00157                 
00158             /*
00159              *******************************************************************
00160              * bisogna invocare login()?
00161              *******************************************************************
00162              */
00163                 if (request.substring(0,loginBeginMarkerLength).equals("[login]")){
00164                     
00165                     responseBeginMarker = "[login_response]";
00166                     responseEndMarker = "[/login_response]";
00167                     
00168                     request += getRequest(streamFromClient, "[/login]");
00169                     
00170                     String requestContent = getRequestContent(request, "[login]", "[/login]");
00171                     
00172                     String nickName = requestContent.substring(0, requestContent.indexOf(" "));
00173                     String userType = "Midlet";
00174                     
00175                     response = "error";
00176                     
00177                     try{
00178                         result  = service.login(nickName, userType);
00179                         if (result != -1){
00180                             this.sessionId = result;
00181                             response="ok";
00182                         }
00183                         
00184                         
00185                         //TODO debug
00186                         System.out.println("ConnectionProcessor::run sessid "+ result);
00187                         
00188                         
00189                     } catch (RemoteException e){
00190                         response = "error";
00191                     }
00192                     
00193                     response = responseBeginMarker + response + responseEndMarker;
00194                     streamToClient.write(response.getBytes());
00195                     
00196                     executeRequest = false;
00197                     
00198                 }
00199                 
00200                 
00201              /*
00202               ******************************************************************
00203               * bisogna invocare logout()?
00204               ******************************************************************
00205               */
00206                 if (request.substring(0,logoutBeginMarkerLength).equals("[logout]")){
00207                     request += getRequest(streamFromClient, "[/logout]");
00208                     
00209                     executeRequest = false;
00210                     service.logout(sessionId);
00211                     //TODO devo mandare un response al client
00212                     logout = true;
00213                     
00214                 }
00215                 
00216                 
00217             /*
00218              *******************************************************************
00219              * bisogna invocare xrequest()?
00220              *******************************************************************
00221              */
00222                 if(executeRequest){
00223                     request += getRequest(streamFromClient, "</xrequest>");
00224                     //TODO debug
00225                     System.out.println("ConnectionProcessor:: run xrequest:" + request);
00226                     response = service.executeRequest(this.sessionId, request);
00227                     
00228                     //TODO debug
00229                     System.out.println("ConnectionProcessor:: run xresponse:" + response);
00230                     streamToClient.write(response.getBytes());
00231                     
00232                     //TODO devo controllare se oltre i marker non c'è nient'altro?
00233                     
00234                 }
00235                 
00236             }
00237             
00238             catch(IOException e){
00244                 //TODO debug
00245                 System.out.println("errore");
00246                 
00247                 e.printStackTrace();
00248                 //se avviene l'eccezione ci sono errori sulla connessione:bisogna rimuoverla
00249                 server.removeConnection(this.connection);
00250             }
00251         }
00252         while (!logout);
00253         
00254     }
00255     
00256     
00257 }

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