📄 zipcodeclient.java
字号:
//package ZipClient;
// A client for ZipCodeServer.
// Uses ZipCodeServer as an interface and tests all methods implemented
// by remote server.
// Reads data from a local file containing (city, zip) pairs as follows:
// city1
// zip1
// ...
// ...
import java.io.*;
public class ZipCodeClient
{ // main takes four arguments:
// (0) a host.
// (1) a port.
// (2) a service name.
// (3) a file name.
public static void main(String[] args) throws IOException
{ String host = args[0];
int port = Integer.parseInt(args[1]);
String serviceName = args[2];
BufferedReader in = new BufferedReader(new FileReader(args[3]));
// Locate the registry and get ror.
SimpleRegistry sr =
LocateSimpleRegistry.getRegistry(host, port);
RemoteObjectRef ror = sr.lookup(serviceName);
// Create stub (=proxy) out of ror.
// You must write the stub class for ZipCodeServerImpl.java.
// It won't be automatically generated by rmic as in the standard RMI.
// For each method in ZipCodeServerImpl.java, there should be a
// corresponding method in the stub class.
ZipCodeServerImpl_stub zcs = (ZipCodeServerImpl_stub) ror.localise();
// Read data file (3) and construct a "local" zip code list,
// consisting linked instances of ZipCodeList.
// Later, this will be sent to the server.
// again no error check!
ZipCodeList l = null;
boolean flag = true;
while (flag)
{ String city = in.readLine();
String code = in.readLine();
if (city == null)
flag = false;
else
l = new ZipCodeList(city.trim(), code.trim(), l);
}
// The final value of l is the instance at the head of the list.
// Print out the local list just constructed.
System.out.println("This is the original list.");
ZipCodeList temp = l;
while (temp !=null)
{ System.out.println("city: "+temp.city+", "+"code: "+temp.ZipCode);
temp = temp.next;
}
// Test the (remote) initialise method implemented by ZipCodeServerImpl.
// Main challenge is to code the stub to marshall the whole list, l.
zcs.initialise(l);
System.out.println("\n Server initalised.");
// Test the (remote) find method.
System.out.println("\n This is the remote list given by find.");
temp = l;
while (temp !=null)
{ // Here is a test.
String res = zcs.find(temp.city);
System.out.println("city: "+temp.city+", "+"code: "+res);
temp=temp.next;
}
// Test the (remote) findAll method.
System.out.println("\n This is the remote list given by findAll.");
// Here is a test.
temp = zcs.findAll(); // Called once: cf. zcs.find above.
while (temp !=null)
{ System.out.println("city: "+temp.city+", "+"code: "+temp.ZipCode);
temp=temp.next;
}
// Test the (remote) printAll method.
System.out.println("\n We test printing at remote site.");
// here is a test.
zcs.printAll();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -