Code Examples of the Transfer-&-Exchange Layer

Editor: T. Beiler (SRB), J.Brauckmann (SRB)

@version [CVS] @(#)
$Id: txExamples.html,v 1.9 1998/07/16 12:17:36 semper Exp $

You can get a good understanding of what's going on by looking at the test programms that are located in the test directory, namely test/, test/ for the transfer part of the txlayer, and test/ and test/ for the exchange part. All test programms are driven by test/, but that should not bother you too much.

Short introduction into the txlayer:

Objects that do a transfer are called TransferTransaction, objects that do an exchange are called ExchangeTransaction.

The things that you want to transfer are given to the Transfer-/ExchangeTransaction upon instantiation.

The Transfer-/ExchangeTransaction itself must be prepared in some way to do what you want from it. You can either prepare it for sending or receiving in the case of a transfer, or prepare it for originating or responding to an exchange.

Both have as a parameter an object called a TXContext, which contains means for communication. Upon creation of a TXContext parameters such as the hostname must be specified, and you must explicitely tell the TXContext to offer a connection request to a peer or to accept such a request.

After the transactions are prepared on both sides, they can be started by calling the begin() method. This spawns a thread that runs the transaction. Naturally, you can afterwards call join() on the transaction to wait for its completion, or you can call beginAndBlock() right from the begining.

A result can be obtained by calling getObject() after completion, that is, after join() or beginAndBlock() returned.

Now into real life:

At the sender side:

        /* First, create a TXContext. */
        TXContext txc_send = new TXContext(peerHostname, claddress);

        /* Now we can offer the context to the peer specified
         * in the TXAddress. The peer must run now an acceptContext(),
         * see below.

        /* Create some goods that can be transferred. */
        Container tc = new Container();
        tc.append(new ItemA("Good so and so"));

        /* Ask the container for a suitable TransferTransaction. */
        TransferTransaction sender = tc.getTransferTransaction();
        /* Now prepare the sender side. */

        /* Start it and wait until we are finished. */

At the receiver side:

        /* First create a TXContext. */
        TXContext txc_rec = new TXContext(claddress);

        /* Now we can accept the context from the peer specified
         * in the TXAddress. The peer must run now an offerContext(),
         * see above.
        userdata = txc_rec.acceptContext(); 

        /* Now we have to create a suitable TransferTransaction... .*/
        TransferTransaction receiver = new ContainerTransfer();

        /* Now prepare the Receiver side. */
        receiver.prepareReceiver( txc_rec.getSubContext(1));
        /* Start. */

        /* Obtain the results. */
        Result = receiver.getObject();

Classes and interfaces relevant for understanding the example