org.jawk.ext
Class SocketExtension
java.lang.Object
org.jawk.ext.AbstractExtension
org.jawk.ext.SocketExtension
- All Implemented Interfaces:
- JawkExtension
public class SocketExtension
- extends AbstractExtension
Enable Socket processing in Jawk.
To use:
## example echo server using CServerSocket (character-based)
BEGIN {
css = CServerSocket(7777);
}
$0 = SocketAcceptBlock(css,
SocketInputBlock(handles,
SocketCloseBlock(css, handles \
)));
$1 == "SocketAccept" {
handles[SocketAccept($2)] = 1
}
$1 == "SocketClose" {
SocketClose($2)
delete handles[$2]
}
$1 == "SocketInput" {
input = SocketRead($2)
SocketWrite($2, input); ## do the echo
}
The extension functions are as follows:
- ServerSocket -
Sets up a server socket to listen for incomming
connections. SocketRead on sockets accepted
by ServerSocket return arbitrary-length Strings
(bytes buffered by the input stream, converted
to a string).
Parameters:
Returns:
- A string handle to a serversocket.
- CServerSocket -
Sets up a server socket to listen for incomming
connections. SocketRead on sockets accepted
by CServerSocket return strings which terminate
by a newline, or text in the input buffer just
prior to the closing of the socket.
Parameters:
Returns:
- A string handle to a cserversocket.
- Socket -
Create a Socket and connect it to a TCP socket
endpoint. SocketRead on sockets returned
by Socket return arbitrary-length Strings
(bytes buffered by the input stream, converted
to a string).
Parameters:
- hostname/ip/"localhost" - required
- port number - required
Returns:
- A string handle to a socket.
- CSocket -
Create a Socket and connect it to a TCP socket
endpoint. SocketRead on sockets returned
by Socket return strings which terminate
by a newline, or text in the input buffer just
prior to the closing of the socket.
Parameters:
- hostname/ip/"localhost" - required
- port number - required
Returns:
- A string handle to a csocket.
- SocketAcceptBlock -
Blocks until a serversocket or cserversocket
is ready to accept a connecting socket.
Parameters:
- Any mix of
serversocket or cserversocket handles
and/or associative arrays whose keys
are serversocket or cserversocket handles.
The last argument can optionally be
another block call for block chaining.
Returns:
- A string of the form:
SocketAcceptOFShandle
where handle is a serversocket or cserversocket
handle.
- SocketInputBlock -
Blocks until a socket or csocket is ready
to accept input (via SocketRead).
Parameters:
- Any mix of
socket or csocket handles and/or associative
arrays whose keys are socket or csocket handles.
The last argument can optionally be
another block call for block chaining.
Returns:
- A string of the form:
SocketInputOFShandle
where handle is a socket or csocket
handle.
- SocketCloseBlock -
Blocks until a serversocket, cserversocket,
socket, or csocket has been closed on the
remote end.
Parameters:
- Any mix of
serversocket, cserversocket, socket, or csocket
handles and/or associative
arrays whose keys are serversocket, cserversocket,
socket, or csocket handles.
The last argument can optionally be
another block call for block chaining.
Returns:
- A string of the form:
SocketCloseOFShandle
where handle is a serversocket, cserversocket, socket,
or csocket handle.
- SocketAccept -
Accepts a socket from a serversocket or
a cserversocket. The operation will
block if there is no socket to accept.
Parameters:
- serversocket-or-cserversocket handle - required
Returns:
- A string handle to a socket or csocket.
- SocketRead -
Reads input from the input stream of a socket
or a csocket. For a socket, the input length
is arbitrary. For a csocket, the input
length is bounded by a newline or upon
termination of the socket.
The operation will block if there is no input
on the socket.
Parameters:
- socket-or-csocket handle - required
Returns:
- A string containing the input on the socket.
- SocketWrite -
Writes data to the socket or csocket.
For a socket, the string is converted
to bytes (via java.lang.String.getBytes()),
and the bytes are sent to the socket's
output stream.
For a csocket, println() is called on the
underlying socket's PrintStream.
Parameters:
- socket-or-csocket handle - required
- msg - required - The string to write to
the socket. For a csocket, a newline
is added to it (via the
java.io.PrintStream.println() method).
Returns:
- 1 upon a successful write, 0 otherwise
- SocketFlush -
Flushes the output stream of a socket or csocket.
Parameters:
- socket-or-csocket handle - required
Returns:
- 1 upon a successful flush, 0 otherwise
- SocketClose -
Closes the socket/csocket on the local end,
or a serversocket/cserversocket.
Can be called in response to a SocketCloseBlock
event, or to force a socket/csocket connection to
terminate.
Parameters:
- socket/csocket/serversocket/cserversocket handle - required
Returns:
- 1 upon successful close, 0 otherwise
Method Summary |
java.lang.String[] |
extensionKeywords()
All the extended keywords supported
by this extension. |
java.lang.String |
getExtensionName()
The name of the extension package. |
void |
init(VariableManager vm,
JRT jrt)
Called after the creation and before normal processing of the
extension, pass in the Jawk Runtime Manager
and the Variable Manager once. |
java.lang.Object |
invoke(java.lang.String method_name,
java.lang.Object[] args)
Invoke extension as a method. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
SocketExtension
public SocketExtension()
init
public final void init(VariableManager vm,
JRT jrt)
- Description copied from interface:
JawkExtension
- Called after the creation and before normal processing of the
extension, pass in the Jawk Runtime Manager
and the Variable Manager once.
It is guaranteed init() is called before invoke() is called.
- Specified by:
init
in interface JawkExtension
- Overrides:
init
in class AbstractExtension
getExtensionName
public final java.lang.String getExtensionName()
- Description copied from interface:
JawkExtension
- The name of the extension package.
extensionKeywords
public final java.lang.String[] extensionKeywords()
- Description copied from interface:
JawkExtension
- All the extended keywords supported
by this extension.
Note: Jawk will
throw a runtime exception if the
keyword collides with any other keyword
in the system, extension or otherwise.
invoke
public final java.lang.Object invoke(java.lang.String method_name,
java.lang.Object[] args)
- Description copied from interface:
JawkExtension
- Invoke extension as a method.
- Parameters:
method_name
- The extension keyword.args
- Arguments to the extension.
- Returns:
- The return value (result) of the extension.