⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timeserver.java

📁 sea是一个基于seda模式的实现。这个设计模式将系统分为很多stage。每个stage分布不同的任务(基于线程池)。通过任务流的方式提高系统的效率。
💻 JAVA
字号:
/* * Copyright (c) 2003, The Regents of the University of California, through * Lawrence Berkeley National Laboratory (subject to receipt of any required * approvals from the U.S. Dept. of Energy). All rights reserved. */package gov.lbl.dsd.sea.nio.demo;import gov.lbl.dsd.sea.Stage;import gov.lbl.dsd.sea.StageManager;import gov.lbl.dsd.sea.nio.AgentEventHandler;import gov.lbl.dsd.sea.nio.NetAgent;import gov.lbl.dsd.sea.nio.event.ChannelRequest;import gov.lbl.dsd.sea.nio.event.ChannelResponse;import java.io.IOException;import java.nio.ByteBuffer;import java.nio.channels.SelectionKey;import java.nio.channels.SocketChannel;import java.nio.charset.Charset;import java.util.Date;/** * Simple time server that can be tested in * conjunction with a standard UNIX telnet client. *  * @author whoschek@lbl.gov * @author $Author: gegles $ * @version $Revision: 1.4 $, $Date: 2004/09/16 16:57:15 $ */public class TimeServer extends AgentEventHandler {		private static final Charset CHARSET = Charset.forName("US-ASCII");	private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory			.getLog(TimeServer.class);	static public void main(String args[]) throws IOException {		new TimeServer(args);	}	public TimeServer(String args[]) throws IOException {		NetAgent agent = new NetAgent();		Stage myStage = new StageManager().createStage(this).start();		for (int i = 0; i < args.length; i++) {			int port = Integer.parseInt(args[i]);					agent.addListenPort(myStage, port);		}		agent.start();	}	protected void onClosed(ChannelResponse.Closed rsp) {		log.info("*************got channel closed=" + rsp);		if (rsp.getException() != null) {			; // nothing special to be done		}	}	protected void onConnected(ChannelResponse.Connected rsp) {		throw new UnsupportedOperationException();	}	protected void onRead(ChannelResponse.Read rsp) {		throw new UnsupportedOperationException();	}	protected void onAccepted(ChannelResponse.Accepted rsp) {		rsp.getAgent().enqueue(				new ChannelRequest.WriteData(rsp.getKey()						.channel(), createResponse((SocketChannel) rsp.getKey().channel())));	}	protected void onWrite(ChannelResponse.Write rsp) {		rsp.getAgent().enqueue(new ChannelRequest.Close(rsp.getKey().channel()));		rsp.getBuffer().flip();		System.out.println("******************** wrote *********** "				+ CHARSET.decode(rsp.getBuffer()).toString());		// recycling to buffer pool is an optional optimization		rsp.getAgent().getReadBufferPool().put(rsp.getBuffer());			}	protected void onRegistered(ChannelResponse.Registered rsp) {		if (rsp.getInterestOps() == SelectionKey.OP_ACCEPT) {			System.out.println("******** server started; now listening *****");		}	}	protected ByteBuffer createResponse(SocketChannel channel) {		Date now = new Date();		ByteBuffer buffer = CHARSET.encode(now + "\r\n");		System.out.println("Response to " + channel.socket().getInetAddress() + " = " + now);		return buffer;	}		}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -