Apache-HTTunnel

 view release on metacpan or  search on metacpan

Client/lib/HTTunnel/Client.java  view on Meta::CPAN

import java.net.* ;


public class Client {
	private URL url = null ;
	private String fhid = null ;
	private String proto = null ;
	private String peer_info = null ;


	public Client(String _url) throws MalformedURLException {
		url = new URL(_url) ;
	}


	public int connect(String proto, String host, int port) throws ClientException {
		return connect(proto, host, port, 0) ;
	}

	public int connect(String _proto, String host, int port, int timeout) throws ClientException {
		proto = _proto ;
		if ((proto == null)||(proto.equals(""))){
			proto = "tcp" ;
		}
		if ((host == null)||(host.equals(""))){
			host = "localhost" ;
		}
		if (timeout <= 0){
			timeout = 15 ;
		}

Client/lib/HTTunnel/Client.java  view on Meta::CPAN

				sbfhid.append(st.nextToken()) ;
			}
			fhid = sbfhid.toString() ;
			peer_info = addr + ":" + port ;
		}

		return 1 ;
	}


	public String read(int len) throws ClientException {
		return read(len, 0) ;
	}

	public String read(int len, int timeout) throws ClientException {
		if (timeout <= 0){
			timeout = 15 ;
		}

		if (fhid == null){
			throw new ClientException("HTTunnel.Client object is not connected") ;
		}

		while (true){
			String addr = null ;
			String port = null ;
			String data = null ;
			try {
				data = execute(
					"read", 
					new String [] { fhid, proto, (new Integer(len)).toString(),

Client/lib/HTTunnel/Client.java  view on Meta::CPAN

						sbdata.append(st.nextToken()) ;
					}
					peer_info = addr + ":" + port ;
					data = sbdata.toString() ;
				}
			}
			catch (ClientTimeoutException hcte){
				continue ;
			}
			catch (ClientException hce){
				throw hce ;
			}

			return data ;
		}
	}


	public String get_peer_info(){
		return peer_info ;
	}


	public int print(String data) throws ClientException {
		if (fhid == null){
			throw new ClientException("HTTunnel.Client object is not connected") ;
		}

		execute(
			"write",
			new String [] { fhid, proto },
			data
		) ;

		return 1 ;
	}


	public int close() throws ClientException {
		if (fhid != null){
			execute(
				"close",
				new String [] { fhid }
			) ;
			fhid = null ;

			return 1 ;
		}
	
		return 0 ;
	}


	private String execute(String cmd, String args[]) throws ClientException {
		return execute(cmd, args, null) ;
	}
	
	
	private String execute(String cmd, String args[], String data) throws ClientException {
		StringBuffer furlsb = new StringBuffer(url.toString() + "/" + cmd) ;
		for (int i = 0 ; i < args.length ; i++){
			furlsb.append("/" + args[i]) ;
		}

		HttpURLConnection huc = null ;
		InputStream is = null ;
		StringBuffer rdata = new StringBuffer() ;
		try {
			URL furl = new URL(furlsb.toString()) ;

Client/lib/HTTunnel/Client.java  view on Meta::CPAN

			if (data != null){
				OutputStream os = huc.getOutputStream() ;
				os.write(data.getBytes()) ;
				os.flush() ;
				os.close() ;
			}

			response_callback(huc) ;
			is = huc.getInputStream() ;
			if (huc.getResponseCode() != HttpURLConnection.HTTP_OK){
				throw new ClientException("HTTP error : " + huc.getResponseCode() + 
					" (" + huc.getResponseMessage() + ")") ;
			}

			byte buf[] = new byte[16834] ;
			int len = 0 ;
			while ((len = is.read(buf)) != -1){
				rdata.append(new String(buf, 0, len)) ;
			}
		}
		catch (IOException ioe){
			throw new ClientException(ioe.getClass().getName() + ": " + ioe.getMessage()) ;
		}
		finally {
			if (is != null){
				try {
			     	is.close() ;
				}
				catch (IOException ioe){}
			}
			if (huc != null){
				huc.disconnect() ;
			}
		}

		String content = rdata.toString() ;
		String code = content.substring(0, 3) ;
		if (code.equals("err")){
			throw new ClientException("Apache::HTTunnel error:" + content.substring(3)) ;
		}
		else if (code.equals("okn")){
			return null ;
		}
		else if (code.equals("okd")){
			return content.substring(3) ;
		}
		else if (code.equals("okt")){
			throw new ClientTimeoutException() ;
		}
		else{
			throw new ClientException("Invalid Apache::HTTunnel response code '" + code + "'") ;
		}
	}


	protected void request_callback(HttpURLConnection huc){
	}


	protected void response_callback(HttpURLConnection huc){
	}

Client/lib/HTTunnel/Client.js  view on Meta::CPAN

		return 1 ;
	}


	this.read = function(len, timeout, callback){
		if (timeout <= 0){
			timeout = 15 ;
		}

		if (this.fhid == null){
			throw("HTTunnelClient object is not connected") ;
		}

		if (callback){
			var htc = this ;
			this.execute("read", new Array(this.fhid, this.proto, len, timeout), null, function(data, exception){
				// alert("data:" + data + ", exception:" + exception) ;
				if (exception == "timeout"){
					htc.read(len, timeout, callback) ;
				}
				else if (exception != null){

Client/lib/HTTunnel/Client.js  view on Meta::CPAN

	}


	this.get_peer_info = function(){
		return this.peer_info ;
	}


	this.print = function(data, callback){
		if (this.fhid == null){
			throw("HTTunnel.Client object is not connected") ;
		}

		this.execute("write", new Array(this.fhid, this.proto), data, (! callback ? null : function(exception){
			callback(exception) ;
		})) ;

		return 1 ;
	}


Client/lib/HTTunnel/Client.js  view on Meta::CPAN

		}
		else {
			return 1 ;
		}
	}


	this._post_execute = function(content){
		var code = content.substring(0, 3) ;
		if (code == "err"){
			throw("Apache::HTTunnel error:" + content.substring(3)) ;
		}
		else if (code == "okn"){
			return null ;
		}
		else if (code == "okd"){
			return content.substring(3) ;
		}
		else if (code == "okt"){
			throw("timeout") ;
		}
		else {
			throw("Invalid Apache::HTTunnel response code '" + code + "'") ;
		}
	}


	// This code is isolated since it is less portable.
	this._xmlhttprequest = function(url, data, callback){
		var req = (window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP")) ;
		req.open("POST", url, (callback ? true : false)) ;
		this.request_callback(req) ;
		if (callback){
			var htc = this ;
			req.onreadystatechange = function(){
				if (req.readyState == 4){
					if (req.status == 200){
						htc.response_callback(req) ;
						var content = req.responseText ;
						// alert(content) ;
						callback(content) ;
					}
					else {
						throw("HTTP error: " + req.status + " (" + req.statusText + ")") ;
					}
				}
			} ;
			req.send(data) ;
			return 1 ;
		}
		else {
			req.send(data) ;
			this.response_callback(req) ;
			var content = req.responseText ;

Client/lib/HTTunnel/Client.pod  view on Meta::CPAN

=back


=head1 METHODS

=over 4

=item connect ( PROTO, HOST, PORT, [TIMEOUT] )

Asks the C<Apache::HTTunnel> server to establish a connection of protocol
C<PROTO> to C<HOST>:C<PORT>. An exception is thrown if an error occurs.

Accepted values for C<PROTO> are 'tcp' and 'udp'.

=item print ( DATA )

Asks the C<Apache::HTTunnel> server to write C<DATA> to the established 
remote connection. An exception is thrown if an error occurs.

C<DATA> can be a scalar or a list, in which case the list items are 
concatenated together.

=item read ( LEN, [TIMEOUT], [LIFELINE], [LIFELINE_CUT_ACTION] )

Asks the C<Apache::HTTunnel> server to read up to C<LEN> bytes from
the established remote connection. An exception is thrown if an error occurs.

When trying to read, C<HTTunnel::Client> will establish an HTTP connection
to the C<Apache::HTTunnel> server asking that C<LEN> bytes be read. If no 
data is available after C<TIMEOUT> seconds (the default value is 15 seconds),
the HTTP connection is closed by the server and the C<read> method will establish
a new one. This will go on until some data or EOF is returned.

Therefore C<read> will return only when some (or no more) data is available
to be read (like the regular L<read>).



( run in 0.288 second using v1.01-cache-2.11-cpan-496ff517765 )