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

PictureSource.java

00001 /* 00002 * This RBNB object supplies a "psudo video" stream by sending 00003 * a series of JPG images. 00004 * 00005 * Created on Feb 5, 2004 00006 * Derived from Axis Source (Dec 12, 2003) by Jason P. Hanley 00007 */ 00008 package org.nees.rbnb; 00009 00015 import java.awt.Image; 00016 import java.awt.Toolkit; 00017 import java.awt.MediaTracker; 00018 import java.awt.Component; 00019 import java.net.URL; 00020 00021 import java.io.FileInputStream; 00022 import java.io.File; 00023 import java.io.IOException; 00024 00025 import java.net.URL; 00026 import java.net.URLConnection; 00027 import java.net.MalformedURLException; 00028 00029 import com.rbnb.sapi.ChannelMap; 00030 import com.rbnb.sapi.Source; 00031 import com.rbnb.sapi.SAPIException; 00032 00033 //import COM.Creare.Utility.ArgHandler; //for argument parsing 00034 import com.rbnb.utility.ArgHandler; //for argument parsing 00035 00036 public class PictureSource extends RBNBBase{ 00037 private byte[][] fileBuffer; 00038 private String[] text; 00039 private int current = 0; 00040 private Thread timerThread = null; 00041 private int channelId = -1; 00042 private Source source = null; 00043 private ChannelMap cmap = null; 00044 00045 private static final String SOURCE_NAME_NAME = "SourceName"; 00046 private static final String SOURCE_NAME = "FakeVideoSource"; 00047 private static final String CHANNEL_NAME_NAME = "ChannelName"; 00048 private static final String CHANNEL_NAME = "Duke"; 00049 private static final String PICTURE_PATH_NAME = "PicturePath"; 00050 private static final String PICTURE_PATH = "./duke/Duke"; 00051 private static final String PICTURE_COUNT_NAME = "PictureCount"; 00052 private static final int PICTURE_COUNT = 10; 00053 private static final String TIMER_INTERVAL_NAME = "TimerInterval"; 00054 private static final long TIMER_INTERVAL=1000; 00055 private static final String CACHE_SIZE_NAME = "CacheSize"; 00056 private static final int DEFAULT_CACHE_SIZE=30; 00057 00058 private String sourceName = SOURCE_NAME; 00059 private String channelName = CHANNEL_NAME; 00060 private String picturePath = PICTURE_PATH; 00061 private int pictureCount = PICTURE_COUNT; 00062 private long timerInterval = TIMER_INTERVAL; 00063 private int cacheSize = DEFAULT_CACHE_SIZE; 00064 00065 private boolean connected = false; 00066 private boolean running = false; 00067 00068 private String[] parameterNameArray; 00069 private String[] parameterTypeArray; 00070 private Object[] parameterDefaultArray; 00071 00072 public static void main(String[] args) { 00073 // start from command line 00074 PictureSource p = new PictureSource(); 00075 if (p.setArgs(args)) p.start(); 00076 } 00077 00078 public PictureSource() 00079 { 00080 setParamterArrays(); 00081 initialize(); 00082 } 00083 00084 public void printUsage() { 00085 System.out.println("PictureSource: usage is..."); 00086 System.out.println("PictureSource "); 00087 super.printUsage(); 00088 System.out.println("[-N source_name *FakeVideoSource] "); 00089 System.out.println("[-C channel_name *Duke] "); 00090 System.out.println("[-P pathname_for_pictures *./duke/Duke]"); 00091 System.out.println("[-K picture_count *10]"); 00092 System.out.println("[-t timer_interval *1000]"); 00093 System.out.println("[-z cache size *" + DEFAULT_CACHE_SIZE + "]"); 00094 System.out.println(); 00095 } 00096 00097 protected void setParamterArrays() { 00098 00099 String [] pNameArray = super.getBaseParameterNameArray(); 00100 String [] pTypeArray = super.getBaseParameterTypeArray(); 00101 Object [] pDefaultArray = super.getBaseParameterDefaultArray(); 00102 00103 int base = pNameArray.length; 00104 int numberOfparameters = 6; 00105 parameterNameArray = new String[base + numberOfparameters]; 00106 parameterTypeArray = new String[base + numberOfparameters]; 00107 parameterDefaultArray = new Object[base + numberOfparameters]; 00108 00109 for (int i = 0; i < base; i++) 00110 { 00111 parameterNameArray[i] = pNameArray[i]; 00112 parameterTypeArray[i] = pTypeArray[i]; 00113 parameterDefaultArray[i] = pDefaultArray[i]; 00114 } 00115 00116 parameterNameArray[base + 0] = SOURCE_NAME_NAME; 00117 parameterTypeArray[base + 0] = RBNBBaseParameterHolder.STRING_TYPE; 00118 parameterDefaultArray[base + 0] = new String(SOURCE_NAME); 00119 00120 parameterNameArray[base + 1] = CHANNEL_NAME_NAME; 00121 parameterTypeArray[base + 1] = RBNBBaseParameterHolder.STRING_TYPE; 00122 parameterDefaultArray[base + 1] = new String(CHANNEL_NAME); 00123 00124 parameterNameArray[base + 2] = TIMER_INTERVAL_NAME; 00125 parameterTypeArray[base + 2] = RBNBBaseParameterHolder.LONG_TYPE; 00126 parameterDefaultArray[base + 2] = new Long(TIMER_INTERVAL); 00127 00128 parameterNameArray[base + 3] = PICTURE_PATH_NAME; 00129 parameterTypeArray[base + 3] = RBNBBaseParameterHolder.STRING_TYPE; 00130 parameterDefaultArray[base + 3] = new String(PICTURE_PATH); 00131 00132 parameterNameArray[base + 4] = PICTURE_COUNT_NAME; 00133 parameterTypeArray[base + 4] = RBNBBaseParameterHolder.INTEGER_TYPE; 00134 parameterDefaultArray[base + 4] = new Integer(PICTURE_COUNT); 00135 00136 parameterNameArray[base + 5] = CACHE_SIZE_NAME; 00137 parameterTypeArray[base + 5] = RBNBBaseParameterHolder.INTEGER_TYPE; 00138 parameterDefaultArray[base + 5] = new Integer(DEFAULT_CACHE_SIZE); 00139 00140 } 00141 00142 /* (non-Javadoc) 00143 * @see org.nees.rbnb.RBNBBase#setInstanceArgs(java.lang.String[]) 00144 */ 00145 protected boolean setInstanceArgs(String[] args) throws IllegalArgumentException { 00146 00147 //parse args 00148 try { 00149 ArgHandler ah=new ArgHandler(args); 00150 if (ah.checkFlag('N')) { 00151 String a=ah.getOption('N'); 00152 if (a!=null) sourceName=a; 00153 } 00154 if (ah.checkFlag('C')) { 00155 String a=ah.getOption('C'); 00156 if (a!=null) channelName=a; 00157 } 00158 if (ah.checkFlag('P')) { 00159 String a=ah.getOption('P'); 00160 if (a!=null) picturePath=a; 00161 } 00162 if (ah.checkFlag('K')) { 00163 String a=ah.getOption('K'); 00164 if (a!=null) pictureCount=Integer.parseInt(a); 00165 } 00166 if (ah.checkFlag('t')) { 00167 String a=ah.getOption('t'); 00168 if (a!=null) timerInterval=Long.parseLong(a); 00169 } 00170 if (ah.checkFlag('z')) { 00171 String a=ah.getOption('z'); 00172 if (a!=null) 00173 try 00174 { 00175 Integer i = new Integer(a); 00176 int value = i.intValue(); 00177 cacheSize = value; 00178 System.out.println("sizes " + cacheSize + "," + value); 00179 } 00180 catch (Exception ignore) {} 00181 } 00182 } catch (Exception e) { 00183 System.err.println("PictureSource argument exception "+e.getMessage()); 00184 throw new IllegalArgumentException("PictureSource argument exception "+e.getMessage()); 00185 } 00186 00187 System.out.println("Starting PictureSource on " + getServer() + " as " + sourceName); 00188 System.out.println(" Channel name = " + channelName + "; timer interval = " + timerInterval); 00189 System.out.println(" Using images " + picturePath + "01.jpg" 00190 + " through " + picturePath 00191 + (pictureCount<10?("0" + pictureCount):("" + pictureCount)) + ".jpg"); 00192 System.out.println(" Use PictureSource -h to see optional parameters"); 00193 return true; 00194 } 00195 00196 /* (non-Javadoc) 00197 * @see org.nees.rbnb.RBNBBase#setVariablesFromParameters() 00198 */ 00199 public void setVariablesFromParameters() { 00200 super.setBaseVarialbesFromParametes(); 00201 00202 sourceName = SOURCE_NAME; 00203 try{ 00204 Object obj = getValueWithDefault(SOURCE_NAME_NAME, SOURCE_NAME); 00205 sourceName = (String)obj; 00206 } catch (Throwable ignore){} 00207 00208 channelName = CHANNEL_NAME; 00209 try{ 00210 Object obj = getValueWithDefault(CHANNEL_NAME_NAME, CHANNEL_NAME); 00211 channelName = (String)obj; 00212 } catch (Throwable ignore){} 00213 00214 picturePath = PICTURE_PATH; 00215 try{ 00216 Object obj = getValueWithDefault(PICTURE_PATH_NAME, PICTURE_PATH); 00217 picturePath = (String)obj; 00218 } catch (Throwable ignore){} 00219 00220 pictureCount = PICTURE_COUNT; 00221 try{ 00222 Integer i = new Integer(PICTURE_COUNT); 00223 Object obj = getValueWithDefault(PICTURE_COUNT_NAME, i); 00224 pictureCount = ((Integer)obj).intValue(); 00225 } catch (Throwable ignore){} 00226 00227 timerInterval = TIMER_INTERVAL; 00228 try{ 00229 Long l = new Long(TIMER_INTERVAL); 00230 Object obj = getValueWithDefault(TIMER_INTERVAL_NAME, l); 00231 timerInterval = ((Long)obj).longValue(); 00232 } catch (Throwable ignore){} 00233 00234 cacheSize = DEFAULT_CACHE_SIZE; 00235 try{ 00236 Integer i = new Integer(DEFAULT_CACHE_SIZE); 00237 Object obj = getValueWithDefault(CACHE_SIZE_NAME, i); 00238 cacheSize = ((Integer)obj).intValue(); 00239 } catch (Throwable ignore){} 00240 00241 System.out.println("Starting PictureSource on " + getServer() + " as " + sourceName); 00242 System.out.println(" Channel name = " + channelName + "; timer interval = " + timerInterval); 00243 System.out.println(" Using images " + picturePath + "01.jpg" 00244 + " through " + picturePath 00245 + (pictureCount<10?("0" + pictureCount):("" + pictureCount)) + ".jpg"); 00246 System.out.println(" Use PictureSource -h to see optional parameters"); 00247 } 00248 00249 public boolean loadImageFiles() { 00250 fileBuffer = new byte[10][]; 00251 text = new String[10]; 00252 00253 current = 0; 00254 00255 URL fig = null; 00256 try { 00257 for (int i = 1; i < (pictureCount + 1); i++ ) 00258 { 00259 text[i-1] = picturePath + ((i<10)?("0" + i):(""+i)) + ".jpg"; 00260 } 00261 00262 for (int i = 0; i < text.length; i++) 00263 { 00264 File f = new File(text[i]); 00265 FileInputStream in = new FileInputStream(f); 00266 int fileLength = (int)f.length(); 00267 byte[] buffer = new byte[fileLength]; 00268 in.read(buffer); 00269 fileBuffer[i] = buffer; 00270 System.out.println("Read " + text[i] + " for " + fileLength + " bytes"); 00271 } 00272 00273 return true; 00274 } 00275 catch (Exception e) 00276 { 00277 System.out.println("Loading Image files failed at " + current + "; " + e); 00278 e.printStackTrace(); 00279 return false; 00280 } 00281 } 00282 00283 private byte[] next() 00284 { 00285 byte[] ret = null; 00286 int l = (int)fileBuffer.length; 00287 current = ( current + 1 ) % l; 00288 ret = fileBuffer[current]; 00289 return ret; 00290 } 00291 00292 private boolean exec() 00293 { 00294 connect(); 00295 if (!connected) return false; 00296 startThread(); 00297 return true; 00298 } 00299 00300 private void connect() { 00301 connected = false; 00302 source=new Source(cacheSize, "none", 0); 00303 try { 00304 source.OpenRBNBConnection(getServer(), sourceName); 00305 } catch (SAPIException e) { 00306 System.err.println("Failed to connect to RBNB server."); 00307 source = null; 00308 return; 00309 } 00310 00311 cmap = new ChannelMap(); 00312 cmap.PutTimeAuto("timeofday"); 00313 channelId = -1; 00314 try { 00315 channelId = cmap.Add(channelName); 00316 } catch (SAPIException e) { 00317 System.err.println("Failed to add video channel to channel map."); 00318 disconnect(); 00319 return; 00320 } 00321 cmap.PutMime(channelId, "image/jpeg"); 00322 System.out.println("ChannelId = " + channelId); 00323 connected = true; 00324 } // readySource 00325 00326 private void startThread() { 00327 // Use this inner class to hide the public run method 00328 Runnable r = new Runnable() { 00329 public void run() { 00330 runWork(); 00331 } 00332 }; 00333 running = true; 00334 timerThread = new Thread(r, "Timer"); 00335 timerThread.start(); 00336 System.out.println("PictureSource: Started thread."); 00337 } 00338 00339 public void stopThread() 00340 { 00341 if (!connected) return; 00342 00343 running = false; 00344 timerThread.interrupt(); 00345 System.out.println("PictureSource: Stopped thread."); 00346 } 00347 00348 private void runWork () 00349 { 00350 try 00351 { 00352 while(isRunning()) 00353 { 00354 if (!oneStep()) break; 00355 try { 00356 Thread.sleep(1000); 00357 } catch (InterruptedException e) { 00358 return; 00359 } 00360 } 00361 } 00362 catch (Throwable t) 00363 { 00364 System.out.println("PictureSource: error while running -- " + t); 00365 } 00366 if (connected) disconnect(); 00367 running = false; 00368 } 00369 00370 private boolean oneStep() 00371 { 00372 try{ 00373 byte[] imageData = next(); 00374 cmap.PutDataAsByteArray(channelId, imageData); 00375 source.Flush(cmap); 00376 // System.out.println("On channel " + channelId + ": sent image file " + text[current] + " for " 00377 // + imageData.length + " bytes"); 00378 } catch (Exception e){ 00379 System.err.println("Failed to send image " + current); 00380 e.printStackTrace(); 00381 disconnect(); 00382 return false; 00383 } 00384 return true; 00385 } 00386 00387 private void disconnect() 00388 { 00389 source.CloseRBNBConnection(); 00390 source = null; 00391 cmap = null; 00392 connected = false; 00393 } 00394 00395 /* (non-Javadoc) 00396 * @see org.nees.rbnb.RBNBBase#getParameterNameArray() 00397 */ 00398 public String[] getParameterNameArray() { 00399 return parameterNameArray; 00400 } 00401 00402 /* (non-Javadoc) 00403 * @see org.nees.rbnb.RBNBBase#getParameterTypeArray() 00404 */ 00405 public String[] getParameterTypeArray() { 00406 return parameterTypeArray; 00407 } 00408 00409 /* (non-Javadoc) 00410 * @see org.nees.rbnb.RBNBBase#getParameterDefaultsArray() 00411 */ 00412 public Object[] getParameterDefaultArray() { 00413 return parameterDefaultArray; 00414 } 00415 00416 public boolean isRunning() 00417 { 00418 return (connected && running); 00419 } 00420 00421 /* (non-Javadoc) 00422 * @see org.nees.rbnb.RBNBBase#start() 00423 */ 00424 public boolean start() { 00425 if (isRunning()) return false; 00426 if (connected) disconnect(); 00427 setVariablesFromParameters(); 00428 if (loadImageFiles()) 00429 { 00430 return exec(); 00431 } 00432 return false; 00433 } 00434 00435 /* (non-Javadoc) 00436 * @see org.nees.rbnb.RBNBBase#stop() 00437 */ 00438 public boolean stop() { 00439 if (!isRunning()) return false; 00440 stopThread(); 00441 disconnect(); 00442 return true; 00443 } 00444 00445 }

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