001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020package org.apache.mina.example.udp.client; 021 022import java.net.InetSocketAddress; 023 024import org.apache.mina.core.buffer.IoBuffer; 025import org.apache.mina.core.future.ConnectFuture; 026import org.apache.mina.core.future.IoFutureListener; 027import org.apache.mina.core.service.IoConnector; 028import org.apache.mina.core.service.IoHandlerAdapter; 029import org.apache.mina.core.session.IdleStatus; 030import org.apache.mina.core.session.IoSession; 031import org.apache.mina.example.udp.MemoryMonitor; 032import org.apache.mina.transport.socket.nio.NioDatagramConnector; 033import org.slf4j.Logger; 034import org.slf4j.LoggerFactory; 035 036/** 037 * Sends its memory usage to the MemoryMonitor server. 038 * 039 * @author <a href="http://mina.apache.org">Apache MINA Project</a> 040 */ 041public class MemMonClient extends IoHandlerAdapter { 042 043 private final static Logger LOGGER = LoggerFactory.getLogger(MemMonClient.class); 044 045 private IoSession session; 046 047 private IoConnector connector; 048 049 /** 050 * Default constructor. 051 */ 052 public MemMonClient() { 053 054 LOGGER.debug("UDPClient::UDPClient"); 055 LOGGER.debug("Created a datagram connector"); 056 connector = new NioDatagramConnector(); 057 058 LOGGER.debug("Setting the handler"); 059 connector.setHandler(this); 060 061 LOGGER.debug("About to connect to the server..."); 062 ConnectFuture connFuture = connector.connect(new InetSocketAddress( 063 "localhost", MemoryMonitor.PORT)); 064 065 LOGGER.debug("About to wait."); 066 connFuture.awaitUninterruptibly(); 067 068 LOGGER.debug("Adding a future listener."); 069 connFuture.addListener(new IoFutureListener<ConnectFuture>() { 070 public void operationComplete(ConnectFuture future) { 071 if (future.isConnected()) { 072 LOGGER.debug("...connected"); 073 session = future.getSession(); 074 try { 075 sendData(); 076 } catch (InterruptedException e) { 077 e.printStackTrace(); 078 } 079 } else { 080 LOGGER.error("Not connected...exiting"); 081 } 082 } 083 }); 084 } 085 086 private void sendData() throws InterruptedException { 087 for (int i = 0; i < 30; i++) { 088 long free = Runtime.getRuntime().freeMemory(); 089 IoBuffer buffer = IoBuffer.allocate(8); 090 buffer.putLong(free); 091 buffer.flip(); 092 session.write(buffer); 093 094 try { 095 Thread.sleep(1000); 096 } catch (InterruptedException e) { 097 e.printStackTrace(); 098 throw new InterruptedException(e.getMessage()); 099 } 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}