View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
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   * Sends its memory usage to the MemoryMonitor server.
38   *
39   * @author The Apache MINA Project (dev@mina.apache.org)
40   * @version $Rev$, $Date$
41   */
42  public class MemMonClient extends IoHandlerAdapter {
43  
44      private Logger log = LoggerFactory.getLogger(MemMonClient.class);
45  
46      private IoSession session;
47  
48      private IoConnector connector;
49  
50      /**
51       * Default constructor.
52       */
53      public MemMonClient() {
54  
55          log.debug("UDPClient::UDPClient");
56          log.debug("Created a datagram connector");
57          connector = new NioDatagramConnector();
58  
59          log.debug("Setting the handler");
60          connector.setHandler(this);
61  
62          log.debug("About to connect to the server...");
63          ConnectFuture connFuture = connector.connect(new InetSocketAddress(
64                  "localhost", MemoryMonitor.PORT));
65  
66          log.debug("About to wait.");
67          connFuture.awaitUninterruptibly();
68  
69          log.debug("Adding a future listener.");
70          connFuture.addListener(new IoFutureListener<ConnectFuture>() {
71              public void operationComplete(ConnectFuture future) {
72                  if (future.isConnected()) {
73                      log.debug("...connected");
74                      session = future.getSession();
75                      try {
76                          sendData();
77                      } catch (InterruptedException e) {
78                          e.printStackTrace();
79                      }
80                  } else {
81                      log.error("Not connected...exiting");
82                  }
83              }
84          });
85      }
86  
87      private void sendData() throws InterruptedException {
88          for (int i = 0; i < 30; i++) {
89              long free = Runtime.getRuntime().freeMemory();
90              IoBuffer buffer = IoBuffer.allocate(8);
91              buffer.putLong(free);
92              buffer.flip();
93              session.write(buffer);
94  
95              try {
96                  Thread.sleep(1000);
97              } catch (InterruptedException e) {
98                  e.printStackTrace();
99                  throw new InterruptedException(e.getMessage());
100             }
101         }
102     }
103 
104     @Override
105     public void exceptionCaught(IoSession session, Throwable cause)
106             throws Exception {
107         cause.printStackTrace();
108     }
109 
110     @Override
111     public void messageReceived(IoSession session, Object message)
112             throws Exception {
113         log.debug("Session recv...");
114     }
115 
116     @Override
117     public void messageSent(IoSession session, Object message) throws Exception {
118         log.debug("Message sent...");
119     }
120 
121     @Override
122     public void sessionClosed(IoSession session) throws Exception {
123         log.debug("Session closed...");
124     }
125 
126     @Override
127     public void sessionCreated(IoSession session) throws Exception {
128         log.debug("Session created...");
129     }
130 
131     @Override
132     public void sessionIdle(IoSession session, IdleStatus status)
133             throws Exception {
134         log.debug("Session idle...");
135     }
136 
137     @Override
138     public void sessionOpened(IoSession session) throws Exception {
139         log.debug("Session opened...");
140     }
141 
142     public static void main(String[] args) {
143         new MemMonClient();
144     }
145 }