1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.example.udp.client;
21
22 import java.net.InetSocketAddress;
23
24 import org.apache.mina.core.buffer.IoBuffer;
25 import org.apache.mina.core.future.ConnectFuture;
26 import org.apache.mina.core.future.IoFutureListener;
27 import org.apache.mina.core.service.IoConnector;
28 import org.apache.mina.core.service.IoHandlerAdapter;
29 import org.apache.mina.core.session.IdleStatus;
30 import org.apache.mina.core.session.IoSession;
31 import org.apache.mina.example.udp.MemoryMonitor;
32 import org.apache.mina.transport.socket.nio.NioDatagramConnector;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36
37
38
39
40
41 public class MemMonClient extends IoHandlerAdapter {
42
43 private final static Logger LOGGER = LoggerFactory.getLogger(MemMonClient.class);
44
45 private IoSession session;
46
47 private IoConnector connector;
48
49
50
51
52 public MemMonClient() {
53
54 LOGGER.debug("UDPClient::UDPClient");
55 LOGGER.debug("Created a datagram connector");
56 connector = new NioDatagramConnector();
57
58 LOGGER.debug("Setting the handler");
59 connector.setHandler(this);
60
61 LOGGER.debug("About to connect to the server...");
62 ConnectFuture connFuture = connector.connect(new InetSocketAddress(
63 "localhost", MemoryMonitor.PORT));
64
65 LOGGER.debug("About to wait.");
66 connFuture.awaitUninterruptibly();
67
68 LOGGER.debug("Adding a future listener.");
69 connFuture.addListener(new IoFutureListener<ConnectFuture>() {
70 public void operationComplete(ConnectFuture future) {
71 if (future.isConnected()) {
72 LOGGER.debug("...connected");
73 session = future.getSession();
74 try {
75 sendData();
76 } catch (InterruptedException e) {
77 e.printStackTrace();
78 }
79 } else {
80 LOGGER.error("Not connected...exiting");
81 }
82 }
83 });
84 }
85
86 private void sendData() throws InterruptedException {
87 for (int i = 0; i < 30; i++) {
88 long free = Runtime.getRuntime().freeMemory();
89 IoBuffer buffer = IoBuffer.allocate(8);
90 buffer.putLong(free);
91 buffer.flip();
92 session.write(buffer);
93
94 try {
95 Thread.sleep(1000);
96 } catch (InterruptedException e) {
97 e.printStackTrace();
98 throw new InterruptedException(e.getMessage());
99 }
100 }
101 }
102
103 @Override
104 public void exceptionCaught(IoSession session, Throwable cause)
105 throws Exception {
106 cause.printStackTrace();
107 }
108
109 @Override
110 public void messageReceived(IoSession session, Object message)
111 throws Exception {
112 LOGGER.debug("Session recv...");
113 }
114
115 @Override
116 public void messageSent(IoSession session, Object message) throws Exception {
117 LOGGER.debug("Message sent...");
118 }
119
120 @Override
121 public void sessionClosed(IoSession session) throws Exception {
122 LOGGER.debug("Session closed...");
123 }
124
125 @Override
126 public void sessionCreated(IoSession session) throws Exception {
127 LOGGER.debug("Session created...");
128 }
129
130 @Override
131 public void sessionIdle(IoSession session, IdleStatus status)
132 throws Exception {
133 LOGGER.debug("Session idle...");
134 }
135
136 @Override
137 public void sessionOpened(IoSession session) throws Exception {
138 LOGGER.debug("Session opened...");
139 }
140
141 public static void main(String[] args) {
142 new MemMonClient();
143 }
144 }