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

WalkerSource.java

00001 /* 00002 * Created on Feb 5, 2004 00003 * 00004 * A RBNB source that generates numbers in a bounded random walk. 00005 */ 00006 00007 package org.nees.rbnb; 00008 00009 import com.rbnb.sapi.*; 00010 //import COM.Creare.Utility.ArgHandler; //for argument parsing 00011 import com.rbnb.utility.ArgHandler; //for argument parsing 00012 00021 public class WalkerSource extends RBNBBase{ 00022 00023 // set the source for the random walk data 00024 private SimpleRandomWalk base = new SimpleRandomWalk(); 00025 00026 private static final String SOURCE_NAME_NAME = "SourceName"; 00027 private static final String SOURCE_NAME = "RandomWalk"; 00028 private static final String CHANNEL_NAME_NAME = "ChannelName"; 00029 private static final String CHANNEL_NAME = "RandomWalkData"; 00030 private static final String TIMER_INTERVAL_NAME = "TimerInterval"; 00031 private static final long TIMER_INTERVAL=1000; 00032 private static final String CHANNEL_COUNT_NAME = "ChannelCount"; 00033 private static final int CHANNEL_COUNT = 1; 00034 private static final String CACHE_SIZE_NAME = "CacheSize"; 00035 private static final int DEFAULT_CACHE_SIZE=100; 00036 00037 private String sourceName = SOURCE_NAME; 00038 private String channelName = CHANNEL_NAME; 00039 private long timerInterval = TIMER_INTERVAL; 00040 private int cacheSize = DEFAULT_CACHE_SIZE; 00041 private int channelCount = CHANNEL_COUNT; 00042 00043 Source source = null; 00044 ChannelMap sMap; 00045 int index; 00046 boolean connected = false; 00047 00048 Thread timerThread; 00049 boolean runit = false; 00050 00051 private String[] parameterNameArray; 00052 private String[] parameterTypeArray; 00053 private Object[] parameterDefaultArray; 00054 00055 public static void main(String[] args) { 00056 // start from command line 00057 WalkerSource w = new WalkerSource(); 00058 if (w.setArgs(args)) 00059 { 00060 w.connect(); 00061 w.startThread(); 00062 } 00063 } 00064 00065 public WalkerSource() 00066 { 00067 setParamterArrays(); 00068 initialize(); 00069 } 00070 00071 public void printUsage() { 00072 System.out.println("WalkerSource: usage is..."); 00073 System.out.println("WalkerSource "); 00074 super.printUsage(); // uses h, s, p 00075 System.out.println("[-n source_name *" + SOURCE_NAME + "] "); 00076 System.out.println("[-c channel_name *" + CHANNEL_NAME + "] "); 00077 System.out.println("[-m number of channels *" + CHANNEL_COUNT+ " ]"); 00078 System.out.println("[-t timer_interval *" + TIMER_INTERVAL + "]"); 00079 System.out.println("[-z cache size *" + DEFAULT_CACHE_SIZE + "]"); 00080 System.out.println(); 00081 } 00082 00083 protected boolean setInstanceArgs(String[] args) throws IllegalArgumentException 00084 { 00085 //parse args 00086 try { 00087 ArgHandler ah=new ArgHandler(args); 00088 if (ah.checkFlag('n')) { 00089 String a=ah.getOption('n'); 00090 if (a!=null) sourceName=a; 00091 } 00092 if (ah.checkFlag('c')) { 00093 String a=ah.getOption('c'); 00094 if (a!=null) channelName=a; 00095 } 00096 if (ah.checkFlag('t')) { 00097 String a=ah.getOption('t'); 00098 if (a!=null) timerInterval=Long.parseLong(a); 00099 } 00100 if (ah.checkFlag('z')) { 00101 String a=ah.getOption('z'); 00102 if (a!=null) 00103 try 00104 { 00105 Integer i = new Integer(a); 00106 int value = i.intValue(); 00107 cacheSize = value; 00108 System.out.println("sizes " + cacheSize + "," + value); 00109 } 00110 catch (Exception ignore) {} 00111 } 00112 if (ah.checkFlag('m')) { 00113 String a=ah.getOption('m'); 00114 if (a!=null) 00115 try 00116 { 00117 Integer i = new Integer(a); 00118 int value = i.intValue(); 00119 channelCount = value; 00120 System.out.println("channel count " + channelCount + "," + value); 00121 } 00122 catch (Exception ignore) {} 00123 } 00124 } catch (Exception e) { 00125 throw new IllegalArgumentException("Argument Exception: " + e); 00126 } 00127 00128 System.out.println("Starting WalkerSource on " + getServer() + " as " + sourceName); 00129 System.out.println(" Channel name = " + channelName + "; timer interval = " + timerInterval); 00130 System.out.println(" Cache Size = " + cacheSize + " Channel Count = " + channelCount); 00131 System.out.println(" Use WalkerSource -h to see optional parameters"); 00132 00133 return true; 00134 } 00135 00136 /* public WalkerSource(String server_host, String source_name, String channel_name, 00137 long timer_interval, int cache_size, int channel_count) 00138 { 00139 this(); 00140 sourceName = source_name; 00141 channelName = channel_name; 00142 timerInterval = timer_interval; 00143 cacheSize = cache_size; 00144 channelCount = channel_count; 00145 00146 System.out.println("Starting WalkerSource on " + getServer() + " as " + sourceName); 00147 System.out.println(" Channel name = " + channelName + "; timer interval = " + timerInterval); 00148 System.out.println(" Cache Size = " + cacheSize + " Channel Count = " + channelCount); 00149 System.out.println(" Use WalkerSource -h to see optional parameters"); 00150 } 00151 */ 00152 00153 protected void setParamterArrays() { 00154 00155 String [] pNameArray = super.getBaseParameterNameArray(); 00156 String [] pTypeArray = super.getBaseParameterTypeArray(); 00157 Object [] pDefaultArray = super.getBaseParameterDefaultArray(); 00158 00159 int base = pNameArray.length; 00160 int numberOfparameters = 5; 00161 parameterNameArray = new String[base + numberOfparameters]; 00162 parameterTypeArray = new String[base + numberOfparameters]; 00163 parameterDefaultArray = new Object[base + numberOfparameters]; 00164 00165 for (int i = 0; i < base; i++) 00166 { 00167 parameterNameArray[i] = pNameArray[i]; 00168 parameterTypeArray[i] = pTypeArray[i]; 00169 parameterDefaultArray[i] = pDefaultArray[i]; 00170 } 00171 00172 parameterNameArray[base + 0] = SOURCE_NAME_NAME; 00173 parameterTypeArray[base + 0] = RBNBBaseParameterHolder.STRING_TYPE; 00174 parameterDefaultArray[base + 0] = new String(SOURCE_NAME); 00175 00176 parameterNameArray[base + 1] = CHANNEL_NAME_NAME; 00177 parameterTypeArray[base + 1] = RBNBBaseParameterHolder.STRING_TYPE; 00178 parameterDefaultArray[base + 1] = new String(CHANNEL_NAME); 00179 00180 parameterNameArray[base + 2] = TIMER_INTERVAL_NAME; 00181 parameterTypeArray[base + 2] = RBNBBaseParameterHolder.LONG_TYPE; 00182 parameterDefaultArray[base + 2] = new Long(TIMER_INTERVAL); 00183 00184 parameterNameArray[base + 3] = CHANNEL_COUNT_NAME; 00185 parameterTypeArray[base + 3] = RBNBBaseParameterHolder.INTEGER_TYPE; 00186 parameterDefaultArray[base + 3] = new Integer(CHANNEL_COUNT); 00187 00188 parameterNameArray[base + 4] = CACHE_SIZE_NAME; 00189 parameterTypeArray[base + 4] = RBNBBaseParameterHolder.INTEGER_TYPE; 00190 parameterDefaultArray[base + 4] = new Integer(DEFAULT_CACHE_SIZE); 00191 00192 } 00193 00194 /* (non-Javadoc) 00195 * @see org.nees.rbnb.RBNBBase#setVariablesFromParameters() 00196 */ 00197 public void setVariablesFromParameters() { 00198 super.setBaseVarialbesFromParametes(); 00199 00200 sourceName = SOURCE_NAME; 00201 try{ 00202 Object obj = getValueWithDefault(SOURCE_NAME_NAME, SOURCE_NAME); 00203 sourceName = (String)obj; 00204 } catch (Throwable ignore){} 00205 00206 channelName = CHANNEL_NAME; 00207 try{ 00208 Object obj = getValueWithDefault(CHANNEL_NAME_NAME, CHANNEL_NAME); 00209 channelName = (String)obj; 00210 } catch (Throwable ignore){} 00211 00212 timerInterval = TIMER_INTERVAL; 00213 try{ 00214 Long l = new Long(TIMER_INTERVAL); 00215 Object obj = getValueWithDefault(TIMER_INTERVAL_NAME, l); 00216 timerInterval = ((Long)obj).longValue(); 00217 } catch (Throwable ignore){} 00218 00219 channelCount = CHANNEL_COUNT; 00220 try{ 00221 Integer i = new Integer(CHANNEL_COUNT); 00222 Object obj = getValueWithDefault(CHANNEL_COUNT_NAME, i); 00223 channelCount = ((Integer)obj).intValue(); 00224 } catch (Throwable ignore){} 00225 00226 cacheSize = DEFAULT_CACHE_SIZE; 00227 try{ 00228 Integer i = new Integer(DEFAULT_CACHE_SIZE); 00229 Object obj = getValueWithDefault(CACHE_SIZE_NAME, i); 00230 cacheSize = ((Integer)obj).intValue(); 00231 } catch (Throwable ignore){} 00232 00233 } 00234 00235 public void connect() 00236 { 00237 System.out.println("WalkerSource: Attempting to connect to server = " 00238 + getServer() + " as " + sourceName + " with " + channelName + "."); 00239 try { 00240 // Create a source and connect: 00241 source=new Source(cacheSize, "none", 0); 00242 source.OpenRBNBConnection(getServer(),sourceName); 00243 connected = true; 00244 System.out.println("WalkerSource: Connection made to server = " 00245 + getServer() + " as " + sourceName + " with " + channelName + "."); 00246 } catch (SAPIException se) { se.printStackTrace(); } 00247 } 00248 00249 private void disconnect() { 00250 source.CloseRBNBConnection(); 00251 connected = false; 00252 source = null; 00253 } 00254 00255 public void startThread() 00256 { 00257 00258 if (!connected) return; 00259 00260 // Use this inner class to hide the public run method 00261 Runnable r = new Runnable() { 00262 public void run() { 00263 runWork(); 00264 } 00265 }; 00266 runit = true; 00267 timerThread = new Thread(r, "Timer"); 00268 timerThread.start(); 00269 System.out.println("WalkerSource: Started thread."); 00270 } 00271 00272 public void stopThread() 00273 { 00274 if (!connected) return; 00275 00276 runit = false; 00277 timerThread.interrupt(); 00278 System.out.println("WalkerSource: Stopped thread."); 00279 } 00280 00281 private void runWork () 00282 { 00283 String[] names = new String[channelCount]; 00284 names[0] = channelName; 00285 for (int i = 1; i < channelCount; i++) 00286 { 00287 names[i] = channelName + i; 00288 } 00289 try 00290 { 00291 while(connected && runit) 00292 { 00293 // Push data onto the server: 00294 // System.out.print("Put new data to server: "); 00295 sMap = new ChannelMap(); 00296 int index[] = new int[channelCount]; 00297 for (int i = 0; i < channelCount; i++) 00298 index[i] = sMap.Add(names[i]); 00299 sMap.PutTimeAuto("timeofday"); 00300 for (int i = 0; i < channelCount; i++) 00301 { 00302 double data[] = new double[1]; 00303 data[0] = base.next(); 00304 sMap.PutDataAsFloat64(index[i],data); 00305 } 00306 // System.out.println("" + data[0]); 00307 source.Flush(sMap); 00308 Thread.sleep(timerInterval); 00309 } 00310 } catch (SAPIException se) { 00311 se.printStackTrace(); 00312 } catch (InterruptedException e) { 00313 e.printStackTrace(); 00314 } 00315 stop(); 00316 } 00317 00318 public boolean isRunning() 00319 { 00320 return (connected && runit); 00321 } 00322 00323 /* (non-Javadoc) 00324 * @see org.nees.rbnb.RBNBBase#getParameterNameArray() 00325 */ 00326 public String[] getParameterNameArray() { 00327 return parameterNameArray; 00328 } 00329 00330 /* (non-Javadoc) 00331 * @see org.nees.rbnb.RBNBBase#getParameterTypeArray() 00332 */ 00333 public String[] getParameterTypeArray() { 00334 return parameterTypeArray; 00335 } 00336 00337 /* (non-Javadoc) 00338 * @see org.nees.rbnb.RBNBBase#getParameterDefaultArray() 00339 */ 00340 public Object[] getParameterDefaultArray() { 00341 return parameterDefaultArray; 00342 } 00343 00344 /* (non-Javadoc) 00345 * @see org.nees.rbnb.RBNBBase#start() 00346 */ 00347 public boolean start() { 00348 if (isRunning()) return false; 00349 if (connected) disconnect(); 00350 setVariablesFromParameters(); 00351 connect(); 00352 if (!connected) return false; 00353 startThread(); 00354 return true; 00355 } 00356 00357 /* (non-Javadoc) 00358 * @see org.nees.rbnb.RBNBBase#stop() 00359 */ 00360 public boolean stop() { 00361 if (!isRunning()) return false; 00362 stopThread(); 00363 disconnect(); 00364 return true; 00365 } 00366 00367 }

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