View Javadoc

1   /*
2      Copyright 2010 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 mirrormap;
17  
18  import java.io.Serializable;
19  import java.util.Map.Entry;
20  
21  import mirrormap.commands.ICommand;
22  import mirrormap.nio.IConnectionEndPointReceiver;
23  
24  /**
25   * The end point receiver on the client-side that handles the serialised
26   * {@link ICommand} objects sent from a remote server-side end point.
27   * 
28   * @author Ramon Servadei
29   */
30  class ClientCommandReceiver extends ServerCommandReceiver implements
31      IConnectionEndPointReceiver
32  {
33      ClientCommandReceiver()
34      {
35          super();
36      }
37  
38      /**
39       * Create a {@link ConnectionEndPointMapListener} and attach it to the named
40       * {@link MirrorMap}
41       * 
42       * @param mirrorMapName
43       *            the name of the {@link MirrorMap} to attach the listener to
44       * @return <code>true</code> if the listener is added, <code>false</code>
45       *         otherwise
46       */
47      public boolean addListener(String mirrorMapName)
48      {
49          synchronized (this.listeners)
50          {
51              if (this.listeners.containsKey(mirrorMapName))
52              {
53                  return false;
54              }
55              ConnectionEndPointMapListener listener =
56                  new ConnectionEndPointMapListener(this.connectionEndPoint,
57                      mirrorMapName);
58              this.listeners.put(mirrorMapName, listener);
59              final MirrorMap<Serializable, Serializable> mirrorMap =
60                  MirrorMap.get(mirrorMapName);
61              if (mirrorMap == null)
62              {
63                  return false;
64              }
65              return mirrorMap.addListener(listener);
66          }
67      }
68  
69      /**
70       * Remove the {@link ConnectionEndPointMapListener} attached to the named
71       * {@link MirrorMap}
72       * 
73       * @param mirrorMapName
74       *            the name of the {@link MirrorMap} to remove the listener from
75       */
76      public void removeListener(String mirrorMapName)
77      {
78          synchronized (this.listeners)
79          {
80              ConnectionEndPointMapListener listener =
81                  this.listeners.remove(mirrorMapName);
82              if (listener != null)
83              {
84                  final MirrorMap<Serializable, Serializable> mirrorMap =
85                      MirrorMap.get(mirrorMapName);
86                  if (mirrorMap != null)
87                  {
88                      mirrorMap.removeListener(listener);
89                  }
90              }
91          }
92      }
93  
94      @Override
95      protected void doReceive(ICommand command)
96      {
97          executeCommand(command);
98      }
99  
100     @Override
101     protected void destroyListener(
102         Entry<String, ConnectionEndPointMapListener> entry)
103     {
104         MirrorMap.disconnect(MirrorMap.get(entry.getKey()));
105         super.destroyListener(entry);
106     }
107 }