public void messageReceived(int to, MessageJ m) { try { if (m.getType() == AgillaTSResMsg.AM_TYPE) { log("Got a results message: " + m); // results = (AgillaTSResMsgJ)m; synchronized (response) { response.add(m); // if (timer != null) timer.kill(); response.notifyAll(); } } else if (m.getType() == AgillaTSReqMsg.AM_TYPE) { AgillaTSReqMsgJ reqMsg = (AgillaTSReqMsgJ) m; log("Got a request message: " + m); processRequest(reqMsg); } } catch (Exception e) { e.printStackTrace(); } }
/** * Registers a reaction on this tuple space. * * @param rxn The reaction to register * @param listener The reaction callback function */ public void registerReaction(Reaction rxn, ReactionListener listener) { reactions.add(new RegisteredReaction(rxn, listener)); for (int i = 0; i < ts.size(); i++) { Tuple t = (Tuple) ts.get(i); if (rxn.getTemplate().matches(t)) listener.reactionFired(t); } }
public Tuple rdp(Tuple template) { for (int i = 0; i < ts.size(); i++) { Tuple tuple = (Tuple) ts.get(i); if (template.matches(tuple)) return tuple; } return null; }
private void checkRxns(Tuple t) { for (int i = 0; i < reactions.size(); i++) { RegisteredReaction rr = (RegisteredReaction) reactions.get(i); if (rr.getReaction().getTemplate().matches(t)) { rr.getListener().reactionFired(t); } } }
/** * Deregisters a reaction. * * @param r The reaction to register. * @return true if successful, false otherwise. */ public boolean deregisterReaction(Reaction r) { for (int i = 0; i < reactions.size(); i++) { RegisteredReaction rxn = (RegisteredReaction) reactions.get(i); if (rxn.rxn.equals(r)) { reactions.remove(i); return true; } } return false; }
public Tuple inp(Tuple template) { for (int i = 0; i < ts.size(); i++) { Tuple tuple = (Tuple) ts.get(i); log("INP: Comparing:\n" + template + "\nwith:\n" + tuple); if (template.matches(tuple)) { log("INP: Match!"); ts.remove(i); return tuple; } } return null; }
// -------------------------------------------------------------------------- // BEGIN LOCAL TS OPERATIONS public void out(Tuple t) { ts.add(t); checkRxns(t); synchronized (blocked) { blocked.notifyAll(); } }
/** * Performs a remote tuple space operation. * * @param template * @param dest * @param type * @return The matching tuple or null if none was found. */ private Tuple doMonoOp(Tuple template, int dest, int type) { AgillaTSReqMsgJ request; if (type == REMOVE) request = new AgillaTSReqMsgJ(dest, TOS_UART_ADDRESS, BasicOpcodes.OPrinp, template); else request = new AgillaTSReqMsgJ(dest, TOS_UART_ADDRESS, BasicOpcodes.OPrrdp, template); log("doMonoOp: request = " + request + ", dest = " + dest); response.clear(); try { log("doMonoOp: Sent inp or rdp request."); // results = null; sni.send(request); } catch (IOException e) { e.printStackTrace(); return null; } TimeoutTimer timer = new TimeoutTimer(response, AGILLA_RTS_TIMEOUT, timerid++); MessageJ rMsg = waitForResponse(); if (rMsg instanceof TimeoutMsgJ && ((TimeoutMsgJ) rMsg).id() == timer.id()) { log("doMonoOp: Remote TS operation timed out."); return null; } else if (rMsg instanceof AgillaTSResMsgJ) { AgillaTSResMsgJ results = (AgillaTSResMsgJ) rMsg; log("doMonoOp: Got results, " + (results.isSuccess() ? "SUCCESS" : "FAIL")); if (results.isSuccess()) return results.getTuple(); else return null; } return null; }
private MessageJ waitForResponse() { synchronized (response) { while (response.size() == 0) { try { response.wait(); } catch (Exception e) { e.printStackTrace(); } } } if (response.size() > 0) { return (MessageJ) response.remove(0); } else { log("BUG: Recursive call to waitForResponse()..."); return waitForResponse(); } }
public Tuple[] ingp(Tuple template) { Vector matchingTuples = new Vector(); for (int i = 0; i < ts.size(); i++) { Tuple tuple = (Tuple) ts.get(i); if (template.matches(tuple)) matchingTuples.add(tuple); } if (matchingTuples.size() == 0) return null; ts.removeAll(matchingTuples); return (Tuple[]) matchingTuples.toArray(new Tuple[0]); }