/* * Copyright (C) 2003 Peter Jonathan Klauser. All rights reserved. * * This software is published under the terms of the Jumpi Software * License version 1.1, a copy of which has been included with this * distribution in the LICENSE-jumpi.txt file. * */ package org.jumpi.examples.tcp; import org.jumpi.Destination; import org.jumpi.Handle; import org.jumpi.Jumpi; import org.jumpi.Status; import org.jumpi.examples.JumpiClientApplication; import org.jumpi.examples.JumpiClientApplicationRunner; import java.util.Hashtable; /** * Receive messages over TCP, unreliably, with each message received causing a * response message being sent back to the Requestor. The Replyer only * responds to the same Requestor until receiving from that Requestor has * failed. Multiple Requestors are therefore not handled concurrently. */ public class Replyer implements JumpiClientApplication { /** The runner. */ JumpiClientApplicationRunner runner_ = null; /** * Set the runner. * * @param runner the runner. */ public void setJumpiClientApplicationRunner( JumpiClientApplicationRunner runner) { runner_ = runner; } /** * Return the configuration values to use in doWork(). * * @return the configuration values to use. */ public Hashtable getConfiguration() { Hashtable config = new Hashtable(); config.put("org.jumpi.configurationloader", "org.jumpi.impl.ConfigurationLoader"); config.put("org.jumpi.controller", "ordering"); config.put("org.jumpi.controller.ordering.classname", "org.jumpi.impl.controller.ordering.OrderingController"); config.put("org.jumpi.controller.ordering.connector", "tcp"); config.put("org.jumpi.controller.ordering.connector.tcp.classname", "org.jumpi.impl.connector.tcp.TcpConnector"); config.put("org.jumpi.controller.ordering.connector.tcp.protocol", "tcp"); config.put("org.jumpi.controller.ordering.connector.tcp.servermode", "true"); config.put("org.jumpi.controller.ordering.connector.tcp.acceptor", "accept"); config.put("org.jumpi.controller.ordering.connector.tcp.acceptor.accept.classname", "org.jumpi.impl.connector.tcp.TcpConnectionAcceptor"); config.put("org.jumpi.controller.ordering.connector.tcp.acceptor.accept.port", "8888"); config.put("org.jumpi.controller.ordering.connector.tcp.acceptor.accept.address", "127.0.0.1"); config.put("org.jumpi.controller.ordering.connector.tcp.sendtransformer", "serialize"); config.put("org.jumpi.controller.ordering.connector.tcp.sendtransformer.serialize.classname", "org.jumpi.impl.transformer.serialization.SerializeTransformer"); config.put("org.jumpi.controller.ordering.connector.tcp.recvtransformer", "deserialize"); config.put("org.jumpi.controller.ordering.connector.tcp.recvtransformer.deserialize.classname", "org.jumpi.impl.transformer.serialization.DeserializeTransformer"); return config; } /** * Run the example code. * * @param j the started, configured Jumpi instance to use in the example. */ public void doWork(Jumpi j) { // get a Destination corresponding to the Requestor. The true // Destination is only known once the Requestor has connected // since it's local port number is determined dynamically. // We use a wildcard receive to accept from any port on the // local machine. Alternatively tcp://*:* for any host and any port. Destination anyone = j.getDestination("tcp://localhost:*"); // the specific Destination of the requestor when known. Destination requestor = anyone; long startTime = System.currentTimeMillis(); long now = startTime; // loop for one minute while ((startTime + 60000) > now) { // receive a message from the requestor, or anyone the first time around. Handle recv = j.recv(requestor); // wait for up to 60s for a message to arrive from the destination. recv.waitWhileBlocking(60000); // print out information about the status of the operation. runner_.evaluateHandle(recv); if (recv.getStatusCode() == Status.STATUS_SUCCESS) { // on a successful receive, we get the real address of the requestor requestor = recv.getSender(); // send the message to the requestor Handle send = j.send(requestor, "Response - " + recv.getMessage()); // check the send send.waitWhileBlocking(10000); // print out information about the status of the operation. runner_.evaluateHandle(send); } else if (recv.getStatusCode() == Status.STATUS_FAILURE) { // if we fail receiving from a specific Destination // then the connection has most likely failed - we can /// go back to receiving from anyone requestor = anyone; } else if (recv.getStatusCode() == Status.STATUS_TRANSIENT) { // cancel the receive if it has not matched anything for 60s. recv.cancel(); } now = System.currentTimeMillis(); } } } /* * Version History * $Log: Replyer.java,v $ * Revision 1.2 2003/06/09 21:37:03 klp * CONFIG_LOADER_KEY refactored to be 'org.jumpi.configurationloader' property * * Revision 1.1 2003/05/20 19:40:58 klp * transfer to sf.net * */