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
59 private SelectionKey key;
60
61
62
63
64 NioDatagramSession(IoService service,
65 DatagramChannel ch, IoProcessor<NioSession> processor,
66 SocketAddress remoteAddress) {
67 super(processor);
68 this.service = service;
69 this.ch = ch;
70 this.config = new NioDatagramSessionConfig(ch);
71 this.config.setAll(service.getSessionConfig());
72 this.handler = service.getHandler();
73 this.remoteAddress = (InetSocketAddress) remoteAddress;
74 this.localAddress = (InetSocketAddress) ch.socket().getLocalSocketAddress();
75 }
76
77
78
79
80 NioDatagramSession(IoService service, DatagramChannel ch, IoProcessor<NioSession> processor) {
81 this(service, ch, processor, ch.socket().getRemoteSocketAddress());
82 }
83
84 public IoService getService() {
85 return service;
86 }
87
88 public DatagramSessionConfig getConfig() {
89 return config;
90 }
91
92 public IoFilterChain getFilterChain() {
93 return filterChain;
94 }
95
96 @Override
97 DatagramChannel getChannel() {
98 return ch;
99 }
100
101 @Override
102 SelectionKey getSelectionKey() {
103 return key;
104 }
105
106 @Override
107 void setSelectionKey(SelectionKey key) {
108 this.key = key;
109 }
110
111 public IoHandler getHandler() {
112 return handler;
113 }
114
115 public TransportMetadata getTransportMetadata() {
116 return METADATA;
117 }
118
119 public InetSocketAddress getRemoteAddress() {
120 return remoteAddress;
121 }
122
123 public InetSocketAddress getLocalAddress() {
124 return localAddress;
125 }
126
127 @Override
128 public InetSocketAddress getServiceAddress() {
129 return (InetSocketAddress) super.getServiceAddress();
130 }
131 }