001/*
002 * This file is part of McIDAS-V
003 *
004 * Copyright 2007-2015
005 * Space Science and Engineering Center (SSEC)
006 * University of Wisconsin - Madison
007 * 1225 W. Dayton Street, Madison, WI 53706, USA
008 * https://www.ssec.wisc.edu/mcidas
009 * 
010 * All Rights Reserved
011 * 
012 * McIDAS-V is built on Unidata's IDV and SSEC's VisAD libraries, and
013 * some McIDAS-V source code is based on IDV and VisAD source code.  
014 * 
015 * McIDAS-V is free software; you can redistribute it and/or modify
016 * it under the terms of the GNU Lesser Public License as published by
017 * the Free Software Foundation; either version 3 of the License, or
018 * (at your option) any later version.
019 * 
020 * McIDAS-V is distributed in the hope that it will be useful,
021 * but WITHOUT ANY WARRANTY; without even the implied warranty of
022 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
023 * GNU Lesser Public License for more details.
024 * 
025 * You should have received a copy of the GNU Lesser Public License
026 * along with this program.  If not, see http://www.gnu.org/licenses.
027 */
028
029package edu.wisc.ssec.mcidasv.data;
030
031import java.io.BufferedInputStream;
032import java.io.BufferedReader;
033import java.io.DataInputStream;
034import java.io.InputStream;
035import java.io.InputStreamReader;
036import java.net.URL;
037import java.net.URLConnection;
038import java.util.ArrayList;
039import java.util.List;
040import java.util.StringTokenizer;
041
042/**
043 * Class McIdasXInfo Holds the state of the McIDAS-X session
044 * on the other end of the bridge
045 */
046public class McIdasXInfo {
047
048    /** Conduit protocol info */
049    private String versionString = "2";
050    private String hostString = "localhost";
051    private String portString = "8080";
052    private String keyString = "00000000000000000000000000000000";
053    
054    /**
055     * Constructor
056     */
057    public McIdasXInfo() {}
058
059    /**
060     * Copy constructor
061     *
062     * @param that The McIdasXInfo to copy
063     *
064     */
065    public McIdasXInfo(McIdasXInfo that) {
066        this.hostString = that.hostString;
067        this.portString = that.portString;
068        this.keyString  = that.keyString;
069    }
070
071    /**
072     * Constructor
073     */
074    public McIdasXInfo(String host, String port, String key) {
075        this.hostString = host;
076        this.portString = port;
077        this.keyString  = key;
078    }
079
080    /**
081     * Get the hostString property.
082     *
083     * @return The hostString property.
084     */
085    public String getHostString() {
086        return this.hostString;
087    }
088
089    /**
090     * Get the portString property.
091     *
092     * @return The portString property.
093     */
094    public String getPortString() {
095        return this.portString;
096    }
097
098    /**
099     * Get the keyString property.
100     *
101     * @return The keyString property.
102     */
103    public String getKeyString() {
104        return this.keyString;
105    }
106
107    /**
108     * Set the hostString property.
109     *
110     * @param newValue The new vaue for the hostString property.
111     */
112    public void setHostString(String newValue) {
113        this.hostString = newValue;
114    }
115
116    /**
117     * Set the portString property.
118     *
119     * @param newValue The new vaue for the portString property.
120     */
121    public void setPortString(String newValue) {
122        this.portString = newValue;
123    }
124
125    /**
126     * Set the keyString property.
127     *
128     * @param newValue The new vaue for the keyString property.
129     */
130    public void setKeyString(String newValue) {
131        this.keyString = newValue;
132    }
133    
134    /**
135     * Get the frame request string.
136     *
137     * @return The frame request string.
138     */
139    private String getFrameRequest() {
140        return "http://" + hostString  + ":" + portString + "/?sessionkey=" + keyString +
141                                "&version=" + versionString + "&frame=0&x=0&y=0&type=V";
142    }
143    
144    /**
145     * Get the frames request string.
146     *
147     * @return The frames request string.
148     */
149    private String getFramesRequest() {
150        return "http://" + hostString  + ":" + portString + "/?sessionkey=" + keyString +
151                                "&version=" + versionString + "&frame=0&x=0&y=0&type=U";
152    }
153    
154    /**
155     * Get the file request string.
156     *
157     * @return The file request string.
158     */
159    private String getFileRequest(String filename) {
160        return "http://" + hostString  + ":" + portString + "/?sessionkey=" + keyString +
161                                "&version=" + versionString + "&frame=0&x=0&y=0&type=F&text=" + filename;
162    }
163    
164    /**
165     * Get the file request DataInputStream.
166     *
167     * @return The file request DataInputStream.
168     */
169    public DataInputStream getFileInputStream(String filename) {
170        return getXInputStream(getFileRequest(filename));
171    }
172    
173    /**
174     * Get the data request string.
175     *
176     * @return The data request string.
177     */
178    private String getDataRequest(Integer frame) {
179        if (frame < 1) frame = getCurrentFrame();
180        return "http://" + hostString  + ":" + portString + "/?sessionkey=" + keyString +
181                                "&version=" + versionString + "&frame=0&x=0&y=0&type=D&text=" + frame;
182    }
183    
184    /**
185     * Get the data request DataInputStream.
186     *
187     * @return The data request DataInputStream.
188     */
189    public DataInputStream getDataInputStream(Integer frame) {
190        if (frame < 1) frame = getCurrentFrame();
191        return getXInputStream(getDataRequest(frame));
192    }
193    
194    /**
195     * Get the graphics request string.
196     *
197     * @return The graphics request string.
198     */
199    private String getGraphicsRequest(Integer frame) {
200        if (frame < 1) frame = getCurrentFrame();
201        return "http://" + hostString  + ":" + portString + "/?sessionkey=" + keyString +
202                                "&version=" + versionString + "&frame=0&x=0&y=0&type=P&text=" + frame;
203    }
204    
205    /**
206     * Get the graphics request DataInputStream.
207     *
208     * @return The graphics request DataInputStream.
209     */
210    public DataInputStream getGraphicsInputStream(Integer frame) {
211        return getXInputStream(getGraphicsRequest(frame));
212    }
213    
214    /**
215     * Get the command request string.
216     *
217     * @return The command request string.
218     */
219    private String getCommandRequest(String commandLine) {
220        return "http://" + hostString  + ":" + portString + "/?sessionkey=" + keyString +
221                                "&version=" + versionString + "&frame=0&x=0&y=0&type=T&text=" + commandLine;
222    }
223    
224    /**
225     * Get the command request string.
226     *
227     * @return The command request string.
228     */
229    private String getCommandRequest(String commandLine, int frame) {
230        return "http://" + hostString  + ":" + portString + "/?sessionkey=" + keyString +
231                                "&version=" + versionString + "&frame=" + frame + "&x=0&y=0&type=T&text=" + commandLine;
232    }
233    
234    /**
235     * Get the command request DataInputStream.
236     *
237     * @return The command request DataInputStream.
238     */
239    public DataInputStream getCommandInputStream(String commandLine) {
240        return getXInputStream(getCommandRequest(commandLine));
241    }
242    
243    /**
244     * Get the command request DataInputStream.
245     *
246     * @return The command request DataInputStream.
247     */
248    public DataInputStream getCommandInputStream(String commandLine, int frame) {
249        return getXInputStream(getCommandRequest(commandLine, frame));
250    }
251    
252    /**
253     * Get the GIF request string.
254     *
255     * @return The GIF request string.
256     */
257    private String getGIFRequest(Integer frame) {
258        return "http://" + hostString  + ":" + portString + "/?sessionkey=" + keyString +
259                                "&version=" + versionString + "&frame=0&x=0&y=0&type=C&text=" + frame;
260    }
261    
262    /**
263     * Get the GIF request DataInputStream.
264     *
265     * @return The GIF request DataInputStream.
266     */
267    public DataInputStream getGIFInputStream(Integer frame) {
268        return getXInputStream(getGIFRequest(frame));
269    }
270
271    /**
272     * Get a String representation of this object
273     * 
274     * @return a string representation
275     */
276    public String toString() {
277        StringBuffer buf = new StringBuffer();
278        buf.append("McIdasXInfo as string: ");
279        buf.append("hostString=");
280        buf.append(this.hostString);
281        buf.append(", portString=");
282        buf.append(this.portString);
283        buf.append(", keyString=");
284        buf.append(this.keyString);
285        return buf.toString();
286    }
287
288    /**
289     * Get a DataInputStream from a given request String.
290     * 
291     * @return a DataInputStream
292     */
293    private DataInputStream getXInputStream(String newRequest) {
294        URL url;
295        URLConnection urlc;
296        DataInputStream retStream = null;
297        try {
298            url = new URL(newRequest);
299            urlc = url.openConnection();
300            InputStream is = urlc.getInputStream();
301            retStream = new DataInputStream(new BufferedInputStream(is));
302        } catch (Exception e) {
303                return retStream;
304        }
305        return retStream;
306    }
307    
308    /**
309     * Get the current frame in McIDAS-X.
310     * 
311     * @return The current frame in McIDAS-X.
312     */
313    public int getCurrentFrame() {
314        int ret = -1;
315        DataInputStream inputStream = getXInputStream(getFrameRequest());
316        try {
317                BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
318                // Burn key line
319                String lineOut = br.readLine();
320                lineOut = br.readLine();
321                StringTokenizer tok = new StringTokenizer(lineOut, " ");
322            String responseType = tok.nextToken();
323            if (!responseType.equals("V")) {
324                System.out.println("getCurrentFrame unexpected responseType: " + responseType);
325                try { inputStream.close(); }
326                catch (Exception ee) {}
327                return ret;
328            }
329            ret = Integer.parseInt(tok.nextToken());
330            } catch (Exception e) {
331                System.out.println("getCurrentFrame exception: " + e);
332            try { inputStream.close(); }
333            catch (Exception ee) {}
334                return ret;
335            }
336        System.out.println("getCurrentFrame: " + ret);
337            return ret;
338    }
339    
340    /**
341     * Get the number of frames in McIDAS-X.
342     * 
343     * @return The number of frames in McIDAS-X.
344     */
345    public int getNumberOfFrames() {
346        int ret = -1;
347        String token = null;
348        DataInputStream inputStream = getXInputStream(getFrameRequest());
349        try {
350                BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
351                // Burn key line
352                String lineOut = br.readLine();
353                lineOut = br.readLine();
354                StringTokenizer tok = new StringTokenizer(lineOut, " ");
355            String responseType = tok.nextToken();
356            if (!responseType.equals("V")) {
357                System.out.println("getNumberOfFrames unexpected responseType: " + responseType);
358                try { inputStream.close(); }
359                catch (Exception ee) {}
360                return ret;
361            }
362            token = tok.nextToken();
363            token = tok.nextToken();
364            ret = Integer.parseInt(token.substring(1,4));
365            } catch (Exception e) {
366                System.out.println("getNumberOfFrames exception: " + e);
367            try { inputStream.close(); }
368            catch (Exception ee) {}
369                return ret;
370            }
371        System.out.println("getNumberOfFrames: " + ret);
372            return ret;
373    }
374    
375    /**
376     * Get the list of frame numbers in McIDAS-X.
377     * 
378     * @return The list of frame numbers in McIDAS-X.
379     */
380    public List getFrameNumbers() {
381        List frameNumbers = new ArrayList();
382        DataInputStream inputStream = getXInputStream(getFramesRequest());
383        if (inputStream == null) return frameNumbers;
384        String responseType = null;
385        StringTokenizer tok;
386        try {
387                BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
388                // Burn key line
389                String lineOut = br.readLine();
390                lineOut = br.readLine();
391                while (lineOut != null) {
392                        tok = new StringTokenizer(lineOut, " ");
393                    responseType = tok.nextToken();
394                    if (!responseType.equals("U")) {
395                        System.out.println("getFrameNumbers unexpected responseType: " + responseType);
396                        try { inputStream.close(); }
397                        catch (Exception ee) {}
398                        return frameNumbers;
399                    }
400                    Integer frameInt = Integer.parseInt(tok.nextToken());
401                    frameNumbers.add(frameInt);
402                    lineOut = br.readLine();
403                }
404            } catch (Exception e) {
405                System.out.println("getFrameNumbers exception: " + e);
406            try { inputStream.close(); }
407            catch (Exception ee) {}
408                return frameNumbers;
409            }
410//        System.out.println("getFrameNumbers: " + frameNumbers);
411            return frameNumbers;
412    }
413    
414}