1// BEGIN LICENSE BLOCK 2// Version: CMPL 1.1 3// 4// The contents of this file are subject to the Cisco-style Mozilla Public 5// License Version 1.1 (the "License"); you may not use this file except 6// in compliance with the License. You may obtain a copy of the License 7// at www.eclipse-clp.org/license. 8// 9// Software distributed under the License is distributed on an "AS IS" 10// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 11// the License for the specific language governing rights and limitations 12// under the License. 13// 14// The Original Code is The ECLiPSe Constraint Logic Programming System. 15// The Initial Developer of the Original Code is Cisco Systems, Inc. 16// Portions created by the Initial Developer are 17// Copyright (C) 2006 Cisco Systems, Inc. All Rights Reserved. 18// 19// Contributor(s): 20// 21// END LICENSE BLOCK 22 23//Title: Eclipse Visualisation library 24//Version: $Id: RemoteVisClient.java,v 1.1 2006/09/23 01:56:34 snovello Exp $ 25//Copyright: Copyright (c) 2001 26//Author: Josh Singer 27//Company: Parc Technologies Ltd. 28//Description: Visualisation class which attaches to a primed remote eclipse 29package com.parctechnologies.eclipse.visualisation; 30 31import com.parctechnologies.eclipse.*; 32import java.net.*; 33import java.io.*; 34 35/** 36 * Class with command line main method. This allows connection to an Eclipse 37 * process remotely, and then sets up a VisClient based on this connection. The 38 * Eclipse has to have been primed using e.g. remote_connect/3. Also has 39 * specialised exitError and exitNormal methods for exiting: they have to also 40 * disconnect the EclipseConnection. 41 */ 42public class RemoteVisClient extends VisClient 43{ 44 private static final int MIN_CL_ARGS = 2; 45 private RemoteEclipse eclipse; 46 47 public RemoteVisClient(RemoteEclipse eclipse) throws IOException, EclipseException 48 { 49 super(eclipse.registerMultitask(null)); 50 this.eclipse = eclipse; 51 // Write client name out to stdout so that it can be read and returned by 52 // whatever called the command line. 53 EXDROutputStream exdr_out = new EXDROutputStream(System.out); 54 try 55 { 56 exdr_out.write(clientName); 57 exdr_out.flush(); 58 } 59 catch(IOException ioe){super.recover_ioe(ioe);} 60 } 61 62 // main method takes address and port as args, connects to a remote 63 // eclipse at that address/port and constructs/attaches an instance 64 // to it. 65 public static void main(String[] args) 66 { 67 checkargs(args); 68 RemoteEclipse eclipse = null; 69 try 70 { 71 eclipse = new RemoteEclipse(InetAddress.getByName(args[0]), 72 Integer.parseInt(args[1])); 73 } 74 catch(IOException ioe) 75 { 76 System.err.println("Unable to connect to ECLiPSe: "+ioe+" raised."); 77 System.exit(1); 78 } 79 try { 80 RemoteVisClient remoteVisClient1 = new RemoteVisClient(eclipse); 81 try { 82 eclipse.resume(); 83 } catch(IOException ioe) { 84 remoteVisClient1.recover_ioe(ioe); 85 } 86 } catch(Exception e) { 87 e.printStackTrace(); 88 } 89 } 90 91 private static void usage_message() 92 { 93 System.err.println("Usage:"); 94 System.err.println(" java RemoteVisClient <host> <port>"); 95 } 96 97 private static void checkargs(String[] args) 98 { 99 InetAddress host = null; 100 int port = 0; 101 102 if(args.length < MIN_CL_ARGS) 103 { 104 usage_message(); 105 System.exit(1); 106 } 107 108 try 109 { 110 host = InetAddress.getByName(args[0]); 111 } 112 catch(UnknownHostException uhe) 113 { 114 System.err.println("Can't reach host "+args[0]+"."); 115 System.exit(1); 116 } 117 118 try 119 { 120 port = Integer.parseInt(args[1]); 121 if(port <= 0) 122 { 123 System.err.println("Invalid port number "+args[1]+"."); 124 System.exit(1); 125 } 126 } 127 catch(NumberFormatException nfe) 128 { 129 System.err.println("Invalid port number "+args[1]+"."); 130 System.exit(1); 131 } 132 133 } 134 135 /** 136 * Tries to disconnect if possible, then exits JVM with error exit code. 137 */ 138 protected void exitError() 139 { 140 super.exitError(); 141 try 142 { 143 disconnect(); 144 } 145 catch(Exception e){} 146 System.exit(1); 147 } 148 149 /** 150 * Tries to disconnect if possible, and reports an error if this does not work 151 * then exits the JVM normal exit code. 152 */ 153 protected void exitNormal() 154 { 155 super.exitNormal(); 156 try 157 { 158 disconnect(); 159 } 160 catch(IOException ioe){recover_ioe(ioe);} 161 System.exit(0); 162 } 163 164 /** 165 * A unilateralDisconnect must be made if Eclipse has control because it will 166 * not be listening for a disconnect message. Otherwise, we can do a cleaner 167 * disconnect. 168 */ 169 void disconnect() throws IOException 170 { 171 if(stateModel.getEclipseHasControl()) 172 { 173 eclipse.unilateralDisconnect(); 174 } 175 else 176 { 177 eclipse.disconnect(); 178 } 179 } 180} 181