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: */
020: package org.apache.mina.example.udp.client;
021:
022: import java.net.InetSocketAddress;
023:
024: import org.apache.mina.common.ConnectFuture;
025: import org.apache.mina.common.IdleStatus;
026: import org.apache.mina.common.IoBuffer;
027: import org.apache.mina.common.IoConnector;
028: import org.apache.mina.common.IoFutureListener;
029: import org.apache.mina.common.IoHandlerAdapter;
030: import org.apache.mina.common.IoSession;
031: import org.apache.mina.example.udp.MemoryMonitor;
032: import org.apache.mina.transport.socket.nio.NioDatagramConnector;
033: import org.slf4j.Logger;
034: import org.slf4j.LoggerFactory;
035:
036: /**
037: * Sends its memory usage to the MemoryMonitor server.
038: *
039: * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
040: * @version $Rev$, $Date$
041: */
042: public class MemMonClient extends IoHandlerAdapter {
043:
044: private Logger log = LoggerFactory.getLogger(MemMonClient.class);
045:
046: private IoSession session;
047:
048: private IoConnector connector;
049:
050: /**
051: * Default constructor.
052: */
053: public MemMonClient() {
054:
055: log.debug("UDPClient::UDPClient");
056: log.debug("Created a datagram connector");
057: connector = new NioDatagramConnector();
058:
059: log.debug("Setting the handler");
060: connector.setHandler(this );
061:
062: log.debug("About to connect to the server...");
063: ConnectFuture connFuture = connector
064: .connect(new InetSocketAddress("localhost",
065: MemoryMonitor.PORT));
066:
067: log.debug("About to wait.");
068: connFuture.awaitUninterruptibly();
069:
070: log.debug("Adding a future listener.");
071: connFuture.addListener(new IoFutureListener<ConnectFuture>() {
072: public void operationComplete(ConnectFuture future) {
073: if (future.isConnected()) {
074: log.debug("...connected");
075: session = future.getSession();
076: try {
077: sendData();
078: } catch (InterruptedException e) {
079: e.printStackTrace();
080: }
081: } else {
082: log.error("Not connected...exiting");
083: }
084: }
085: });
086: }
087:
088: private void sendData() throws InterruptedException {
089: for (int i = 0; i < 30; i++) {
090: long free = Runtime.getRuntime().freeMemory();
091: IoBuffer buffer = IoBuffer.allocate(8);
092: buffer.putLong(free);
093: buffer.flip();
094: session.write(buffer);
095:
096: try {
097: Thread.sleep(1000);
098: } catch (InterruptedException e) {
099: e.printStackTrace();
100: throw new InterruptedException(e.getMessage());
101: }
102: }
103: }
104:
105: @Override
106: public void exceptionCaught(IoSession session, Throwable cause)
107: throws Exception {
108: cause.printStackTrace();
109: }
110:
111: @Override
112: public void messageReceived(IoSession session, Object message)
113: throws Exception {
114: log.debug("Session recv...");
115: }
116:
117: @Override
118: public void messageSent(IoSession session, Object message)
119: throws Exception {
120: log.debug("Message sent...");
121: }
122:
123: @Override
124: public void sessionClosed(IoSession session) throws Exception {
125: log.debug("Session closed...");
126: }
127:
128: @Override
129: public void sessionCreated(IoSession session) throws Exception {
130: log.debug("Session created...");
131: }
132:
133: @Override
134: public void sessionIdle(IoSession session, IdleStatus status)
135: throws Exception {
136: log.debug("Session idle...");
137: }
138:
139: @Override
140: public void sessionOpened(IoSession session) throws Exception {
141: log.debug("Session opened...");
142: }
143:
144: public static void main(String[] args) {
145: new MemMonClient();
146: }
147: }
|