001    /*
002     * Copyright (c) 2003, The Regents of the University of California, through
003     * Lawrence Berkeley National Laboratory (subject to receipt of any required
004     * approvals from the U.S. Dept. of Energy). All rights reserved.
005     */
006    package gov.lbl.dsd.sea.nio.demo;
007    
008    import gov.lbl.dsd.sea.Stage;
009    import gov.lbl.dsd.sea.StageManager;
010    import gov.lbl.dsd.sea.nio.AgentEventHandler;
011    import gov.lbl.dsd.sea.nio.NetAgent;
012    import gov.lbl.dsd.sea.nio.event.ChannelRequest;
013    import gov.lbl.dsd.sea.nio.event.ChannelResponse;
014    
015    import java.io.IOException;
016    import java.nio.ByteBuffer;
017    import java.nio.channels.SelectionKey;
018    import java.nio.channels.SocketChannel;
019    import java.nio.charset.Charset;
020    import java.util.Date;
021    
022    /**
023     * Simple time server that can be tested in
024     * conjunction with a standard UNIX telnet client.
025     * 
026     * @author whoschek@lbl.gov
027     * @author $Author: gegles $
028     * @version $Revision: 1.4 $, $Date: 2004/09/16 16:57:15 $
029     */
030    public class TimeServer extends AgentEventHandler {
031            
032            private static final Charset CHARSET = Charset.forName("US-ASCII");
033    
034            private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
035                            .getLog(TimeServer.class);
036    
037            static public void main(String args[]) throws IOException {
038                    new TimeServer(args);
039            }
040    
041            public TimeServer(String args[]) throws IOException {
042                    NetAgent agent = new NetAgent();
043    
044                    Stage myStage = new StageManager().createStage(this).start();
045    
046                    for (int i = 0; i < args.length; i++) {
047                            int port = Integer.parseInt(args[i]);           
048                            agent.addListenPort(myStage, port);
049                    }
050    
051                    agent.start();
052            }
053    
054            protected void onClosed(ChannelResponse.Closed rsp) {
055                    log.info("*************got channel closed=" + rsp);
056                    if (rsp.getException() != null) {
057                            ; // nothing special to be done
058                    }
059            }
060    
061            protected void onConnected(ChannelResponse.Connected rsp) {
062                    throw new UnsupportedOperationException();
063            }
064    
065            protected void onRead(ChannelResponse.Read rsp) {
066                    throw new UnsupportedOperationException();
067            }
068    
069            protected void onAccepted(ChannelResponse.Accepted rsp) {
070                    rsp.getAgent().enqueue(
071                                    new ChannelRequest.WriteData(rsp.getKey()
072                                                    .channel(), createResponse((SocketChannel) rsp.getKey().channel())));
073            }
074    
075            protected void onWrite(ChannelResponse.Write rsp) {
076                    rsp.getAgent().enqueue(new ChannelRequest.Close(rsp.getKey().channel()));
077    
078                    rsp.getBuffer().flip();
079                    System.out.println("******************** wrote *********** "
080                                    + CHARSET.decode(rsp.getBuffer()).toString());
081                    // recycling to buffer pool is an optional optimization
082                    rsp.getAgent().getReadBufferPool().put(rsp.getBuffer());                
083            }
084    
085            protected void onRegistered(ChannelResponse.Registered rsp) {
086                    if (rsp.getInterestOps() == SelectionKey.OP_ACCEPT) {
087                            System.out.println("******** server started; now listening *****");
088                    }
089            }
090    
091            protected ByteBuffer createResponse(SocketChannel channel) {
092                    Date now = new Date();
093                    ByteBuffer buffer = CHARSET.encode(now + "\r\n");
094                    System.out.println("Response to " + channel.socket().getInetAddress() + " = " + now);
095                    return buffer;
096            }
097                    
098    }