View Javadoc

1   /**
2      This file is part of GoldenGate Project (named also GoldenGate or GG).
3   
4      Copyright 2009, Frederic Bregier, and individual contributors by the @author
5      tags. See the COPYRIGHT.txt in the distribution for a full listing of
6      individual contributors.
7   
8      All GoldenGate Project is free software: you can redistribute it and/or 
9      modify it under the terms of the GNU General Public License as published 
10     by the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12  
13     GoldenGate is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17  
18     You should have received a copy of the GNU General Public License
19     along with GoldenGate .  If not, see <http://www.gnu.org/licenses/>.
20   */
21  package openr66.protocol.networkhandler;
22  
23  import java.util.concurrent.ConcurrentLinkedQueue;
24  import java.util.concurrent.atomic.AtomicInteger;
25  
26  import openr66.protocol.exception.OpenR66ProtocolRemoteShutdownException;
27  
28  import org.jboss.netty.channel.Channel;
29  import org.jboss.netty.channel.Channels;
30  
31  /**
32   * NetworkChannel object to keep Network channel open while some local channels
33   * are attached to it.
34   *
35   * @author Frederic Bregier
36   *
37   */
38  public class NetworkChannel {
39      /**
40       * Number of active Local Channel referencing this Network Channel
41       */
42      public volatile AtomicInteger count = new AtomicInteger(1);
43      /**
44       * Does this Network Channel is in shutdown
45       */
46      public volatile boolean isShuttingDown = false;
47      /**
48       * Associated LocalChannel
49       */
50      public ConcurrentLinkedQueue<Channel> localChannels =
51          new ConcurrentLinkedQueue<Channel>();
52      /**
53       * Network Channel
54       */
55      public final Channel channel;
56      /**
57       * Last Time in ms this channel was used by a LocalChannel
58       */
59      public long lastTimeUsed;
60      
61      public NetworkChannel(Channel networkChannel) {
62          this.channel = networkChannel;
63      }
64  
65      synchronized public void add(Channel localChannel)
66      throws OpenR66ProtocolRemoteShutdownException {
67          if (isShuttingDown) {
68              throw new OpenR66ProtocolRemoteShutdownException("Current NetworkChannel is closed");
69          }
70          lastTimeUsed = System.currentTimeMillis();
71          localChannels.add(localChannel);
72      }
73  
74      synchronized public void remove(Channel localChannel) {
75          if (localChannel.isConnected()) {
76              Channels.close(localChannel);
77          }
78          localChannels.remove(localChannel);
79      }
80  
81      synchronized public void shutdownAllLocalChannels() {
82          isShuttingDown = true;
83          count.set(0);
84          Channel localChannel = localChannels.poll();
85          while (localChannel != null) {
86              Channels.close(localChannel);
87              localChannel = localChannels.poll();
88          }
89      }
90      @Override
91      public String toString() {
92          return "NC: " + channel.isConnected() + " " +
93                  channel.getRemoteAddress() + " Count: " + count;
94      }
95  
96      /* (non-Javadoc)
97       * @see java.lang.Object#equals(java.lang.Object)
98       */
99      @Override
100     public boolean equals(Object obj) {
101         if (obj instanceof NetworkChannel) {
102             NetworkChannel obj2 = (NetworkChannel) obj;
103             return (obj2.channel.getId() == this.channel.getId());
104         }
105         return false;
106     }
107 
108     /* (non-Javadoc)
109      * @see java.lang.Object#hashCode()
110      */
111     @Override
112     public int hashCode() {
113         return this.channel.getId();
114     }
115 
116 }