1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.transport.socket.nio;
21
22 import java.net.InetSocketAddress;
23 import java.net.SocketAddress;
24 import java.nio.channels.DatagramChannel;
25 import java.nio.channels.SelectionKey;
26
27 import org.apache.mina.core.buffer.IoBuffer;
28 import org.apache.mina.core.filterchain.DefaultIoFilterChain;
29 import org.apache.mina.core.filterchain.IoFilterChain;
30 import org.apache.mina.core.service.DefaultTransportMetadata;
31 import org.apache.mina.core.service.IoHandler;
32 import org.apache.mina.core.service.IoProcessor;
33 import org.apache.mina.core.service.IoService;
34 import org.apache.mina.core.service.TransportMetadata;
35 import org.apache.mina.core.session.IoSession;
36 import org.apache.mina.transport.socket.DatagramSessionConfig;
37
38
39
40
41
42
43
44 class NioDatagramSession extends NioSession {
45
46 static final TransportMetadata METADATA =
47 new DefaultTransportMetadata(
48 "nio", "datagram", true, false,
49 InetSocketAddress.class,
50 DatagramSessionConfig.class, IoBuffer.class);
51
52 private final IoService service;
53 private final DatagramSessionConfig config;
54 private final IoFilterChain filterChain = new DefaultIoFilterChain(this);
55 private final DatagramChannel ch;
56 private final IoHandler handler;
57 private final InetSocketAddress localAddress;
58 private final InetSocketAddress remoteAddress;
59 private final IoProcessor<NioSession> processor;
60
61 private SelectionKey key;
62
63
64
65
66 NioDatagramSession(IoService service,
67 DatagramChannel ch, IoProcessor<NioSession> processor,
68 SocketAddress remoteAddress) {
69 this.service = service;
70 this.ch = ch;
71 this.config = new NioDatagramSessionConfig(ch);
72 this.handler = service.getHandler();
73 this.processor = processor;
74 this.remoteAddress = (InetSocketAddress) remoteAddress;
75 this.localAddress = (InetSocketAddress) ch.socket().getLocalSocketAddress();
76 }
77
78
79
80
81 NioDatagramSession(IoService service,
82 DatagramChannel ch, IoProcessor<NioSession> processor) {
83 this(service, ch, processor, ch.socket().getRemoteSocketAddress());
84 }
85
86 public IoService getService() {
87 return service;
88 }
89
90 @Override
91 public IoProcessor<NioSession> getProcessor() {
92 return processor;
93 }
94
95 public DatagramSessionConfig getConfig() {
96 return config;
97 }
98
99 public IoFilterChain getFilterChain() {
100 return filterChain;
101 }
102
103 @Override
104 DatagramChannel getChannel() {
105 return ch;
106 }
107
108 @Override
109 SelectionKey getSelectionKey() {
110 return key;
111 }
112
113 @Override
114 void setSelectionKey(SelectionKey key) {
115 this.key = key;
116 }
117
118 public IoHandler getHandler() {
119 return handler;
120 }
121
122 public TransportMetadata getTransportMetadata() {
123 return METADATA;
124 }
125
126 public InetSocketAddress getRemoteAddress() {
127 return remoteAddress;
128 }
129
130 public InetSocketAddress getLocalAddress() {
131 return localAddress;
132 }
133
134 @Override
135 public InetSocketAddress getServiceAddress() {
136 return (InetSocketAddress) super.getServiceAddress();
137 }
138 }