001/* jpvmConsole.java
002 *
003 * A simple command line console for interacting with the local
004 * jpvm daemon.
005 *
006 * Adam J Ferrari
007 * Mon 05-27-1996
008 *
009 * Copyright (C) 1996  Adam J Ferrari
010 * 
011 * This library is free software; you can redistribute it and/or
012 * modify it under the terms of the GNU Library General Public
013 * License as published by the Free Software Foundation; either
014 * version 2 of the License, or (at your option) any later version.
015 * 
016 * This library is distributed in the hope that it will be useful,
017 * but WITHOUT ANY WARRANTY; without even the implied warranty of
018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
019 * Library General Public License for more details.
020 * 
021 * You should have received a copy of the GNU Library General Public
022 * License along with this library; if not, write to the
023 * Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
024 * MA 02139, USA.
025 */
026
027package jpvm;
028import  jpvm.jpvmEnvironment;
029import  jpvm.jpvmDaemonMessageTag;
030import  java.io.*;
031
032public 
033class jpvmConsole {
034        private static jpvmEnvironment  jpvm;
035        public static BufferedReader    user;
036
037        public static void main(String args[]) {
038            try {
039                jpvm  = new jpvmEnvironment("jpvm console");
040                InputStreamReader userIn = new InputStreamReader(System.in);
041                user = new BufferedReader(userIn);
042                while(true) {
043                        System.out.print("jpvm> ");
044                        try {
045                                System.out.flush();
046                                String command = user.readLine();
047                                if(command.equalsIgnoreCase("quit") ||
048                                   command.equalsIgnoreCase("q") ) {
049                                        Quit();
050                                }
051                                else if (command.equalsIgnoreCase("help") ||
052                                         command.equals("?")) {
053                                         Help();
054                                }
055                                else if (command.equalsIgnoreCase("conf")) {
056                                        Conf();
057                                }
058                                else if (command.equalsIgnoreCase("halt")) {
059                                        Halt();
060                                }
061                                else if (command.equalsIgnoreCase("add")) {
062                                        Add();
063                                }
064                                else if (command.equalsIgnoreCase("delhost")) {
065                                        DelHost();
066                                }
067                                else if (command.equalsIgnoreCase("delallhost")) {
068                                        DelAllHosts();
069                                }
070                                else if (command.equalsIgnoreCase("addall")) {
071                                        AddAll();
072                                }
073                                else if (command.equalsIgnoreCase("ps")) {
074                                        Ps();
075                                }
076                                else if (command.equalsIgnoreCase("del")) {
077                                        Delete();
078                                }
079                                else {
080                                        System.out.println(command+
081                                                ": not found");
082                                }
083                        }
084                        catch (IOException ioe) {
085                                System.err.println("jpvm console: i/o " +
086                                        "exception.");
087                                System.exit(1);
088                        }
089                }
090            }
091            catch (jpvmException jpe) {
092                perror("internal jpvm error - "+jpe.toString());
093            }
094        }
095
096        private static void Quit() throws jpvmException {
097                System.out.println("jpvm still running.");
098                jpvm.pvm_exit();
099                System.exit(0);
100        }
101
102        private static void Help() throws jpvmException {
103                System.out.println("Commands are:");
104                System.out.println("  add\t- Add a host to the virtual "+
105                                        "machine");
106                System.out.println("  delhost\t- delete a host from the virtual "+ "machine");
107                System.out.println("  delallhost\t- delete all the hosts from the virtual machine");
108                System.out.println("  halt\t- Stop jpvm daemons");
109                System.out.println("  help\t- Print helpful information " +
110                                        "about commands");
111                System.out.println("  ps\t- List tasks");
112                System.out.println("  quit\t- Exit console");
113                System.out.println("  addall\t- Add hosts from file myhosts");
114                System.out.println("  del\t- Delete task lists");
115        }
116
117        private static void Conf() throws jpvmException {
118                jpvmConfiguration conf = jpvm.pvm_config();
119                System.out.println(""+conf.numHosts+" hosts:");
120                for(int i=0;i<conf.numHosts;i++)
121                        System.out.println("\t"+conf.hostNames[i]);
122        }
123
124        private static void Ps() throws jpvmException {
125                jpvmConfiguration conf = jpvm.pvm_config();
126                for(int i=0;i<conf.numHosts;i++) {
127                        jpvmTaskStatus ps = jpvm.pvm_tasks(conf,i);
128                        System.out.println(ps.hostName+", "+ps.numTasks+
129                                " tasks:");
130                        for(int j=0;j<ps.numTasks;j++)
131                            System.out.println("\t"+ps.taskNames[j]);
132                }
133        }
134
135        private static void DelAllHosts() throws jpvmException {
136                jpvmConfiguration conf = jpvm.pvm_config();
137                for(int i=0;i<conf.numHosts;i++) {
138                        jpvmTaskStatus ps = jpvm.pvm_tasks(conf,i);
139                        try {
140                                jpvm.pvm_delhosts(ps.hostName);
141                        }
142                        catch (jpvmException jpe) {
143                                perror("error - couldn't del host " + ps.hostName);
144                        }
145                }
146        }
147
148        private static void Halt() throws jpvmException {
149                jpvm.pvm_halt();
150                try {
151                        Thread.sleep(2000);
152                }
153                catch (InterruptedException ie) {
154                }
155                System.exit(0);
156        }
157
158        private static void Delete() throws jpvmException {
159                jpvm.pvm_deleteTasks();
160                try {
161                        Thread.sleep(100);
162                }
163                catch (InterruptedException ie) {
164                }
165        }
166
167
168        private static void Add() {
169                String host = null;
170                int    port = 0;
171                try {
172                        System.out.print("\tHost name   : ");
173                        System.out.flush();
174                        host = user.readLine();
175                        System.out.print("\tPort number : ");
176                        System.out.flush();
177                        String port_str = user.readLine();
178                    try {
179                        port = Integer.valueOf(port_str).intValue();
180                    }
181                    catch (NumberFormatException nfe) {
182                        System.out.println("Bad port.");
183                        return;
184                    }
185                }
186                catch (IOException e) {
187                        System.out.println("i/o exception");
188                        try {
189                                Quit();
190                        }
191                        catch (jpvmException jpe) {
192                                System.exit(0);
193                        }
194                }
195                jpvmTaskId tid = new jpvmTaskId(host,port);
196                String h[] = new String[1];
197                jpvmTaskId t[] = new jpvmTaskId[1];
198                h[0] = host;
199                t[0] = tid;
200                try {
201                  jpvm.pvm_addhosts(1,h,t);
202                }
203                catch (jpvmException jpe) {
204                  perror("error - couldn't add host " + host);
205                }
206        }
207
208        private static void DelHost() {
209                String host = null;
210                int    port = 0;
211                try {
212                        System.out.print("\tHost name   : ");
213                        System.out.flush();
214                        host = user.readLine();
215                }
216                catch (IOException e) {
217                        System.out.println("i/o exception");
218                        try {
219                                Quit();
220                        }
221                        catch (jpvmException jpe) {
222                                System.exit(0);
223                        }
224                }
225                try {
226                  jpvm.pvm_delhosts(host);
227                }
228                catch (jpvmException jpe) {
229                  perror("error - couldn't del host " + host);
230                }
231        }
232
233        private static void AddAll() {
234                String host = null;
235                int    port = 0;
236                try{
237                FileReader input = new FileReader("myhosts");
238                BufferedReader bufRead = new BufferedReader(input);
239                String line;    // String that holds current
240                int count = 0;  // Line number of count 
241                line = bufRead.readLine();
242                count++;
243                while (line != null){
244                        //System.out.println(count+": "+line);
245                        String [] tmp = null;
246                        //if(line ==null ) break;
247                        tmp=line.split(" ++");
248                        if(tmp.length<2)  perror("myhosts file error -format: hosts port " );
249                        host = tmp[0];
250                        try{
251                                port = Integer.valueOf(tmp[1]).intValue();
252                        } catch (NumberFormatException nfe) {
253                                System.out.println("Bad port.");
254                                return;
255                        }
256
257                        jpvmTaskId tid = new jpvmTaskId(host,port);
258                        String h[] = new String[1];
259                        jpvmTaskId t[] = new jpvmTaskId[1];
260                        h[0] = host;
261                        t[0] = tid;
262                        try {
263                                jpvm.pvm_addhosts(1,h,t);
264                        }
265                        catch (jpvmException jpe) {
266                                perror("error - couldn't add host " + host);
267                        }
268
269                        count++;
270                        line = bufRead.readLine();
271                }
272                bufRead.close();
273                }catch (IOException e){
274                        System.out.println("You need to prepare myhosts file in format:");
275                        System.out.println("Hostsname Port");
276//                      e.printStackTrace();
277                }
278        }
279
280        private static void perror(String message) {
281                System.err.println("jpvm console: "+ message);
282                System.err.flush();
283        }
284};