1 /*********************************************************************
2 Copyright 2012, Ralph Ronnquist.
4 This file is part of GORITE.
6 GORITE is free software: you can redistribute it and/or modify it
7 under the terms of the Lesser GNU General Public License as published
8 by the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 GORITE is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the Lesser GNU General Public
17 License along with GORITE. If not, see <http://www.gnu.org/licenses/>.
18 **********************************************************************/
20 package com.intendico.gorite.addon;
22 import com.intendico.gorite.Goal;
23 import com.intendico.gorite.Data;
24 import java.util.Observable;
27 * Utility class to deal with request-response protocols between
28 * in-core performers with BellBoy capability. The requester would
29 * include a {@link Handshake#create()} step in the requesting plan,
30 * with data elements "responder" and "request" set up, and the
31 * responder, who gets a Handshake object as BellBoy percept, uses the
32 * {@link Handshake#reply(Object)} method to issue their reply.
34 * <p> Note: a null response is not a response, but will make the
35 * requester keep waiting.
37 public class Handshake extends Observable {
40 * The request message of this Handshake.
42 public Object message;
45 * The response message of this Handshake.
47 public Object response;
50 * Java method to set the reply and notify to the requester
53 public void reply(Object r) {
60 * Returns a Goal object for performing a Handshake. It uses the
61 * data elements "responder" and "request" for input details, the
62 * data element "pending handshake" transiently, and the data
63 * element "response" for output, i.e. the reply object.
65 * Note that all incoming "pending handshake" values are
66 * forgotten, but not upon a completed handshake.
68 public static Goal create() {
69 return new Goal( "Perform handshake" ) {
71 * Issue the request when the goal is instantiated.
73 public Instance instantiate(String head,Data data) {
74 String who = (String) data.getValue( "responder" );
75 Handshake h = new Handshake();
76 h.message = data.getValue( "request" );
77 data.forget( "pending handshake" );
78 if ( BellBoy.send( who, h ) ) {
79 data.setValue( "pending handshake", h );
81 return super.instantiate( head, data );
85 * Keep blocking until there is a non-null reply
87 public States execute(Data data) {
88 Handshake h = (Handshake) data.getValue( "pending handshake");
92 if ( h.response == null ) {
94 return States.BLOCKED;
96 data.setValue( "response", h.response );