View Javadoc

1   /*
2      Copyright 2008 Ramon Servadei
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15   */
16  package fulmine.rpc.events;
17  
18  import java.util.Arrays;
19  
20  import fulmine.event.IEventManager;
21  import fulmine.event.system.AbstractSystemEvent;
22  import fulmine.model.field.IField;
23  import fulmine.model.field.StringField;
24  import fulmine.rpc.IRpcCodec;
25  import fulmine.rpc.IRpcDefinition;
26  import fulmine.rpc.IRpcMarker;
27  import fulmine.rpc.IRpcRegistry;
28  import fulmine.rpc.RpcCodec;
29  import fulmine.rpc.RpcDefinition;
30  import fulmine.util.reference.QuadValue;
31  
32  /**
33   * Raised in a receiving context when an RPC is invoked from a remote context.
34   * The event has the RPC key that identifies the RPC to invoke and the
35   * arguments.
36   * <p>
37   * This event differs from {@link SendRpcEvent} in that this event is raised in
38   * the receiving context and encapsulates the action to actually execute the
39   * RPC.
40   * 
41   * @author Ramon Servadei
42   */
43  public final class RpcInvokeEvent extends AbstractSystemEvent
44  {
45      private static final String NOT_DECODED_YET = "Not decoded yet";
46  
47      private static final IField[] IFIELD =
48          new IField[] { new StringField("args", NOT_DECODED_YET) };
49  
50      /** The raw bytes received from the remote context */
51      private final byte[] rpcData;
52  
53      /** The identity of the remote context invoking the RPC */
54      private String remoteContextIdentity;
55  
56      /** The RPC registry key decoded from the rpcData byte buffer */
57      private String rpcKey = NOT_DECODED_YET;
58  
59      /** The RPC arguments decoded from the rpcData byte buffer */
60      private IField[] arguments = IFIELD;
61  
62      /** The RPC marker for the called RPC */
63      private IRpcMarker marker;
64  
65      /**
66       * Constructor for the event
67       * 
68       * @param context
69       *            the local context
70       * @param rpcData
71       *            the data for the event, this includes the RPC key, argument
72       *            data and RPC marker ID
73       */
74      public RpcInvokeEvent(IEventManager context, byte[] rpcData)
75      {
76          super(context);
77          this.rpcData = rpcData;
78      }
79  
80      /**
81       * Decode the byte buffer into the RPC key and the arguments for the RPC
82       * 
83       * @param registry
84       *            the RPC registry used to lookup the {@link IRpcDefinition} for
85       *            the RPC invocation.
86       */
87      public void decode(IRpcRegistry registry)
88      {
89          IRpcCodec codec = new RpcCodec(registry);
90          final QuadValue<IRpcMarker, String, String, IField[]> decodedData =
91              codec.decode(this.rpcData);
92          this.marker = decodedData.getFirst();
93          this.rpcKey = decodedData.getSecond();
94          this.remoteContextIdentity = decodedData.getThird();
95          this.arguments = decodedData.getFourth();
96      }
97  
98      /**
99       * Get the remote context identity
100      * 
101      * @return the identity of the remote context invoking the RPC
102      */
103     public String getRemoteContextIdentity()
104     {
105         return this.remoteContextIdentity;
106     }
107 
108     /**
109      * Get the RPC key identifying the {@link RpcDefinition} this invocation is
110      * for
111      * 
112      * @return the RPC key identifying the {@link RpcDefinition} this invocation
113      *         is for
114      */
115     public String getRpcKey()
116     {
117         return this.rpcKey;
118     }
119 
120     /**
121      * Get the arguments for the RPC invocation.
122      * 
123      * @return the arguments for the RPC invocation
124      */
125     public IField[] getArguments()
126     {
127         return this.arguments;
128     }
129 
130     /**
131      * Get the RPC marker for the RPC invocation. This is used by the caller to
132      * track the result of the RPC with the invocation.
133      * 
134      * @return the marker for the RPC
135      */
136     public IRpcMarker getMarker()
137     {
138         return this.marker;
139     }
140 
141     @Override
142     protected String getAdditionalToString()
143     {
144         return "" + getMarker() + ", rpcKey=" + getRpcKey()
145             + ", remoteContext=" + getRemoteContextIdentity() + " args="
146             + Arrays.deepToString(getArguments());
147     }
148 }