/*
* $Id: AnySocket.java,v 1.15 2002/09/16 08:05:03 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.core.net;
import anvil.core.Any;
import anvil.core.AnyAbstractClass;
import anvil.core.io.AnyInputStream;
import anvil.core.io.AnyOutputStream;
import anvil.script.Context;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
///
/// @class Socket
/// Client socket (also called just "socket").
/// A socket is an endpoint for communication between two machines.
///
/**
* class AnySocket
*
* @author: Jani Lehtim�ki
*/
public class AnySocket extends AnyAbstractClass
{
///
/// @constructor Socket
/// Creates a new socket to given <code>remoteAddr</code> and <code>remotePort</code>.
/// Socket is bound locally either to default address and random port or
/// given <code>localAddr</code> and <code>localPort</code>.
/// @synopsis Socket(string remoteAddr, int remotePort)
/// @synopsis Socket(InetAddress remoteAddr, int remotePort)
/// @synopsis Socket(InetAddress remoteAddr, int remotePort, InetAddress localAddr, int localPort)
/// @synopsis Socket(string remoteAddr, int remotePort, string localAddr, int localPort)
/// @synopsis Socket(InetAddress remoteAddr, int remotePort, string localAddr, int localPort)
/// @synopsis Socket(string remoteAddr, int remotePort, string localAddr, int localPort)
/// @throws IOError if an IO error occured
public static final Object[] newInstance = { null, "address", "port", "*localAddress", null, "*localPort", new Integer(0) };
public static final Any newInstance(Context context, Any remote, int remotePort, Any local, int localPort)
{
try {
InetAddress remoteAddress;
if (remote instanceof AnyInetAddress) {
remoteAddress = (InetAddress)remote.toObject();
} else {
remoteAddress = InetAddress.getByName(remote.toString());
}
context.checkConnect(remoteAddress.getHostName(), remotePort);
if (local == null) {
return new AnySocket(new Socket(remoteAddress, remotePort));
} else {
InetAddress localAddress;
if (local instanceof AnyInetAddress) {
localAddress = (InetAddress)local.toObject();
} else {
localAddress = InetAddress.getByName(local.toString());
}
return new AnySocket(new Socket(remoteAddress, remotePort, localAddress, localPort));
}
} catch (IOException e) {
throw context.exception(e);
}
}
private Socket _socket;
public AnySocket(Socket url)
{
_socket = url;
}
public final anvil.script.ClassType classOf() {
return __class__;
}
public Object toObject()
{
return _socket;
}
/// @method close
/// Closes this socket.
/// @synopsis void close()
/// @throws IOError If an IO error occured
public Any m_close(Context context)
{
try {
_socket.close();
return this;
} catch (IOException e) {
throw context.exception(e);
}
}
/// @method getAddress
/// Returns the remote IP address to which this socket has been bound to.
/// @synopsis InetAddress getAddress()
/// @return remote address
public Any m_getAddress()
{
return new AnyInetAddress(_socket.getInetAddress());
}
/// @method getPort
/// Returns the remote port to which this socket has been bound to.
/// @synopsis int getPort()
/// @return remote port
public Any m_getPort()
{
return Any.create(_socket.getPort());
}
/// @method getLocalAddress
/// Returns the local IP address to which this socket has been bound to.
/// @synopsis InetAddress getLocalAddress()
/// @return local address
public Any m_getLocalAddress()
{
return new AnyInetAddress(_socket.getLocalAddress());
}
/// @method getLocalPort
/// Returns the local port to which this socket has been bound to.
/// @synopsis int getLocalPort()
/// @return port
public Any m_getLocalPort()
{
return Any.create(_socket.getLocalPort());
}
/// @method getInput
/// Gets the input stream of this socket.
/// @synopsis InputStream getInput()
/// @throws IOError If an IO error occured
public Any m_getInput(Context context)
{
try {
return new AnyInputStream(_socket.getInputStream());
} catch (IOException e) {
throw context.exception(e);
}
}
/// @method getOutput
/// Gets the output stream of this socket.
/// @synopsis OutputStream getOutput()
/// @throws IOError If an IO error occured
public Any m_getOutput(Context context)
{
try {
return new AnyOutputStream(new BufferedOutputStream(_socket.getOutputStream()));
} catch (IOException e) {
throw context.exception(e);
}
}
/// @method getSoTimeout
/// Returns the setting for SO_TIMEOUT, in milliseconds.
/// @synopsis int getSoTimeout()
/// @return SO_TIMEOUT, 0 implies that the option is
/// disabled (i.e., timeout of infinity).
/// @throws SocketError If an error occured
public Any m_getSoTimeout(Context context)
{
try {
return Any.create(_socket.getSoTimeout());
} catch (SocketException e) {
throw context.exception(e);
}
}
/// @method setSoTimeout
/// Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds.
/// With this option set to a non-zero timeout, a read() call on the
/// InputStream associated with this Socket will block for only this
/// amount of time. If the timeout expires, a read operation is
/// interrupted , though the Socket is still valid.
/// The option must be enabled prior to
/// entering the blocking operation to have effect.
/// The timeout must be > 0. A timeout of zero
/// is interpreted as an infinite timeout.
/// @synopsis boolean setSoTimeout(int timeout)
/// @throws SocketError If an error occured
public static final Object[] p_setSoTimeout = { null, "timeout" };
public Any m_setSoTimeout(Context context, int timeout)
{
try {
_socket.setSoTimeout(timeout);
return this;
} catch (SocketException e) {
throw context.exception(e);
}
}
/// @method getTcpNoDelay
/// @synopsis boolean getTcpNoDelay()
/// @return the value of TCP_NODELAY option.
/// @throws SocketError If an error occured
public Any m_getTcpNoDelay(Context context)
{
try {
return _socket.getTcpNoDelay() ? TRUE : FALSE;
} catch (SocketException e) {
throw context.exception(e);
}
}
/// @method setTcpNoDelay
/// Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm).
/// @synopsis boolean setTcpNoDelay(boolean on)
/// @throws SocketError If an error occured
public static final Object[] p_setTcpNoDelay = { null, "enabled" };
public Any m_setTcpNoDelay(Context context, boolean enabled)
{
try {
_socket.setTcpNoDelay(enabled);
return this;
} catch (SocketException e) {
throw context.exception(e);
}
}
/// @method getSoLinger
/// Returns setting for SO_LINGER.
/// @synopsis int getSoLinger()
/// @return SO_LINGER, -1 implies that the option is disabled.
/// @throws SocketError If an error occured
public Any m_getSoLinger(Context context)
{
try {
return Any.create(_socket.getSoLinger());
} catch (SocketException e) {
throw context.exception(e);
}
}
/// @method setSoLinger
/// Enable/disable SO_LINGER with the specified linger time in seconds.
/// If the specified timeout value exceeds 65,535 it will be reduced to 65,535.
/// @synopsis boolean setSoLinger(boolean on, int linger)
/// @throws SocketError If an error occured
public static final Object[] p_setSoLinger = { null, "enabled", "value" };
public Any m_setSoLinger(Context context, boolean enabled, int linger)
{
try {
_socket.setSoLinger(enabled, linger);
return this;
} catch (SocketException e) {
throw context.exception(e);
}
}
/// @method getReceiveBufferSize
/// A datagram socket option.
/// @synopsis int getReceiveBufferSize()
/// @return buffer size
/// @throws SocketError If an error occured
public Any m_getReceiveBufferSize(Context context)
{
try {
return Any.create(_socket.getReceiveBufferSize());
} catch (SocketException e) {
throw context.exception(e);
}
}
/// @method setReceiveBufferSize
/// A datagram socket option.
/// @synopsis void setReceiveBufferSize(int bufferSize)
/// @throws SocketError If an error occured
public static final Object[] p_setReceiveBufferSize = { null, "bufferSize" };
public Any m_setReceiveBufferSize(Context context, int bufferSize)
{
try {
_socket.setReceiveBufferSize(bufferSize);
return this;
} catch (SocketException e) {
throw context.exception(e);
}
}
/// @method getSendBufferSize
/// A datagram socket option.
/// @synopsis int getSendBufferSize()
/// @return buffer size
/// @throws SocketError If an error occured
public Any m_getSendBufferSize(Context context)
{
try {
return Any.create(_socket.getSendBufferSize());
} catch (SocketException e) {
throw context.exception(e);
}
}
/// @method setSendBufferSize
/// A datagram socket option.
/// @synopsis void setSendBufferSize(int bufferSize)
/// @throws SocketError If an error occured
public static final Object[] p_setSendBufferSize = { null, "bufferSize" };
public Any m_setSendBufferSize(Context context, int bufferSize)
{
try {
_socket.setSendBufferSize(bufferSize);
return this;
} catch (SocketException e) {
throw context.exception(e);
}
}
public static final anvil.script.compiler.NativeClass __class__ =
new anvil.script.compiler.NativeClass("Socket", AnySocket.class,
//DOC{{
""+
"\n" +
" @class Socket\n" +
" Client socket (also called just \"socket\"). \n" +
" A socket is an endpoint for communication between two machines. \n" +
"\n" +
"\n" +
" @constructor Socket\n" +
" Creates a new socket to given <code>remoteAddr</code> and <code>remotePort</code>. \n" +
" Socket is bound locally either to default address and random port or \n" +
" given <code>localAddr</code> and <code>localPort</code>. \n" +
" @synopsis Socket(string remoteAddr, int remotePort)\n" +
" @synopsis Socket(InetAddress remoteAddr, int remotePort)\n" +
" @synopsis Socket(InetAddress remoteAddr, int remotePort, InetAddress localAddr, int localPort)\n" +
" @synopsis Socket(string remoteAddr, int remotePort, string localAddr, int localPort)\n" +
" @synopsis Socket(InetAddress remoteAddr, int remotePort, string localAddr, int localPort)\n" +
" @synopsis Socket(string remoteAddr, int remotePort, string localAddr, int localPort)\n" +
" @throws IOError if an IO error occured\n" +
" @method close\n" +
" Closes this socket. \n" +
" @synopsis void close()\n" +
" @throws IOError If an IO error occured\n" +
" @method getAddress\n" +
" Returns the remote IP address to which this socket has been bound to.\n" +
" @synopsis InetAddress getAddress()\n" +
" @return remote address\n" +
" @method getPort\n" +
" Returns the remote port to which this socket has been bound to.\n" +
" @synopsis int getPort()\n" +
" @return remote port\n" +
" @method getLocalAddress\n" +
" Returns the local IP address to which this socket has been bound to.\n" +
" @synopsis InetAddress getLocalAddress()\n" +
" @return local address\n" +
" @method getLocalPort\n" +
" Returns the local port to which this socket has been bound to.\n" +
" @synopsis int getLocalPort()\n" +
" @return port\n" +
" @method getInput\n" +
" Gets the input stream of this socket. \n" +
" @synopsis InputStream getInput()\n" +
" @throws IOError If an IO error occured\n" +
" @method getOutput\n" +
" Gets the output stream of this socket. \n" +
" @synopsis OutputStream getOutput()\n" +
" @throws IOError If an IO error occured\n" +
" @method getSoTimeout\n" +
" Returns the setting for SO_TIMEOUT, in milliseconds.\n" +
" @synopsis int getSoTimeout()\n" +
" @return SO_TIMEOUT, 0 implies that the option is\n" +
" disabled (i.e., timeout of infinity).\n" +
" @throws SocketError If an error occured\n" +
" @method setSoTimeout\n" +
" Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. \n" +
" With this option set to a non-zero timeout, a read() call on the \n" +
" InputStream associated with this Socket will block for only this \n" +
" amount of time. If the timeout expires, a read operation is\n" +
" interrupted , though the Socket is still valid. \n" +
" The option must be enabled prior to\n" +
" entering the blocking operation to have effect. \n" +
" The timeout must be > 0. A timeout of zero\n" +
" is interpreted as an infinite timeout.\n" +
" @synopsis boolean setSoTimeout(int timeout)\n" +
" @throws SocketError If an error occured\n" +
" @method getTcpNoDelay\n" +
" @synopsis boolean getTcpNoDelay()\n" +
" @return the value of TCP_NODELAY option.\n" +
" @throws SocketError If an error occured\n" +
" @method setTcpNoDelay\n" +
" Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm).\n" +
" @synopsis boolean setTcpNoDelay(boolean on)\n" +
" @throws SocketError If an error occured\n" +
" @method getSoLinger\n" +
" Returns setting for SO_LINGER.\n" +
" @synopsis int getSoLinger()\n" +
" @return SO_LINGER, -1 implies that the option is disabled.\n" +
" @throws SocketError If an error occured\n" +
" @method setSoLinger\n" +
" Enable/disable SO_LINGER with the specified linger time in seconds. \n" +
" If the specified timeout value exceeds 65,535 it will be reduced to 65,535.\n" +
" @synopsis boolean setSoLinger(boolean on, int linger)\n" +
" @throws SocketError If an error occured\n" +
" @method getReceiveBufferSize\n" +
" A datagram socket option.\n" +
" @synopsis int getReceiveBufferSize()\n" +
" @return buffer size\n" +
" @throws SocketError If an error occured\n" +
" @method setReceiveBufferSize\n" +
" A datagram socket option.\n" +
" @synopsis void setReceiveBufferSize(int bufferSize)\n" +
" @throws SocketError If an error occured\n" +
" @method getSendBufferSize\n" +
" A datagram socket option.\n" +
" @synopsis int getSendBufferSize()\n" +
" @return buffer size\n" +
" @throws SocketError If an error occured\n" +
" @method setSendBufferSize\n" +
" A datagram socket option.\n" +
" @synopsis void setSendBufferSize(int bufferSize)\n" +
" @throws SocketError If an error occured\n"
//}}DOC
);
static {
NetModule.class.getName();
}
}