Main Page | Class Hierarchy | Class List | File List | Class Members | Related Pages

NTCPHack.java

Go to the documentation of this file.
00001 00014 package org.nees.ntcp.client; 00015 00016 import org.nees.ntcp.ntcpServer.ParameterType; 00017 import org.nees.ntcp.ntcpServer.ControlPointType; 00018 import org.nees.ntcp.ntcpServer.ControlPointParameterNameType; 00019 import org.nees.ntcp.ntcpServer.ControlPointGeomParameterType; 00020 import org.nees.ntcp.ntcpServer.GeomAxisType; 00021 import org.nees.ntcp.ntcpServer.TransactionType; 00022 import org.nees.ntcp.ntcpServer.TransactionStateType; 00023 import org.nees.ntcp.server.util.NtcpHelper; 00024 import org.nees.ntcp.ntcpServer.NtcpServer; 00025 00026 import java.math.BigInteger; 00027 import java.util.Vector; 00028 import java.util.*; 00029 00030 import com.rbnb.sapi.*; 00031 00032 import com.rbnb.utility.ArgHandler; //for argument parsing 00033 import com.rbnb.utility.RBNBProcess; //alternative to System.exit, so 00034 //don't bring down servlet engine 00035 //import COM.Creare.Utility.ArgHandler; //for argument parsing 00036 //import COM.Creare.Utility.RBNBProcess; //alternative to System.exit, so 00037 //don't bring down servlet engine 00038 00039 public class NTCPHack { 00040 00041 String serverURL= "http://neespop.mcs.anl.gov:8090/"; 00042 String instanceName = "NTCPServer"; 00043 boolean beSecure = false; 00044 NtcpServer Server = null; 00045 String transName = "RedFish"; 00046 ControlPointGeomParameterType move = new ControlPointGeomParameterType(); 00047 Random ran = new Random(); 00048 BigInteger transNum = new BigInteger(16,ran); 00049 ControlPointType ctrlPoint = new ControlPointType(); 00050 ControlPointType cpq = new ControlPointType(); 00051 TransactionStateType tstate; 00052 int timeout = 120; 00053 int memory = 300; 00054 TransactionType trans = new TransactionType();; 00055 ControlPointType[] cpArray; 00056 Vector osParams; 00057 00058 Sink sink = null; 00059 int inputChannelIndex = -1; 00060 Source source = null; 00061 int outputChannelIndex = -1; 00062 00063 private static final String MODE_TEST = "t"; 00064 private static final String MODE_RBNB = "r"; 00065 private static final String MODE_ONLY = "o"; 00066 private String mode = null; 00067 00068 private final static String DEFAULT_RBNB_SERVER = "localhost"; 00069 private final static String DEFAULT_RBNB_PORT = "3333"; 00070 private final static String DEFAULT_RBNB_OUTPUT_NAME = "CommandEcho"; 00071 private final static String DEFAULT_RBNB_OUTPUT_CHANNEL = "echo"; 00072 private final static String DEFAULT_RBNB_SINK_NAME = "NTCPHack"; 00073 00074 private String rbnbServerName = DEFAULT_RBNB_SERVER; 00075 private String rbnbServerPort = DEFAULT_RBNB_PORT; 00076 private String rbnbHostName = rbnbServerName + ":" + rbnbServerPort; 00077 private String rbnbSourceName = DEFAULT_RBNB_OUTPUT_NAME; 00078 private String rbnbSourceChannel = DEFAULT_RBNB_OUTPUT_CHANNEL; 00079 00080 private String rbnbSinkName = DEFAULT_RBNB_SINK_NAME; 00081 00082 private String rbnbInputPath = null; // required 00083 00085 private static void printUsage() 00086 { 00087 System.out.println("Usage for NTCPHack..."); 00088 System.out.println(" -v RBNB Sink (input command) path (required) "); 00089 System.out.println(" -m mode (required) " + MODE_TEST + "|" + MODE_RBNB + "|" + MODE_ONLY +"*"); 00090 System.out.println(" [-r RBNB Server Name *"+ DEFAULT_RBNB_SERVER + "]"); 00091 System.out.println(" [-p RBNB Server Port *" + DEFAULT_RBNB_PORT + "]"); 00092 System.out.println(" [-s RBNB Source (output) Name *" + DEFAULT_RBNB_OUTPUT_NAME + "]"); 00093 System.out.println(" [-c RBNB Source (output) Channel *" + DEFAULT_RBNB_OUTPUT_CHANNEL + "]"); 00094 System.out.println(" * mode -m " + MODE_TEST + " -> NTCP connection only"); 00095 System.out.println(" mode -m " + MODE_ONLY + " -> RBNB connection only"); 00096 System.out.println(" mode -m " + MODE_RBNB + " -> both NTCP and RBNB connections"); 00097 } 00098 00103 public NTCPHack(String[] args) 00104 { 00105 //parse args 00106 try { 00107 ArgHandler ah=new ArgHandler(args); 00108 if (ah.checkFlag('h')) { 00109 printUsage(); 00110 RBNBProcess.exit(0); 00111 } 00112 if (ah.checkFlag('r')) { 00113 String a=ah.getOption('r'); 00114 if (a!=null) rbnbServerName=a; 00115 } 00116 if (ah.checkFlag('p')) { 00117 String a=ah.getOption('p'); 00118 if (a!=null) rbnbServerPort=a; 00119 } 00120 if (ah.checkFlag('s')) { 00121 String a=ah.getOption('s'); 00122 if (a!=null) rbnbSourceName=a; 00123 } 00124 if (ah.checkFlag('c')) { 00125 String a=ah.getOption('c'); 00126 if (a!=null) rbnbSourceChannel=a; 00127 } 00128 if (ah.checkFlag('v')) { 00129 String a=ah.getOption('v'); 00130 if (a!=null) rbnbInputPath=a; 00131 } 00132 if (ah.checkFlag('m')) { 00133 String a=ah.getOption('m'); 00134 if (a!=null) mode=a; 00135 } 00136 } catch (Exception e) { 00137 System.err.println("NTCPHack argument exception "+e.getMessage()); 00138 e.printStackTrace(); 00139 printUsage(); 00140 RBNBProcess.exit(0); 00141 } 00142 00143 if (rbnbInputPath == null) 00144 { 00145 System.err.println("The source/channel path for the video source is required. " 00146 + "Use NTCPHack -h for help"); 00147 printUsage(); 00148 RBNBProcess.exit(0); 00149 } 00150 00151 if ((mode==null) 00152 || !(mode.equals(MODE_TEST) 00153 || mode.equals(MODE_ONLY) 00154 || mode.equals(MODE_RBNB) 00155 )) 00156 { 00157 System.err.println("mode = " + mode + " is incorrect."); 00158 printUsage(); 00159 RBNBProcess.exit(0); 00160 } 00161 00162 rbnbHostName = rbnbServerName + ":" + rbnbServerPort; 00163 00164 } 00165 00170 public static void main (String[] args) 00171 { 00172 (new NTCPHack(args)).doIt(); 00173 } 00174 00178 private void doIt () { 00179 00180 try 00181 { 00182 if (mode.equals(MODE_TEST) || mode.equals(MODE_RBNB)) 00183 { 00184 // Create CP - ANCO 00185 ctrlPoint.setControlPointName("ANCO"); 00186 00187 // Create parameter for opensession 00188 osParams = new Vector(); 00189 osParams.add(NtcpHelper.getParameter("OneFish", "TwoFish")); 00190 00191 // Set up the move - displacement along X axis, 10mm 00192 move.setName(ControlPointParameterNameType.displacement); 00193 move.setAxis(GeomAxisType.x); 00194 move.setValue(new Float(10.0)); 00195 00196 // set the type 00197 ctrlPoint.setControlPointType(new ControlPointGeomParameterType[] {move}); 00198 00199 // Create the 1-element array 00200 cpArray = new ControlPointType[] {ctrlPoint}; 00201 00202 } 00203 00204 if (mode.equals(MODE_TEST)) 00205 { 00206 openConnection(); 00207 doMove(); 00208 // queryControlPoint(); 00209 shutdown(); 00210 } 00211 else if (mode.equals(MODE_ONLY)) 00212 { 00213 String message = "Starting..."; 00214 startRBNB(); 00215 while(!message.equals("stop")) 00216 { 00217 message = getRBNBMessage(); 00218 System.out.println("Message from RBNB: " + message); 00219 sendRBNBMessageResponse("Got message: " + message); 00220 } 00221 stopRBNB(); 00222 } 00223 else if (mode.equals(MODE_RBNB)) 00224 { 00225 String message = "Starting..."; 00226 openConnection(); 00227 startRBNB(); 00228 while(!message.equals("stop")) 00229 { 00230 message = getRBNBMessage(); 00231 System.out.println("Message from RBNB: " + message); 00232 if (message.equals("shake")) 00233 { 00234 doMove(); 00235 } 00236 sendRBNBMessageResponse("Got message: " + message); 00237 } 00238 stopRBNB(); 00239 shutdown(); 00240 } 00241 } 00242 catch (Exception e) 00243 { 00244 System.out.println("Oops: " + e); 00245 e.printStackTrace(); 00246 } 00247 } 00248 00253 private void openConnection() throws Exception 00254 { 00255 // Try and open a connection to the server 00256 System.out.println("Opening connection to NTCP server at " + serverURL); 00257 if(beSecure == true) 00258 { 00259 System.out.println("with security enabled"); 00260 } 00261 else 00262 { 00263 System.out.println("in insecure mode"); 00264 } 00265 00266 try 00267 { 00268 Server = NtcpHelper.activateNtcpServer(serverURL, 00269 "ogsa/services/nees/ntcp/" + instanceName, 00270 beSecure); 00271 } catch (Exception e) 00272 { 00273 if(beSecure == true) 00274 { 00275 System.out.println("Unable to open connection to NTCP server at " + serverURL + " with security enabled"); 00276 } 00277 else 00278 { 00279 System.out.println("Unable to open connection to NTCP server at " + serverURL + " in insecure mode"); 00280 } 00281 throw e; 00282 } 00283 00284 // Open session 00285 System.out.println("Connection opened OK, now sending openSession"); 00286 try 00287 { 00288 NtcpHelper.openSession(Server, (ParameterType[]) osParams.toArray(new ParameterType[osParams.size()])); 00289 } 00290 catch(Exception e) 00291 { 00292 System.out.println("Error opening session with server"); 00293 throw e; 00294 } 00295 } 00296 00301 private void doMove() throws Exception 00302 { 00303 00304 transNum = new BigInteger(16,ran); 00305 transName = "TR" + ran.nextInt(); 00306 00307 // Propose the move/trigger 00308 try 00309 { 00310 tstate = NtcpHelper.propose(Server, 00311 transName, 00312 transNum, 00313 cpArray, 00314 timeout, 00315 timeout, 00316 memory); 00317 } 00318 catch(Exception e) 00319 { 00320 System.out.println("Error proposing move " + e); 00321 throw e; 00322 } 00323 00324 // Check the return value 00325 if(tstate == org.nees.ntcp.ntcpServer.TransactionStateType.accepted) 00326 { 00327 System.out.println("Transaction proposal accepted. Excellent."); 00328 } 00329 else 00330 { 00331 System.out.println("Transaction rejected! Why? Why!?"); 00332 throw new Exception("Trasnaction rejected, no additional information :-("); 00333 } 00334 00335 // Do it. 00336 try 00337 { 00338 NtcpHelper.execute(Server, transName); 00339 } 00340 catch(Exception e) 00341 { 00342 System.out.println("Exception during execution, state of ANCO unknown!"); 00343 // throw e; 00344 } 00345 00346 // Check the results 00347 try 00348 { 00349 trans = NtcpHelper.getTransaction(Server, transName); 00350 } 00351 catch(Exception e) 00352 { 00353 System.out.println("Exception during getTransaciton = " + e); 00354 throw e; 00355 } 00356 // System.out.println("Transaction state: + " trans.getState()); 00357 00358 } 00359 00363 private void queryControlPoint() throws Exception 00364 { 00365 // Just for fun, query the control point 00366 try 00367 { 00368 cpq = NtcpHelper.getControlPoint(Server, transName); 00369 } 00370 catch(Exception e) 00371 { 00372 System.out.println("Error querying the control point!"); 00373 throw e; 00374 } 00375 } 00376 00380 private void shutdown() throws Exception 00381 { 00382 // Shut down the ntcp connection 00383 try 00384 { 00385 System.out.println("Closing NTCP session"); 00386 NtcpHelper.closeSession(Server); 00387 00388 } 00389 catch(Exception e) 00390 { 00391 System.out.println("Error closing the session = " + e); 00392 throw e; 00393 } 00394 00395 System.out.println("Done."); 00396 } 00397 00401 private void startRBNB() throws Exception 00402 { 00403 // Create a sink and connect: 00404 sink=new Sink(); 00405 sink.OpenRBNBConnection(rbnbHostName, rbnbSinkName); 00406 ChannelMap reqmap = new ChannelMap(); 00407 inputChannelIndex = reqmap.Add(rbnbInputPath); 00408 sink.Subscribe(reqmap,"newest"); 00409 00410 // create soruce (to send responses) 00411 // connect to RBNB server as source 00412 source = new Source(10, "none", 0); 00413 source.OpenRBNBConnection(rbnbHostName, rbnbSourceName); 00414 00415 System.out.println("Source and Sink created."); 00416 } 00417 00421 private String getRBNBMessage() throws SAPIException { 00422 // see if any data is available from source 00423 ChannelMap m = sink.Fetch(-1); 00424 if (m == null) 00425 { 00426 System.out.println("Data fetch failed."); 00427 return ""; 00428 } 00429 00430 // no data received, skip 00431 if (m.GetIfFetchTimedOut()) { 00432 System.err.println("Data request timed out, retrying."); 00433 return ""; 00434 } 00435 00436 String[] st = m.GetDataAsString(inputChannelIndex); 00437 System.out.println("Command(s) Received: "); 00438 for (int i = 0; i < st.length; i++) 00439 { 00440 System.out.println(i + ": " + st[i]); 00441 } 00442 00443 return st[0]; 00444 } 00445 00449 private void sendRBNBMessageResponse(String message) throws SAPIException 00450 { 00451 00452 // select channel to output data to 00453 ChannelMap cmap = new ChannelMap(); 00454 try { 00455 outputChannelIndex = cmap.Add(rbnbSourceChannel); 00456 } catch (SAPIException e) { 00457 System.err.println("Failed to add output channel to channel map."); 00458 throw e; 00459 } 00460 00461 // put data in channel map, preserving original time stamp 00462 try { 00463 cmap.PutDataAsString(outputChannelIndex, message); 00464 source.Flush(cmap, true); 00465 } catch (SAPIException e) { 00466 System.err.println("Failed to flush output data to server, skipping."); 00467 throw e; 00468 } 00469 } 00470 00472 private void stopRBNB() { 00473 System.out.println("Called stopRBNB which is unimplemented!"); 00474 } 00475 00476 } 00477

Generated on Tue Aug 24 11:12:26 2004 for Data turbine for NEESGrid by doxygen 1.3.7