KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Map > Client


1 package Map;
2
3 // Client.java
4

5 import Map.*;
6 import java.io.*;
7 import java.util.StringTokenizer JavaDoc;
8 import org.omg.CosNaming.*;
9
10
11 public class Client
12 {
13
14     static String JavaDoc[] stringToArray(String JavaDoc from, String JavaDoc separator)
15     {
16         if (from == null) {
17             return null;
18         }
19         if (separator == null) {
20             separator = " ";
21         }
22         StringTokenizer JavaDoc toks = new StringTokenizer JavaDoc(from, separator);
23         String JavaDoc[] result = new String JavaDoc[toks.countTokens()];
24         int i = 0;
25         while (toks.hasMoreTokens()) {
26             result[i++] = toks.nextToken().trim();
27         }
28         return result;
29     }
30
31     static void printUsage() {
32         System.out.println("USAGE INFORMATION");
33         System.out.println("createbase <label>");
34         System.out.println("createderived <label>");
35         System.out.println("connect <label>");
36         System.out.println("destroy <label>");
37         System.out.println("get <label>");
38         System.out.println("list <num copies of each>");
39         System.out.println("quit");
40     }
41
42     public static void main(String JavaDoc[] args)
43     {
44         try
45         {
46             // Initialize the ORB.
47
org.omg.CORBA.ORB JavaDoc orb = org.omg.CORBA.ORB.init(args,null);
48
49             NamingContextExt nc =
50                 NamingContextExtHelper.narrow(
51                          orb.resolve_initial_references( "NameService" ));
52
53             org.omg.CORBA.Object JavaDoc o =
54                 nc.resolve(nc.to_name("PointManager1.example"));
55
56             PointManager pointManager =
57                 PointManagerHelper.narrow( o );
58
59             int x = 0;
60             int y = -1000;
61
62             BufferedReader reader =
63                 new BufferedReader((new InputStreamReader(java.lang.System.in)));
64
65             boolean done = false;
66
67             java.lang.String JavaDoc lab = null;
68             while (!done)
69             {
70                 System.out.print("Command-->");
71                 try
72                 {
73                     java.lang.String JavaDoc input = reader.readLine();
74
75                     if (input.equals("")) {
76                         Client.printUsage();
77                         continue;
78                     }
79
80                     java.lang.String JavaDoc[] tokens = Client.stringToArray(input, null);
81
82                     if (tokens == null)
83                     {
84                         Client.printUsage();
85                         continue;
86                     }
87
88                     lab = (tokens.length == 2) ? tokens[1] : null;
89
90                     if (tokens[0].equals("createbase"))
91                     {
92                         Point point = pointManager.create_point(x++, y++, lab, false);
93                         if (point != null)
94                         {
95                             point.print();
96                         }
97                         else
98                         {
99                             System.out.println("Point is [nil]");
100                         }
101                     }
102                     else if (tokens[0].equals("createderived"))
103                     {
104                         Point point = pointManager.create_point(x++, y++, lab, true);
105                         if (point != null)
106                         {
107                             point.print();
108                         }
109                         else
110                         {
111                             System.out.println("Point is [nil]");
112                         }
113                     }
114                     else if (tokens[0].equals("connect"))
115                     {
116                         System.out.print("Connect " + lab + " to how many points? ");
117                         java.lang.String JavaDoc name = reader.readLine();
118                         int num = Integer.parseInt(name);
119                         String JavaDoc[] connectTo = new String JavaDoc[num];
120                         for (int i = 0; i < num; i++)
121                         {
122                             System.out.print("\tconnection #" + i + ": ");
123                             connectTo[i] = reader.readLine();
124                         }
125                         pointManager.connect_point(lab, connectTo);
126                     }
127                     else if (tokens[0].equals("destroy"))
128                     {
129                         pointManager.destroy_point(lab);
130                     }
131                     else if (tokens[0].equals("get"))
132                     {
133                         Point point = pointManager.get_point(lab);
134                         if (point != null)
135                         {
136                             point.print();
137                         }
138                         else
139                         {
140                             System.out.println("Point is [nil]");
141                         }
142                     }
143                     else if (tokens[0].equals("list"))
144                     {
145                         Point[] points =
146                             pointManager.list_points( Integer.parseInt(lab) );
147
148                         for (int i = 0; i < points.length; i++)
149                         {
150                             if (points[i] != null)
151                             {
152                                 points[i].print();
153                             }
154                             else
155                             {
156                                 System.out.println("Point is [nil]");
157                             }
158                         }
159                     }
160                     else if (tokens[0].equals("quit"))
161                     {
162                         System.out.println("quit");
163                         done = true;
164                     }
165                     else
166                     {
167                         Client.printUsage();
168                     }
169                 }
170                 catch(InvalidPoint e) {
171                     System.out.println("InvalidPoint exception caught");
172                     Client.printUsage();
173                 }
174                 catch(java.io.IOException JavaDoc e) {
175                     System.out.println(e);
176                 }
177                 catch(Exception JavaDoc e) {
178                     e.printStackTrace();
179                     System.out.println(e);
180                 }
181             }
182         }
183         catch(Exception JavaDoc e) {
184             e.printStackTrace();
185             System.out.println(e);
186         }
187
188     }
189
190 }
191
Popular Tags