package com.nailabs.abac.process; import com.nailabs.abac.trust.*; import com.nailabs.abac.test.*; import edu.stanford.peer.rbtm.credential.Entity; import java.rmi.*; import java.rmi.server.*; /** * This class is a concrete negotiator implementation that employs RMI as the * underlying communications transport mechanism. */ public class RMINegotiator extends UnicastRemoteObject implements Negotiator { /** * The peer with which this instance negotiates. */ protected Negotiator peer; /** * The context for the current negotiation, including a trust target graph, * strategy, and frontier manager instances. */ protected NegotiationContext context; /** * Default constructor for the client-side negotiatior. This negotiator * is requesting access to a resource. */ public RMINegotiator(NegotiationContext nc) throws RemoteException { context = nc; } /** * Constructor for the server-side negotiator. This negotiator has a * resource that needs protection. */ public RMINegotiator(TargetNode root, RMINegotiator peer) throws RemoteException { setPeer(peer); } public Entity getSelf() { return context.getSelf(); } /** * initial a new root node and start the negotiation process. */ public boolean negotiate() { TTG graph = context.getGraph(); Strategy s = context.getStrategy(); SatisfactionState state; int i = 0; debug("pre-processing locally..."); //perform strategy once s.iterate(); Message changes = graph.getChanges(); graph.reset(); do { debug("processing cycle = " + ++i); debug("sending message " + changes); System.out.println("Sending message: "); System.out.println(changes.toXML()); try { Message updates = peer.receive(changes); debug("receiving message " + updates); changes = receive(updates); } catch(Exception ex) { Debug.rbtmStop(); ex.printStackTrace(); return false; } state = new SatisfactionState(context.getStrategy().getSatisfactionState()); debug("RMINegotiator: primary trust target is " + state); }while(state.getState() == SatisfactionState.UNKNOWN && i <= 100); // out of the loop so we need to return success or failure return (state.getState() == SatisfactionState.SATISFIED); } /** set the other negotiator dynamically */ public void setPeer(Negotiator peer) { this.peer = peer; } public void setRoot(TrustTarget tt) { context.getGraph().setRoot(tt); //TargetNode node = (TargetNode)context.getGraph().getNodeByHash(tt); //context.getStrategy().addToWorklist(node); } /** * Synchronously receive a message which can contain multiple operations * from an peer and then return the updates to the originating * peer. */ public Message receive(Message msg) throws Exception { Strategy strategy = context.getStrategy(); TTG graph = context.getGraph(); Message results = null; //for(int i = 0, max = msg.getOperationsCount(); i < max; i++) { // System.out.println("max = " + max); // try { // Operation op = msg.getNextOperation(); // op.perform(graph); // } // catch(Exception ex) { // ex.printStackTrace(); // } //} debug("begin receive"); graph.update(msg); debug("graph has been updated"); strategy.process(msg); debug("strategy has processed the work list"); results = graph.getChanges(); debug("grabbing the delta list = " + results); graph.reset(); debug("end receive"); return results; } /** convenience method for printing messages */ public void debug(String message) { StringBuffer buff = new StringBuffer("RMINegotiator["); buff.append(context.getSelf()).append("]: ").append(message); //System.out.println(buff); Debug.debug("negotiate", buff.toString()); } }