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 class NioDatagramSession extends NioSession {
44
45 static final TransportMetadata METADATA =
46 new DefaultTransportMetadata(
47 "nio", "datagram", true, false,
48 InetSocketAddress.class,
49 DatagramSessionConfig.class, IoBuffer.class);
50
51 private final IoService service;
52 private final DatagramSessionConfig config;
53 private final IoFilterChain filterChain = new DefaultIoFilterChain(this);
54 private final DatagramChannel ch;
55 private final IoHandler handler;
56 private final InetSocketAddress localAddress;
57 private final InetSocketAddress remoteAddress;
58 private final IoProcessor<NioSession> processor;
59
60 private SelectionKey key;
61
62
63
64
65 NioDatagramSession(IoService service,
66 DatagramChannel ch, IoProcessor<NioSession> processor,
67 SocketAddress remoteAddress) {
68 this.service = service;
69 this.ch = ch;
70 this.config = new NioDatagramSessionConfig(ch);
71 this.handler = service.getHandler();
72 this.processor = processor;
73 this.remoteAddress = (InetSocketAddress) remoteAddress;
74 this.localAddress = (InetSocketAddress) ch.socket().getLocalSocketAddress();
75 }
76
77
78
79
80 NioDatagramSession(IoService service,
81 DatagramChannel ch, IoProcessor<NioSession> processor) {
82 this(service, ch, processor, ch.socket().getRemoteSocketAddress());
83 }
84
85 public IoService getService() {
86 return service;
87 }
88
89 @Override
90 public IoProcessor<NioSession> getProcessor() {
91 return processor;
92 }
93
94 public DatagramSessionConfig getConfig() {
95 return config;
96 }
97
98 public IoFilterChain getFilterChain() {
99 return filterChain;
100 }
101
102 @Override
103 DatagramChannel getChannel() {
104 return ch;
105 }
106
107 @Override
108 SelectionKey getSelectionKey() {
109 return key;
110 }
111
112 @Override
113 void setSelectionKey(SelectionKey key) {
114 this.key = key;
115 }
116
117 public IoHandler getHandler() {
118 return handler;
119 }
120
121 public TransportMetadata getTransportMetadata() {
122 return METADATA;
123 }
124
125 public InetSocketAddress getRemoteAddress() {
126 return remoteAddress;
127 }
128
129 public InetSocketAddress getLocalAddress() {
130 return localAddress;
131 }
132
133 @Override
134 public InetSocketAddress getServiceAddress() {
135 return (InetSocketAddress) super.getServiceAddress();
136 }
137 }