KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > NumberCruncherClient


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package examples;
18
19 import java.rmi.Remote JavaDoc;
20 import java.rmi.server.UnicastRemoteObject JavaDoc;
21 import java.rmi.RemoteException JavaDoc;
22 import java.rmi.Naming JavaDoc;
23 import java.util.Vector JavaDoc;
24 import java.io.*;
25
26 /**
27    NumberCruncherClient is a simple client for factoring integers. A
28    remote NumberCruncher is contacted and asked to factor an
29    integer. The factors returned by the {@link NumberCruncherServer}
30    are displayed on the screen.
31
32    <p>See <a HREF=doc-files/NumberCruncherClient.java>source</a> code of
33    <code>NumberCruncherClient</code> for more details.
34
35    <pre>
36    <b>Usage:</b> java org.apache.log4j.examples.NumberCruncherClient HOST
37     &nbsp;&nbsp;&nbsp;&nbsp;where HOST is the machine where the NumberCruncherServer is running
38    </pre>
39    
40    <p>Note that class files for the example code is not included in
41    any of the distributed log4j jar files. You will have to add the
42    directory <code>/dir-where-you-unpacked-log4j/classes</code> to
43    your classpath before trying out the examples.
44
45    @author Ceki G&uuml;lc&uuml;
46    
47  */

48 public class NumberCruncherClient {
49
50
51   public static void main(String JavaDoc[] args) {
52     if(args.length == 1) {
53       try {
54         String JavaDoc url = "rmi://"+args[0]+ "/Factor";
55         NumberCruncher nc = (NumberCruncher) Naming.lookup(url);
56     loop(nc);
57       }
58       catch(Exception JavaDoc e) {
59     e.printStackTrace();
60       }
61     }
62     else
63       usage("Wrong number of arguments.");
64   }
65
66   static
67   void usage(String JavaDoc msg) {
68     System.err.println(msg);
69     System.err.println(
70      "Usage: java org.apache.log4j.examples.NumberCruncherClient HOST\n" +
71      " where HOST is the machine where the NumberCruncherServer is running.");
72     System.exit(1);
73   }
74
75
76   static
77   void loop(NumberCruncher nc) {
78     BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
79     int i = 0;
80     while (true) {
81       System.out.print("Enter a number to factor, '-1' to quit: ");
82       try {
83     i = Integer.parseInt(in.readLine());
84       }
85       catch(Exception JavaDoc e) {
86     e.printStackTrace();
87       }
88       if(i == -1) {
89     System.out.print("Exiting loop.");
90     return;
91       }
92       else {
93     try {
94       System.out.println("Will attempt to factor "+i);
95       int[] factors = nc.factor(i);
96       System.out.print("The factors of "+i+" are");
97       for(int k=0; k < factors.length; k++) {
98         System.out.print(" " + factors[k]);
99       }
100       System.out.println(".");
101     }
102     catch(RemoteException JavaDoc e) {
103       System.err.println("Could not factor "+i);
104       e.printStackTrace();
105     }
106       }
107     }
108   }
109 }
110
Popular Tags