Ignore:
Timestamp:
06/23/2016 06:30:20 PM (10 years ago)
Author:
jdalbey
Message:

MultiFile? commit: Add CADViewer Interface and change CADSimulator to Observer Pattern.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/tmcsim/cadsimulator/stillimagecontrol/ATMSCommunicator.java

    r2 r44  
    77 
    88/** 
    9  * ATMSCommunicator handles communication between the CAD Simulator and the  
    10  * ATMS Server.  The funcationality provided includes querying the current time  
    11  * on the ATMS server and replacing images on the ATMS to model  
    12  * traffic flow changes.  The ATMSCommunicator uses the plink.exe external  
    13  * application to establish an SSH communication with the ATMS server, which is 
    14  * used to execute commands remotely.   
    15  *  
     9 * ATMSCommunicator handles communication between the CAD Simulator and the ATMS 
     10 * Server. The funcationality provided includes querying the current time on the 
     11 * ATMS server and replacing images on the ATMS to model traffic flow changes. 
     12 * The ATMSCommunicator uses the plink.exe external application to establish an 
     13 * SSH communication with the ATMS server, which is used to execute commands 
     14 * remotely. 
     15 * 
    1616 * @author Matthew Cechini 
    1717 * @version 
    1818 */ 
    19 public class ATMSCommunicator { 
    20      
    21     /** Connection user name. */ 
     19public class ATMSCommunicator 
     20{ 
     21 
     22    /** 
     23     * Connection user name. 
     24     */ 
    2225    protected String username; 
    23      
    24     /** Connection password. */ 
     26    /** 
     27     * Connection password. 
     28     */ 
    2529    protected String password; 
    26      
    27     /** ATMS Server Host name. */ 
     30    /** 
     31     * ATMS Server Host name. 
     32     */ 
    2833    protected String viewerHost; 
    29      
    30     /** Absolute directory path for images. */ 
     34    /** 
     35     * Absolute directory path for images. 
     36     */ 
    3137    protected String image_dir; 
    32      
    33     /** Base plink command string. */ 
     38    /** 
     39     * Base plink command string. 
     40     */ 
    3441    protected String plinkBaseCMD; 
    35      
    36     /** Constructor. */ 
    37     public ATMSCommunicator(String host, String user, String pwd, String dir) { 
     42    protected String remoteShellCmd; 
     43 
     44    /** 
     45     * Constructor. 
     46     */ 
     47    public ATMSCommunicator(String host, String user, String pwd, String dir) 
     48    { 
    3849        viewerHost = host; 
    39         username   = user; 
    40         password   = pwd; 
    41         image_dir  = dir; 
    42          
    43         plinkBaseCMD = "plink -l " + username + " -pw " + password + " " + viewerHost + " ";         
     50        username = user; 
     51        password = pwd; 
     52        image_dir = dir; 
     53        String osname = System.getProperty("os.name"); 
     54        if (osname.startsWith("Windows")) 
     55        { 
     56            remoteShellCmd = "plink -l " + username + " -pw " + password + " " + viewerHost + " \"date\""; 
     57        } 
     58        else 
     59        { 
     60            remoteShellCmd = "date"; //sshpass -p " + password + " " + "ssh -t -l " + username + viewerHost + " date &"; 
     61        } 
    4462    } 
    45      
     63 
    4664    /** 
    47      * Get the current ATMS server time as the number of seconds since Jan 1, 1970. 
    48      *  
     65     * Get the current ATMS server time as the number of seconds since Jan 1, 
     66     * 1970. 
     67     * 
    4968     * @return Current time in seconds. 
    5069     * @throws RemoteException If there is an exception in RMI communication. 
    51      */  
    52     public long getCurrentTime() throws Exception { 
    53          
     70     */ 
     71    public long getCurrentTime() throws Exception 
     72    { 
     73 
    5474        Calendar currentCal = Calendar.getInstance(); 
    5575 
    56         Process timeProc = Runtime.getRuntime().exec(plinkBaseCMD + " \"date\""); 
     76        Process timeProc = Runtime.getRuntime().exec(remoteShellCmd); 
    5777        timeProc.waitFor(); 
    58          
    59         if(timeProc.exitValue() == 0) { 
     78//        System.out.println("timeProc exit = " + timeProc.exitValue()); 
     79//        byte[] procBytes = new byte[timeProc.getInputStream().available()]; 
     80//        timeProc.getInputStream().read(procBytes); 
     81//        String returnString = new String(procBytes); 
     82//        System.out.println("timeProc returned: " + returnString); 
     83 
     84        if (timeProc.exitValue() == 0) 
     85        { 
    6086            String tempToken = null; 
    6187            byte[] dateBytes = new byte[timeProc.getInputStream().available()]; 
    6288            timeProc.getInputStream().read(dateBytes); 
    63              
     89 
    6490            StringTokenizer spaceTok = new StringTokenizer(new String(dateBytes), " "); 
    65             while(spaceTok.hasMoreTokens()) { 
     91            while (spaceTok.hasMoreTokens()) 
     92            { 
    6693                tempToken = spaceTok.nextToken(); 
    67                  
    68                 if(tempToken.indexOf(":") != -1) { 
     94 
     95                if (tempToken.indexOf(":") != -1) 
     96                { 
    6997                    StringTokenizer colonTok = new StringTokenizer(new String(tempToken), ":"); 
    70                      
     98 
    7199                    currentCal.set(Calendar.HOUR, Integer.parseInt(colonTok.nextToken())); 
    72100                    currentCal.set(Calendar.MINUTE, Integer.parseInt(colonTok.nextToken())); 
    73101                    currentCal.set(Calendar.SECOND, Integer.parseInt(colonTok.nextToken())); 
    74                      
    75                     System.out.println("Time retreieved from ATRMS server: " +  
    76                             DateFormat.getDateTimeInstance().format(currentCal.getTime()));                                  
    77                      
     102 
     103                    System.out.println("Time retreieved from ATRMS server: " 
     104                            + DateFormat.getDateTimeInstance().format(currentCal.getTime())); 
     105 
    78106                } 
    79107            } 
    80         }        
    81          
    82         return currentCal.getTimeInMillis();         
     108        } 
     109 
     110        return currentCal.getTimeInMillis(); 
    83111 
    84112    } 
    85113 
    86114    /** 
    87      * Show a new image for an ATMS camera.  The ATMS camera files are named 
    88      * <ATMS_Camera_ID>.xpm.  If a camera file exists, delete it.  Then copy 
    89      * the parameter file name to <ATMS_Camera_ID>.xpm.  If the camera ID or  
    90      * new file does not exist, throw an exception.      
    91      *  
     115     * Show a new image for an ATMS camera. The ATMS camera files are named 
     116     * <ATMS_Camera_ID>.xpm. If a camera file exists, delete it. Then copy the 
     117     * parameter file name to <ATMS_Camera_ID>.xpm. If the camera ID or new file 
     118     * does not exist, throw an exception. 
     119     * 
    92120     * @param ATMS_cameraID ATMS indexed camera ID. 
    93121     * @param fileName Filename to show. 
    94122     * @throws RemoteException If there is an exception in RMI communication. 
    95      */  
    96     public void showImage(Integer ATMS_cameraID, String fileName) throws RemoteException { 
    97          
     123     */ 
     124    public void showImage(Integer ATMS_cameraID, String fileName) throws RemoteException 
     125    { 
     126 
    98127        System.out.println("Showing CameraID " + ATMS_cameraID + ", with filename: " + fileName); 
    99128        /* 
    100         Process imageProc = Runtime.getRuntime().exec(plinkBaseCMD + "\"ls " + image_dir + "/cctvImage" + ATMS_cameraID + ".xpm\""); 
    101         imageProc.waitFor(); 
    102          
    103         if(imageProc.exitValue() != 0) { 
    104             throw new Exception(image_dir + "/cctvImage" + ATMS_cameraID + ".xpm does not exist"); 
    105         } 
    106              
    107         imageProc = Runtime.getRuntime().exec(plinkBaseCMD + "\"ls " + image_dir + "/" + fileName + ".xpm\""); 
    108         imageProc.waitFor(); 
     129         Process imageProc = Runtime.getRuntime().exec(plinkBaseCMD + "\"ls " + image_dir + "/cctvImage" + ATMS_cameraID + ".xpm\""); 
     130         imageProc.waitFor(); 
    109131 
    110         if(imageProc.exitValue() != 0) { 
    111             throw new Exception(image_dir + "/" + fileName + ".xpm does not exist"); 
    112         } 
    113      
    114         imageProc = Runtime.getRuntime().exec(plinkBaseCMD + "\"rm " + image_dir + "/cctvImage" + ATMS_cameraID + ".xpm\""); 
    115         imageProc.waitFor(); 
    116          
    117         imageProc = Runtime.getRuntime().exec(plinkBaseCMD + "\"cp " + image_dir + "/" + fileName + ".xpm" +  
    118                                   "   " + image_dir + "/" + "cctvImage" + ATMS_cameraID + ".xpm\"");         
    119         imageProc.waitFor(); 
    120         */ 
    121          
    122     }    
     132         if(imageProc.exitValue() != 0) { 
     133         throw new Exception(image_dir + "/cctvImage" + ATMS_cameraID + ".xpm does not exist"); 
     134         } 
     135 
     136         imageProc = Runtime.getRuntime().exec(plinkBaseCMD + "\"ls " + image_dir + "/" + fileName + ".xpm\""); 
     137         imageProc.waitFor(); 
     138 
     139         if(imageProc.exitValue() != 0) { 
     140         throw new Exception(image_dir + "/" + fileName + ".xpm does not exist"); 
     141         } 
     142 
     143         imageProc = Runtime.getRuntime().exec(plinkBaseCMD + "\"rm " + image_dir + "/cctvImage" + ATMS_cameraID + ".xpm\""); 
     144         imageProc.waitFor(); 
     145 
     146         imageProc = Runtime.getRuntime().exec(plinkBaseCMD + "\"cp " + image_dir + "/" + fileName + ".xpm" + 
     147         "   " + image_dir + "/" + "cctvImage" + ATMS_cameraID + ".xpm\""); 
     148         imageProc.waitFor(); 
     149         */ 
     150 
     151    } 
    123152} 
Note: See TracChangeset for help on using the changeset viewer.