1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
33
34
35
36
37
38 public class NetworkChannel {
39
40
41
42 public volatile AtomicInteger count = new AtomicInteger(1);
43
44
45
46 public volatile boolean isShuttingDown = false;
47
48
49
50 public ConcurrentLinkedQueue<Channel> localChannels =
51 new ConcurrentLinkedQueue<Channel>();
52
53
54
55 public final Channel channel;
56
57
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
97
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
109
110
111 @Override
112 public int hashCode() {
113 return this.channel.getId();
114 }
115
116 }