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.Creare.Utility.RBNBProcess; //alternative to System.exit, so
00035                                        //don't bring down servlet engine
00036 
00037 public class PictureSource{
00038     private byte[][] fileBuffer;
00039     private String[] text;
00040     private int current = 0;
00041     private Thread timerThread = null;
00042     private int channelId = -1;
00043     private Source source = null;   
00044     private ChannelMap cmap = null;
00045 
00046     private static final String SERVER = "localhost:3333";
00047     private static final String SOURCE_NAME = "FakeVideoSource";
00048     private static final String CHANNEL_NAME = "Duke";
00049     private static final String PICTURE_PATH = "./duke/Duke";
00050     private static final int PICTURE_COUNT = 10;
00051     private static final long TIMER_INTERVAL=1000;
00052     
00053     private String server = SERVER;
00054     private String sourceName = SOURCE_NAME;
00055     private String channelName = CHANNEL_NAME;
00056     private String picturePath = PICTURE_PATH;
00057     private int pictureCount = PICTURE_COUNT;
00058     private long timerInterval = TIMER_INTERVAL;
00059 
00060     private void printUsage() {
00061         System.out.println("PictureSource: usage is...");       
00062         System.out.print("PictureSource ");
00063         System.out.print("[-s server_hostname *localhost:3333] ");
00064         System.out.print("[-n source_name *FakeVideoSource] ");
00065         System.out.print("[-c channel_name *Duke] ");
00066         System.out.print("[-p pathname_for_pictures *./duke/Duke]");
00067         System.out.print("[-k picture_count *10]");
00068         System.out.print("[-t timer_interval *1000]");
00069         System.out.println();
00070     }
00071     
00072     public PictureSource(String[] args) {
00073         //parse args
00074         try {
00075             ArgHandler ah=new ArgHandler(args);
00076             if (ah.checkFlag('h')) {
00077                 printUsage();
00078                 RBNBProcess.exit(0);                
00079             }
00080             if (ah.checkFlag('s')) {
00081                 String a=ah.getOption('s');
00082                 if (a!=null) server=a;
00083             }
00084             if (ah.checkFlag('n')) {
00085                 String a=ah.getOption('n');
00086                 if (a!=null) sourceName=a;
00087             }
00088             if (ah.checkFlag('c')) {
00089                 String a=ah.getOption('c');
00090                 if (a!=null) channelName=a;
00091             }
00092             if (ah.checkFlag('p')) {
00093                 String a=ah.getOption('p');
00094                 if (a!=null) picturePath=a;
00095             }
00096             if (ah.checkFlag('k')) {
00097                 String a=ah.getOption('k');
00098                 try {
00099                     if (a!=null) pictureCount=Integer.parseInt(a);
00100                 } catch (Exception ignore) {}
00101             }
00102             if (ah.checkFlag('t')) {
00103                 String a=ah.getOption('t');
00104                 try {
00105                     if (a!=null) timerInterval=Long.parseLong(a);
00106                 } catch (Exception ignore) {}
00107             }
00108         } catch (Exception e) {
00109             System.err.println("PictureSource argument exception "+e.getMessage());
00110             e.printStackTrace();
00111             RBNBProcess.exit(0);
00112         }
00113         
00114         System.out.println("Starting PictureSource on " + server + " as " + sourceName);
00115         System.out.println("  Channel name = " + channelName + "; timer interval = " + timerInterval);
00116         System.out.println("  Using images " + picturePath + "01.jpg"
00117                 + " through " + picturePath 
00118                 + (pictureCount<10?("0" + pictureCount):("" + pictureCount)) + ".jpg");
00119         System.out.println("  Use PictureSource -h to see optional parameters");
00120     }
00121     
00122     public boolean loadImageFiles() {
00123         fileBuffer = new byte[10][];
00124         text = new String[10];
00125 
00126         current = 0;
00127         
00128         URL fig = null;
00129         try {
00130             for (int i = 1; i < (pictureCount + 1); i++ )
00131             {
00132                 text[i-1] = picturePath + ((i<10)?("0" + i):(""+i)) + ".jpg";
00133             }
00134             
00135             for (int i = 0; i < text.length; i++)
00136             {
00137                 File f = new File(text[i]);
00138                 FileInputStream in = new FileInputStream(f);
00139                 int fileLength = (int)f.length();
00140                 byte[] buffer = new byte[fileLength];
00141                 in.read(buffer);
00142                 fileBuffer[i] = buffer;
00143                 System.out.println("Read " + text[i] + " for " + fileLength + " bytes");
00144             }
00145     
00146             return true;
00147         }
00148         catch (Exception e)
00149         {
00150             System.out.println("Loading Image files failed at " + current + "; " + e);
00151             e.printStackTrace();
00152             return false;
00153         }
00154     }
00155 
00156     private byte[] next()
00157     {
00158         byte[] ret = null;
00159         int l = (int)fileBuffer.length;
00160         current = ( current + 1 ) % l;
00161         ret = fileBuffer[current];
00162         return ret;
00163     }
00164     
00165     private void exec()
00166     {
00167         readySource();
00168         if (source != null)
00169             startThread();
00170     }
00171     
00172     private void readySource() {
00173         source = new Source(30, "none", 0);
00174         try {
00175             source.OpenRBNBConnection(server, sourceName);
00176         } catch (SAPIException e) {
00177             System.err.println("Failed to connect to RBNB server.");
00178             source = null;
00179             return;
00180         }
00181 
00182         cmap = new ChannelMap();
00183         cmap.PutTimeAuto("timeofday");
00184         channelId = -1;
00185         try {
00186             channelId = cmap.Add(channelName);
00187         } catch (SAPIException e) {
00188             System.err.println("Failed to add video channel to channel map.");
00189             source.CloseRBNBConnection();
00190             source = null;
00191             cmap = null;
00192             return;
00193         }
00194         cmap.PutMime(channelId, "image/jpeg");
00195         System.out.println("ChannelId = " + channelId);
00196     } // readySource
00197 
00198     private void startThread() {
00199         
00200       // Use this inner class to hide the public run method
00201       Runnable r = new Runnable() {
00202         public void run() {
00203           runWork();
00204         }
00205       };
00206       timerThread = new Thread(r, "Timer");
00207       timerThread.start();
00208    }
00209 
00210     private void runWork ()
00211     {
00212         while(true)
00213         {
00214             if (!oneStep()) return;
00215             try {
00216                 Thread.sleep(1000);
00217             } catch (InterruptedException e) {
00218                 return;
00219             }
00220         }
00221     }
00222             
00223     private boolean oneStep()
00224     {
00225         try{
00226             byte[] imageData = next();
00227             cmap.PutDataAsByteArray(channelId, imageData);
00228             source.Flush(cmap);
00229             System.out.println("On channel " + channelId + ": sent image file " + text[current] + " for "
00230                 + imageData.length + " bytes");
00231         } catch (Exception e){
00232             System.err.println("Failed to send image " + current);
00233             e.printStackTrace();
00234             source.CloseRBNBConnection();
00235             source = null;
00236             cmap = null;            
00237             return false;
00238         }
00239         return true;
00240     }
00241     
00242     public static void main(String[] args) {
00243         PictureSource p = new PictureSource(args);
00244         if (p.loadImageFiles())
00245         {
00246             p.exec();
00247         }
00248     }
00249 }

Generated on Tue Mar 23 11:54:24 2004 for Data turbine for NEESGrid by doxygen 1.3.6