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