001package cnslab.gui; 002import java.io.*; 003import jpvm.*; 004 005/** 006 * Graphic console for JPVM 007 * 008 * @author Yi Dong 009 */ 010public class GConsole implements Runnable{ 011 012 public jpvmEnvironment jpvm; 013 public BufferedReader user; 014 015 016 public GConsole(){ 017 018 } 019 020 /** 021 * 022 * Test whether the background daemon is started or not 023 * @return boolean 024 */ 025 public boolean testDaemon() 026 { 027 028 try{ 029 jpvm = new jpvmEnvironment("jpvm console"); 030 } 031 catch (jpvmException jpe) { 032 // perror("internal jpvm error - "+jpe.toString()); 033 jpvm=null; 034 return false; 035 } 036 return true; 037 } 038 /** 039 * @see java.lang.Runnable#run() run 040 */ 041 public void run() { 042 043 } 044 045 /** 046 * 047 * stop the jpvm 048 * 049 * @throws jpvmException 050 */ 051 public void Quit() throws jpvmException { 052 //String tmp=""; 053 //tmp=tmp+"jpvmd still running."; 054 jpvm.pvm_exit(); 055 System.exit(0); 056 } 057 058 /** 059 * 060 * 061 * @return help information 062 * 063 * @throws jpvmException 064 */ 065 public String Help() throws jpvmException { 066 String tmp=""; 067 tmp=tmp+"Commands are:"; 068 tmp=tmp+" add\t- Add a host to the virtual "+ 069 "machine"; 070 tmp=tmp+" delhost\t- delete a host from the virtual "+ "machine"; 071 tmp=tmp+" delallhost\t- delete all the hosts from the virtual machine"; 072 tmp=tmp+" halt\t- Stop jpvm daemons"; 073 tmp=tmp+" help\t- Print helpful information " + 074 "about commands"; 075 tmp=tmp+" ps\t- List tasks"; 076 tmp=tmp+" quit\t- Exit console"; 077 tmp=tmp+" addall\t- Add hosts from file myhosts"; 078 tmp=tmp+" del\t- Delete task lists"; 079 return tmp; 080 } 081 082 /** 083 * 084 * 085 * @return show configuration information 086 * 087 * @throws jpvmException 088 */ 089 public String Conf() throws jpvmException { 090 String tmp=""; 091 jpvmConfiguration conf = jpvm.pvm_config(); 092 tmp=tmp+""+conf.numHosts+" hosts:\n"; 093 for(int i=0;i<conf.numHosts;i++) 094 tmp=tmp+"\t"+conf.hostNames[i]+"\n"; 095 return tmp; 096 } 097 098 /** 099 * 100 * 101 * @return process information 102 * 103 * @throws jpvmException 104 */ 105 public String Ps() throws jpvmException { 106 String tmp=""; 107 jpvmConfiguration conf = jpvm.pvm_config(); 108 for(int i=0;i<conf.numHosts;i++) { 109 jpvmTaskStatus ps = jpvm.pvm_tasks(conf,i); 110 tmp=tmp+ps.hostName+", "+ps.numTasks+ 111 " tasks:\n"; 112 for(int j=0;j<ps.numTasks;j++) 113 tmp=tmp+"\t"+ps.taskNames[j]+"\n"; 114 } 115 return tmp; 116 } 117 118 /** 119 * 120 * Delete all the hosts from the JPVM 121 * @return 122 * 123 * @throws jpvmException 124 */ 125 public String DelAllHosts() throws jpvmException { 126 jpvmConfiguration conf = jpvm.pvm_config(); 127 for(int i=0;i<conf.numHosts;i++) { 128 jpvmTaskStatus ps = jpvm.pvm_tasks(conf,i); 129 try { 130 jpvm.pvm_delhosts(ps.hostName); 131 } 132 catch (jpvmException jpe) { 133 return perror("error - couldn't del host " + ps.hostName); 134 } 135 } 136 return new String("success!\n"); 137 } 138 139 /** 140 * 141 * stop all the current process. 142 * 143 * @throws jpvmException 144 */ 145 public void Halt() throws jpvmException { 146 jpvm.pvm_halt(); 147 /* 148 try { 149// Thread.sleep(2000); 150 } 151 catch (InterruptedException ie) { 152 } 153// System.exit(0); 154 */ 155 } 156 157 /** 158 * 159 * delete one task 160 * @return 161 * 162 * @throws jpvmException 163 */ 164 public String Delete() throws jpvmException { 165 jpvm.pvm_deleteTasks(); 166 try { 167 Thread.sleep(100); 168 } 169 catch (InterruptedException ie) { 170 } 171 return "success!\n"; 172 } 173 174 175 /** 176 * Add host 177 * 178 * @return 179 */ 180 public String Add() { 181 String tmp=""; 182 String host = null; 183 int port = 0; 184 try { 185 tmp=tmp+"\tHost name : "; 186 System.out.flush(); 187 host = user.readLine(); 188 tmp=tmp+"\tPort number : "; 189 System.out.flush(); 190 String port_str = user.readLine(); 191 try { 192 port = Integer.valueOf(port_str).intValue(); 193 } 194 catch (NumberFormatException nfe) { 195 tmp=tmp+"Bad port."; 196 return tmp; 197 } 198 } 199 catch (IOException e) { 200 tmp=tmp+"i/o exception"; 201 try { 202 Quit(); 203 } 204 catch (jpvmException jpe) { 205 System.exit(0); 206 } 207 return tmp; 208 } 209 jpvmTaskId tid = new jpvmTaskId(host,port); 210 String h[] = new String[1]; 211 jpvmTaskId t[] = new jpvmTaskId[1]; 212 h[0] = host; 213 t[0] = tid; 214 try { 215 jpvm.pvm_addhosts(1,h,t); 216 } 217 catch (jpvmException jpe) { 218 return perror("error - couldn't add host " + host); 219 } 220 return tmp; 221 } 222 223 public String Add(String host,int port) { 224 String tmp="Adding host:"+host+" listening at:"+port+"\n"; 225 jpvmTaskId tid = new jpvmTaskId(host,port); 226 String h[] = new String[1]; 227 jpvmTaskId t[] = new jpvmTaskId[1]; 228 h[0] = host; 229 t[0] = tid; 230 try { 231 jpvm.pvm_addhosts(1,h,t); 232 } 233 catch (jpvmException jpe) { 234 return perror("error - couldn't add host " + host); 235 } 236 return tmp+"success!\n"; 237 } 238 239 public String DelHost() { 240 String tmp=""; 241 String host = null; 242 int port = 0; 243 try { 244 tmp=tmp+"\tHost name : "; 245 host = user.readLine(); 246 } 247 catch (IOException e) { 248 tmp=tmp+"i/o exception"; 249 try { 250 Quit(); 251 return tmp; 252 } 253 catch (jpvmException jpe) { 254 System.exit(0); 255 } 256 return tmp; 257 } 258 try { 259 jpvm.pvm_delhosts(host); 260 } 261 catch (jpvmException jpe) { 262 return perror("error - couldn't del host " + host+"\n"); 263 } 264 return tmp; 265 } 266 267 public String DelHost(String host) { 268 String tmp="Deleteing "+host+"\n"; 269 try { 270 jpvm.pvm_delhosts(host); 271 } 272 catch (jpvmException jpe) { 273 return perror("error - couldn't del host " + host+"\n"); 274 } 275 return tmp+"success!\n"; 276 } 277 278 /** 279 * Add host from file myhosts 280 * 281 * @return 282 */ 283 public String AddAll() { 284 String tmp=""; 285 String host = null; 286 int port = 0; 287 try{ 288 FileReader input = new FileReader("myhosts"); 289 BufferedReader bufRead = new BufferedReader(input); 290 String line; // String that holds current 291 int count = 0; // Line number of count 292 line = bufRead.readLine(); 293 count++; 294 while (line != null){ 295 if(line.startsWith("#"))continue; // "#"is the comments line 296 //tmp=tmp+count+": "+line); 297 String [] tmp1 = null; 298 //if(line ==null ) break; 299 tmp1=line.split(" ++"); 300 if(tmp1.length<2) return perror("myhosts file error -format: hosts port\n"); 301 host = tmp1[0]; 302 try{ 303 port = Integer.valueOf(tmp1[1]).intValue(); 304 } catch (NumberFormatException nfe) { 305 tmp=tmp+"Bad port.\n"; 306 return tmp; 307 } 308 tmp=tmp+"Adding host: "+tmp1[0]+" port: "+tmp1[1]+"\n"; 309 310 jpvmTaskId tid = new jpvmTaskId(host,port); 311 String h[] = new String[1]; 312 jpvmTaskId t[] = new jpvmTaskId[1]; 313 h[0] = host; 314 t[0] = tid; 315 try { 316 jpvm.pvm_addhosts(1,h,t); 317 } 318 catch (jpvmException jpe) { 319 return perror("error - couldn't add host " + host+"\n"); 320 } 321 322 count++; 323 line = bufRead.readLine(); 324 } 325 bufRead.close(); 326 }catch (IOException e){ 327 tmp=tmp+"You need to prepare myhosts file in format:"; 328 tmp=tmp+"Hostsname Port"; 329// e.printStackTrace(); 330 return tmp; 331 } 332 return tmp+"done\n"; 333 } 334 335 public String perror(String message) { 336 String tmp=""; 337 tmp=tmp+"jpvm console: "+ message; 338 return tmp; 339 } 340 341}