



(See horb/examples/objRefSend for example code.)
We have explored object passing by copying. In this subsection we will learn to pass objects by reference. While a copy of an object is passed to a remote object in the previous examples, now a reference (pointer) to a remote object is passed to another remote object (see diagram below). This is quite useful when you want to pass a handle of a service to another remote object.

The usage is quite simple since a remote object reference is always passed as is. Let's assume there are two remote object references, remote1 and remote2. If we pass remote2 to a method of remote1, remote1 can then use remote2 as a remote object reference as is.
// Caller side:
. . .
remote1.op1(remote2);
. . .
// Callee side
class Remote1 {
void op1(Remote2 remote) {
remote.anotherOp(); // remote is a remote object reference
. . .
Note that ORB (the HORB runtime) creates a new TCP/IP connection when a remote object reference is passed. At the same time a new thread of the server object is created to service the connection. Generally a new TCP/IP connection and a thread of the server object is created when a new remote object reference is created. We must properly synchronize the threads. A connection can be released by _release() method of proxy objects.
Here is another example of remote object reference passing. In this example we use three objects, One, Two, and Three. Object One calls a method of Two with a remote object reference to Three. Then Two calls a method of Three via the remote object reference.
One -> Two -> Three
Class One makes a remote object of Two and a remote object of Three, then calls Two's trip() method with a remote object reference to Three.
// One.java
package horb.examples.objRefSend;
import horb.orb.*;
class One {
public static void main(String argv[]) {
String host = (argv.length == 1) ? argv[0] : "-";
HorbURL url = new HorbURL(host, null);
Two_Proxy two = new Two_Proxy(url);
Three_Proxy three = new Three_Proxy(url);
String result = two.trip(three, "One->")+"One";
System.out.println("result = " + result);
}
}
Class Two receives the remote object reference, and then calls trip() method of class Three through the remote object reference.
// Two.java
package horb.examples.objRefSend;
class Two {
String trip(Three_Proxy three, String msg) {
return three.trip(msg+"Two->")+"Two->";
}
}
Now, class Three returns a message.
// Three.java
package horb.examples.objRefSend;
public class Three {
public String trip(String msg) {
return msg+"Three->";
}
}
Let's see how we compile and execute this code.
c:> horbc Three.java Two.java
c:> horbc -c One.java
c:> horb (in another window)
c:> java horb.examples.objRefSend.One
result = One->Two->Three->Two->One