com.monkeycoder.monkeynetwork
Class Network

java.lang.Object
  extended by java.util.Observable
      extended by com.monkeycoder.monkeynetwork.Network
All Implemented Interfaces:
java.util.Observer
Direct Known Subclasses:
FtpNetwork, HttpNetwork

public abstract class Network
extends java.util.Observable
implements java.util.Observer

This is the super class for all other network objects within the MonkeyNetwork system. Generally this class expects to be observed by a class outside of the MonkeyNetwork system and to observe its ProtocolStateEngine.

This class uses monkeynetwork.ini and requires the following parameters:

Author:
David Stephens

Field Summary
protected  java.net.InetSocketAddress conAddr
          The underlying InetSocketAddress used to create the connection.
protected  java.io.InputStream inStream
          The underlying InputStream.
protected  java.io.OutputStream outStream
          The underlying OutputStream.
protected  java.net.Socket sock
          The underlying Socket.
protected  ProtocolStateEngine stateEngine
          The underlying state engine
 
Constructor Summary
protected Network()
          Super Constructor, this is where Network type inspecific things are done.
 
Method Summary
protected  void connect()
          Connects this network.
protected  void disconnect()
          Disconnects this network.
abstract  void fetch(java.lang.String inUri)
          Fetches web content via the transport medium specified by this network.
 java.io.InputStream getContent()
          Gets the downloaded content.
 java.lang.String getEndpointHost()
          Get the host of the Endpoint.
 int getEndpointPort()
          Get the port of the Endpoint.
protected  java.io.InputStream getInputStream()
          Gets an InputStream for this network connection.
abstract  int getNetworkProtocol()
          Get the protocol of this network object.
protected  java.io.OutputStream getOutputStream()
          Gets an OutputStream for this network connection.
 boolean isConnected()
          Check to see whether this network is presently connected.
 void setEndpoint(java.lang.String inHost, int inPort)
          Set the Endpoint of this connection.
abstract  void update(java.util.Observable inObservable, java.lang.Object inArg)
          Used by the network state engine to pass messages to this class.
 
Methods inherited from class java.util.Observable
addObserver, clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers, setChanged
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

sock

protected java.net.Socket sock
The underlying Socket.


inStream

protected java.io.InputStream inStream
The underlying InputStream.


outStream

protected java.io.OutputStream outStream
The underlying OutputStream.


conAddr

protected java.net.InetSocketAddress conAddr
The underlying InetSocketAddress used to create the connection.


stateEngine

protected ProtocolStateEngine stateEngine
The underlying state engine

Constructor Detail

Network

protected Network()
Super Constructor, this is where Network type inspecific things are done.

Method Detail

connect

protected void connect()
                throws java.io.IOException,
                       MonkeyException
Connects this network.

Throws:
java.io.IOException - If the attempt to connect this network failed.
MonkeyException - If the endpoint of this connection has not been set.

disconnect

protected void disconnect()
                   throws java.io.IOException
Disconnects this network. This method will try to close the connection only if one was already established. If there is no current connection, this method will finish gracefully without notification of the pointless call.

Throws:
java.io.IOException - If the attempt to disconnect this network failed. (Genuine failure to close the socket.)

getInputStream

protected java.io.InputStream getInputStream()
Gets an InputStream for this network connection.

Returns:
A live InputStream to the connected network or null if this network is not presently connected.

getOutputStream

protected java.io.OutputStream getOutputStream()
Gets an OutputStream for this network connection.

Returns:
A live OutputStream to the connected network or null if this network is not presently connected.

setEndpoint

public void setEndpoint(java.lang.String inHost,
                        int inPort)
Set the Endpoint of this connection.

Parameters:
inHost - The host to which this network will be connected.
inPort - The port through which this network will be connected.

getEndpointHost

public java.lang.String getEndpointHost()
Get the host of the Endpoint. This could be either a host name or and IP address.

Returns:
The host of the endpoint.

getEndpointPort

public int getEndpointPort()
Get the port of the Endpoint.

Returns:
The port of the endpoint.

isConnected

public boolean isConnected()
Check to see whether this network is presently connected.

Returns:
Whether or not this network is connected.

getContent

public java.io.InputStream getContent()
                               throws java.io.IOException
Gets the downloaded content. This should only be called once an UpdateMessage has been received indicating that the content is ready.

Returns:
An InputStream to the downloaded content.
Throws:
java.io.IOException - If the underlying TurboBuffer fails to produce an appropriate InputStream.
See Also:
TurboBuffer, UpdateMessage

getNetworkProtocol

public abstract int getNetworkProtocol()
Get the protocol of this network object.

Returns:
The protocol of this network.
See Also:
NetworkProtocol

fetch

public abstract void fetch(java.lang.String inUri)
                    throws java.io.IOException,
                           MonkeyException
Fetches web content via the transport medium specified by this network. This method does not return the content directly and returns immediately after setting up the requested transfer. Once the transfer is complete (or fails) an UpdateMessage will be issued to observers.

Parameters:
inUri - The content being requested. This is a partial URI based on the established connection.
Throws:
java.io.IOException - When thrown by underlying network code.
MonkeyException - If the network is not yet connected.
See Also:
UpdateMessage

update

public abstract void update(java.util.Observable inObservable,
                            java.lang.Object inArg)
Used by the network state engine to pass messages to this class.

Specified by:
update in interface java.util.Observer
Parameters:
inObservable - The observable object.
inArg - The UpdateMessage passed to the notifyObservers method.
See Also:
UpdateMessage