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