00001
00002
00003
00004
00005
00006
00007 package org.nees.rbnb;
00008
00009 import com.rbnb.sapi.*;
00010 import COM.Creare.Utility.ArgHandler;
00011 import COM.Creare.Utility.RBNBProcess;
00012
00013
00022 public class WalkerSource {
00023
00024 SimpleRandomWalk base = new SimpleRandomWalk();
00025
00026 private static final String SERVER = "localhost:3333";
00027 private static final String SOURCE_NAME = "RandomWalk";
00028 private static final String CHANNEL_NAME = "RandomWalkData";
00029 private static final long TIMER_INTERVAL=1000;
00030
00031 private String server = SERVER;
00032 private String sourceName = SOURCE_NAME;
00033 private String channelName = CHANNEL_NAME;
00034 private long timerInterval = TIMER_INTERVAL;
00035
00036 Source source = null;
00037 ChannelMap sMap;
00038 int index;
00039 boolean connected = false;
00040
00041 Thread timerThread;
00042 boolean runit = false;
00043
00044 public static void main(String[] args) {
00045 WalkerSource w = new WalkerSource(args);
00046 w.exec();
00047 w.startThread();
00048 }
00049
00050 private void printUsage() {
00051 System.out.println("WalkerSource: usage is...");
00052 System.out.print("WalkerSource ");
00053 System.out.print("[-s server_hostname *" + SERVER + "] ");
00054 System.out.print("[-n source_name *" + SOURCE_NAME + "] ");
00055 System.out.print("[-c channel_name *" + CHANNEL_NAME + "] ");
00056 System.out.print("[-t timer_interval *" + TIMER_INTERVAL + "]");
00057 System.out.println();
00058 }
00059
00060 public WalkerSource(String[] args) {
00061
00062 try {
00063 ArgHandler ah=new ArgHandler(args);
00064 if (ah.checkFlag('h')) {
00065 printUsage();
00066 RBNBProcess.exit(0);
00067 }
00068 if (ah.checkFlag('s')) {
00069 String a=ah.getOption('s');
00070 if (a!=null) server=a;
00071 }
00072 if (ah.checkFlag('n')) {
00073 String a=ah.getOption('n');
00074 if (a!=null) sourceName=a;
00075 }
00076 if (ah.checkFlag('c')) {
00077 String a=ah.getOption('c');
00078 if (a!=null) channelName=a;
00079 }
00080 if (ah.checkFlag('t')) {
00081 String a=ah.getOption('t');
00082 if (a!=null) timerInterval=Long.parseLong(a);
00083 }
00084 } catch (Exception e) {
00085 System.err.println("WalkerSource argument exception "+e.getMessage());
00086 e.printStackTrace();
00087 RBNBProcess.exit(0);
00088 }
00089
00090 System.out.println("Starting WalkerSource on " + server + " as " + sourceName);
00091 System.out.println(" Channel name = " + channelName + "; timer interval = " + timerInterval);
00092 System.out.println(" Use WalkerSource -h to see optional parameters");
00093 }
00094
00095 public WalkerSource(String server_host, String source_name, String channel_name,
00096 long timer_interval)
00097 {
00098 server = server_host;
00099 sourceName = source_name;
00100 channelName = channel_name;
00101 timerInterval = timer_interval;
00102
00103 System.out.println("Starting WalkerSource on " + server + " as " + sourceName);
00104 System.out.println(" Channel name = " + channelName + "; timer interval = " + timerInterval);
00105 System.out.println(" Use WalkerSource -h to see optional parameters");
00106 }
00107
00108 public WalkerSource()
00109 {
00110 this(SERVER,SOURCE_NAME,CHANNEL_NAME,TIMER_INTERVAL);
00111 }
00112
00113 public void exec()
00114 {
00115 try {
00116
00117 source=new Source();
00118 source.OpenRBNBConnection(server,sourceName);
00119 sMap = new ChannelMap();
00120 int index = sMap.Add(channelName);
00121 connected = true;
00122 System.out.println("WalkerSource: Connection made to sever = "
00123 + server + " as " + sourceName + " with " + channelName + ".");
00124 } catch (SAPIException se) { se.printStackTrace(); }
00125 }
00126
00127 public void startThread()
00128 {
00129
00130 if (!connected) return;
00131
00132
00133 Runnable r = new Runnable() {
00134 public void run() {
00135 runWork();
00136 }
00137 };
00138 runit = true;
00139 timerThread = new Thread(r, "Timer");
00140 timerThread.start();
00141 System.out.println("WalkerSource: Started thread.");
00142 }
00143
00144 public void stopThread()
00145 {
00146 if (!connected) return;
00147
00148 runit = false;
00149 timerThread.interrupt();
00150 System.out.println("WalkerSource: Stopped thread.");
00151 }
00152
00153 private void runWork ()
00154 {
00155 try {
00156 while(connected && runit)
00157 {
00158
00159
00160 sMap.PutTimeAuto("timeofday");
00161 double data[] = new double[1];
00162 data[0] = base.next();
00163 sMap.PutDataAsFloat64(index,data);
00164
00165 source.Flush(sMap);
00166 Thread.sleep(timerInterval);
00167 }
00168 } catch (SAPIException se) {
00169 se.printStackTrace();
00170 } catch (InterruptedException e) {
00171 e.printStackTrace();
00172 }
00173 timerThread = null;
00174 }
00175
00176 public boolean isRunning()
00177 {
00178 return (connected && runit);
00179 }
00180 }