-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNERemoteObjectReference.java
127 lines (116 loc) · 3.63 KB
/
NERemoteObjectReference.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* Class : NERemoteObjectReference
* Authors : Eugene Choi, Norbert Chu
* Andrew IDs : dechoi, nrchu
* Description: Encapsulates server and other information about a remote
* object. This class is meant to be the "reference" to a
* remote object that can be passed around remotely (as a
* Serializable object).
*/
import java.io.*;
import java.net.*;
import java.lang.reflect.*;
public class NERemoteObjectReference implements Serializable {
private String hostAddress, remoteInterfaceName;
private int port;
private int key;
public NERemoteObjectReference (String addr, int p, int k, String riname) {
hostAddress = addr;
port = p;
remoteInterfaceName = riname;
key = k;
}
/*
* Get the address of the server hosting the remote object
*/
public String getHostAddress () {
return hostAddress;
}
/*
* Get the port of the server hosting the remote object
*/
public int getPort () {
return port;
}
/*
* Get the key of the remote object. The key is an id that allows the
* server to locate the actual remote object.
*/
public int getKey () {
return key;
}
/*
* Get the name of the remote interface that the remote object implements
*/
public String getRemoteInterfaceName () {
return remoteInterfaceName;
}
/*
* Localise the remote object reference. Returns the stub for the remote
* object
*/
public NERemoteObjectStub localise () {
// TODO:
// 1. Check if riname + "_Stub" class exists
// 2. If not, automatically create it
try {
String stubTypeName = remoteInterfaceName + "_Stub";
System.out.println ("Stubname: " + stubTypeName);
Class stubType = Class.forName (stubTypeName);
NERemoteObjectStub stub = (NERemoteObjectStub) stubType.newInstance ();
stub.init (this);
return stub;
}
catch (ClassNotFoundException e) {
// generate stub, etc
e.printStackTrace ();
}
catch (IllegalAccessException e) {
e.printStackTrace ();
}
catch (InstantiationException e) {
e.printStackTrace ();
}
return null;
// Implement this as you like: essentially you should
// create a new stub object and returns it.
// Assume the stub class has the name e.g.
//
// Remote_Interface_Name + "_stub".
//
// Then you can create a new stub as follows:
//
// Class c = Class.forName(Remote_Interface_Name + "_stub");
// Object o = c.newinstance()
//
// For this to work, your stub should have a constructor without arguments.
// You know what it does when it is called: it gives communication module
// all what it got (use CM's static methods), including its method name,
// arguments etc., in a marshalled form, and CM (yourRMI) sends it out to
// another place.
// Here let it return null.
//return null;
}
/*
* Are to references pointing to the same remote object?
*/
public boolean equals (Object o) {
NERemoteObjectReference r = (NERemoteObjectReference) o;
try {
return (InetAddress.getByName (r.getHostAddress ()).equals
(InetAddress.getByName(hostAddress)) &&
r.getRemoteInterfaceName ().equals (remoteInterfaceName) &
r.getPort () == port &&
r.getKey () == key);
}
catch (UnknownHostException e) {
return false;
}
}
public String toString () {
return "Address: " + hostAddress + "\n" +
"Port : " + port + "\n" +
"RIName : " + remoteInterfaceName + "\n" +
"Key : " + key + "\n";
}
}