1 22 package fr.dyade.aaa.agent; 23 24 28 public class SyncNotification extends Notification { 29 30 private transient Context ctx; 31 32 protected SyncNotification() { 33 persistent = false; 34 ctx = new Context(this); 35 } 36 37 public Object [] invoke(AgentId to) 38 throws InterruptedException , Exception { 39 return ctx.invoke(to); 40 } 41 42 public void Throw(Exception exc) { 43 if (ctx != null) { 44 ctx.Throw(exc); 45 } 46 } 47 48 public void Return(Object [] values) { 49 if (ctx != null) { 50 ctx.Return(values); 51 } 52 } 53 54 public Object getValue(int index) { 55 if (ctx != null) { 56 return ctx.getValue(index); 57 } else return null; 58 } 59 60 public final Exception getException() { 61 if (ctx != null) { 62 return ctx.getException(); 63 } else return null; 64 } 65 66 static class Result { 67 Object [] values; 68 Exception exc; 69 } 70 71 public static class Context { 72 private Notification syncRequest; 73 74 private Result res; 75 76 public Context(Notification syncRequest) { 77 this.syncRequest = syncRequest; 78 res = new Result(); 79 } 80 81 public synchronized Object [] invoke(AgentId to) 82 throws InterruptedException , Exception { 83 Channel.sendTo(to, syncRequest); 84 wait(); 85 if (res.exc != null) { 86 throw res.exc; 87 } else { 88 return res.values; 89 } 90 } 91 92 public synchronized void Throw(Exception exc) { 93 res.exc = exc; 94 notify(); 95 } 96 97 public synchronized void Return(Object [] values) { 98 res.values = values; 99 notify(); 100 } 101 102 public Object getValue(int index) { 103 if (res.values != null) { 104 return res.values[index]; 105 } else return null; 106 } 107 108 public final Exception getException() { 109 return res.exc; 110 } 111 } 112 } 113 | Popular Tags |