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

📄 httpclient.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.AdminRequest;import gov.lbl.dsd.sea.nio.event.ChannelRequest;import gov.lbl.dsd.sea.nio.event.ChannelResponse;import gov.lbl.dsd.sea.nio.util.ArrayByteList;import java.io.IOException;import java.net.InetSocketAddress;import java.nio.ByteBuffer;import java.nio.channels.SelectionKey;import java.nio.charset.Charset;/** * Simple echo client; connects to one or more HTTP servers and prints page * output. * <p> * Example usage: fire-java gov.lbl.dsd.sea.nio.demo.HttpClient localhost:8080 www.google.com:80 localhost:9999 *  * @author whoschek@lbl.gov * @author $Author: gegles $ * @version $Revision: 1.5 $, $Date: 2004/09/16 16:57:15 $ */public class HttpClient extends AgentEventHandler {	private static final Charset CHARSET = Charset.forName("ISO-8859-1");	private int nrChannels = 0;	private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory			.getLog(HttpClient.class);	static public void main(String args[]) throws IOException {		new HttpClient(args);	}	public HttpClient(String args[]) throws IOException {		NetAgent agent = new NetAgent();		Stage myStage = new StageManager().createStage(this).start();		if (args.length < 1) throw new IllegalArgumentException("usage: java <class> host:port...");		for (int i=0; i < args.length; i++) {			int delim = args[i].indexOf(":");			if (delim < 0) throw new IllegalArgumentException("usage: java <class> host:port...");			String hostName = args[i].substring(0, delim); 			int port = Integer.parseInt(args[i].substring(delim+1, args[i].length()));			agent.addConnectAddress(myStage, new InetSocketAddress(hostName, port));			this.nrChannels++;		}		agent.start();	}	protected void onClosed(ChannelResponse.Closed rsp) {		log.info("*************got channel closed=" + rsp);		if (rsp.getException() != null) {			log.error(rsp.getException());		}		ArrayByteList readBuffer = (ArrayByteList) rsp.getKey().attachment();		if (readBuffer != null) {			System.out.println("*** fully read bytes  = " + readBuffer);					System.out.println("*** fully read string = " + readBuffer.toString(CHARSET));		}		if (--nrChannels == 0) {			this.shutDown(rsp);		}	}	protected void onConnected(ChannelResponse.Connected rsp) {		log.info("*************got connected=" + rsp);		rsp.getKey().attach(new ArrayByteList());		rsp.getAgent().enqueue(				new ChannelRequest.Register(this.getStage(), rsp.getKey()						.channel(), SelectionKey.OP_READ));		ByteBuffer buffer = new ArrayByteList("GET / HTTP/1.0" + "\r\n" + "\r\n", CHARSET).asByteBuffer();		rsp.getAgent().enqueue(				new ChannelRequest.WriteData(rsp.getKey().channel(), buffer));	}	protected void onRead(ChannelResponse.Read rsp) {		ArrayByteList readBuffer = (ArrayByteList) rsp.getKey().attachment();		readBuffer.add(rsp.getBuffer());			}	protected void onAccepted(ChannelResponse.Accepted rsp) {		throw new UnsupportedOperationException();	}	protected void onWrite(ChannelResponse.Write rsp) {}	protected void onRegistered(ChannelResponse.Registered rsp) {}	private void shutDown(ChannelResponse rsp) {		System.out.println("now shutting down...");		this.getStage().stop();		rsp.getAgent().enqueue(new AdminRequest.Stop());	}}

⌨️ 快捷键说明

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