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
00013
00014
00015
00016
00017
00018
00019
00036 public class ConnectionProcessor implements Runnable{
00037
00038 private StreamConnection connection = null;
00039
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
00053 ((JAEBIWSSEI_Stub)service)._setProperty(Stub.ENDPOINT_ADDRESS_PROPERTY,
00054 serviceURL);
00055 new Thread(this).start();
00056 }
00057
00058
00059
00060
00061
00062
00063
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
00079
00080
00081
00082
00083
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
00120 System.out.println("errore");
00121
00122 e.printStackTrace();
00123
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
00146 boolean executeRequest = true;
00147
00148
00149
00150
00151
00152
00153
00154 streamFromClient.read(buffer);
00155 request = new String(buffer);
00156
00157
00158
00159
00160
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
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
00204
00205
00206 if (request.substring(0,logoutBeginMarkerLength).equals("[logout]")){
00207 request += getRequest(streamFromClient, "[/logout]");
00208
00209 executeRequest = false;
00210 service.logout(sessionId);
00211
00212 logout = true;
00213
00214 }
00215
00216
00217
00218
00219
00220
00221
00222 if(executeRequest){
00223 request += getRequest(streamFromClient, "</xrequest>");
00224
00225 System.out.println("ConnectionProcessor:: run xrequest:" + request);
00226 response = service.executeRequest(this.sessionId, request);
00227
00228
00229 System.out.println("ConnectionProcessor:: run xresponse:" + response);
00230 streamToClient.write(response.getBytes());
00231
00232
00233
00234 }
00235
00236 }
00237
00238 catch(IOException e){
00244
00245 System.out.println("errore");
00246
00247 e.printStackTrace();
00248
00249 server.removeConnection(this.connection);
00250 }
00251 }
00252 while (!logout);
00253
00254 }
00255
00256
00257 }