org.jpop.net
Class PoplogDocClient

java.lang.Object
  extended byorg.jpop.net.PoplogDocClient
All Implemented Interfaces:
ClientInterface, java.lang.Runnable
Direct Known Subclasses:
PoplogCachedClient

public abstract class PoplogDocClient
extends java.lang.Object
implements ClientInterface

Creates a Doc Client that sends requests to the server and gets responses from it in the form of messages

Author:
ug57dsm

Field Summary
static java.lang.String ERROR_MESG
          Prepended to the response string to indicate the nature of the message.
static java.lang.String SERVER_MESG
          Prepended to the response string to indicate the nature of the message.
static java.lang.String TEXT_MESG
          Prepended to the response string to indicate the nature of the message.
static java.lang.String WARNING_MESG
          Prepended to the response string to indicate the nature of the message.
 
Constructor Summary
PoplogDocClient()
          Default constructor.
PoplogDocClient(java.lang.String serverName, int port)
          Better constructor.
 
Method Summary
abstract  void clientConnected()
          Fired whenever the client connects to a server
abstract  void clientDisconnected()
          Fired whenever the client closes the conection to the server
 void close()
          Closes the connection to the server, disowns any lists that were got from it and sends out the client disconnected event.
 void connect(java.lang.String servername, int port)
          Connect to the server.
abstract  void docReceived(java.io.BufferedReader doc)
          Fired whenever a document from the server is received.
abstract  void errorReceived(java.io.BufferedReader err)
          Fired whenever an error from the server is received.
abstract  void exceptionGenerated(java.lang.Exception e)
          Fired whenever an exception is generated and for some reason the program should not be halted whilst alerting the user anyway
 void getDoc(java.lang.String type, java.lang.String name)
          send a doc request to sever.
 void getHelp()
          send a help request to sever
 void getInfo()
          send an info request to sever
 void getLicense()
          send a license request to sever
 void getList(java.lang.String type)
          send a list request to sever
 int getPort()
          Get the port of the server we are connected to
 java.lang.String getServerName()
          Get the name of the server we are connected to
 void getVersion()
          send a version request to sever
 void help()
          Help for the client
abstract  void helpReceived(java.io.BufferedReader hlp)
          Fired whenever a help document from the server is received.
 void info()
          info for the client
 boolean isConnected()
          Check if the client is connected
 void license()
          license for the client
abstract  void licenseReceived(java.io.BufferedReader lic)
          Fired whenever a license document from the server is received.
abstract  void listReceived(java.io.BufferedReader list)
          Fired whenever a list from the server is received.
static void main(java.lang.String[] args)
          Main method.
abstract  void messageReceived(java.io.BufferedReader msg)
          Fired whenever a message from the server is received.
 void parse(java.lang.String line)
          Parses a line of text into a statement that can be sent to the server.
 void println(java.lang.String line)
          Prints a line to the server
 java.lang.String resolveDocName(java.lang.String type, java.lang.String name)
          Check whether the doc name we have actually exists, and return a modified version in the correct case-sensitive syntax.
 void run()
          Runs a service loop continually receiving messages from the server and sending out the appropriate messages.
 void version()
          version of the client
abstract  void warningReceived(java.io.BufferedReader warn)
          Fired whenever a warning from the server is received.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

WARNING_MESG

public static final java.lang.String WARNING_MESG
Prepended to the response string to indicate the nature of the message. Forwarded from ProtocolPrefixes for ease of use.

See Also:
Constant Field Values

ERROR_MESG

public static final java.lang.String ERROR_MESG
Prepended to the response string to indicate the nature of the message. Forwarded from ProtocolPrefixes for ease of use.

See Also:
Constant Field Values

TEXT_MESG

public static final java.lang.String TEXT_MESG
Prepended to the response string to indicate the nature of the message. Forwarded from ProtocolPrefixes for ease of use.

See Also:
Constant Field Values

SERVER_MESG

public static final java.lang.String SERVER_MESG
Prepended to the response string to indicate the nature of the message. Forwarded from ProtocolPrefixes for ease of use.

See Also:
Constant Field Values
Constructor Detail

PoplogDocClient

public PoplogDocClient()
Default constructor. An object created using this needs to explicitly call connect(String,int) in order to communicate with a server


PoplogDocClient

public PoplogDocClient(java.lang.String serverName,
                       int port)
                throws java.net.UnknownHostException,
                       java.io.IOException
Better constructor. An object created using this doesn't need to connect in order to communicate with a server as the constructor deals with this

Parameters:
serverName - The name of the server where an instance of the PoplogDocServer is running
port - The port the server is listening to
Throws:
java.net.UnknownHostException
java.io.IOException
java.net.UnknownHostException - When the server can't be reached
java.io.IOException - If the connection can't be made
Method Detail

main

public static void main(java.lang.String[] args)
Main method. Just to test the functionality

Parameters:
args - The commands line arguments

getServerName

public java.lang.String getServerName()
Get the name of the server we are connected to

Returns:
The serverName variable

getPort

public int getPort()
Get the port of the server we are connected to

Returns:
The port variable

isConnected

public boolean isConnected()
Check if the client is connected

Specified by:
isConnected in interface ClientInterface
Returns:
true when connected, false otherwise

getDoc

public void getDoc(java.lang.String type,
                   java.lang.String name)
            throws ClientException
send a doc request to sever. Beware, this might take a while to work as it has to fetch a list from the server to verify the case of the filename... Will be m,uch easier once the cache is implemented. This forwards the call to the private getDoc() which handles the complicated stuff.

Specified by:
getDoc in interface ClientInterface
Parameters:
type - The type of the request (REF, HELP, DOC, or TEACH)
name - The name of the document to fetch ("filename" or "")
Throws:
ClientException

getHelp

public void getHelp()
send a help request to sever

Specified by:
getHelp in interface ClientInterface

getInfo

public void getInfo()
send an info request to sever

Specified by:
getInfo in interface ClientInterface

getLicense

public void getLicense()
send a license request to sever

Specified by:
getLicense in interface ClientInterface

getList

public void getList(java.lang.String type)
send a list request to sever

Specified by:
getList in interface ClientInterface
Parameters:
type -

getVersion

public void getVersion()
send a version request to sever

Specified by:
getVersion in interface ClientInterface

connect

public void connect(java.lang.String servername,
                    int port)
             throws java.net.UnknownHostException,
                    java.io.IOException
Connect to the server. It is not necessary to call this method when the constructor has been given the server details as the constructor works it out itself. If this is called once a connection has been made, The client disconnects from the server and then reconnects to the new one

Specified by:
connect in interface ClientInterface
Parameters:
port - The port the server is listening to
servername -
Throws:
java.net.UnknownHostException - When the server can't be reached
java.io.IOException - If the connection can't be made

close

public void close()
           throws java.io.IOException
Closes the connection to the server, disowns any lists that were got from it and sends out the client disconnected event. If the client was not connected to start with this method does nothing.

Specified by:
close in interface ClientInterface
Throws:
java.io.IOException - When the client cannot disconnect

run

public void run()
Runs a service loop continually receiving messages from the server and sending out the appropriate messages. If the client is not connected the service loop doesn't start at all, it just waits until a connection is established, checking the status every few seconds.

Specified by:
run in interface ClientInterface

parse

public void parse(java.lang.String line)
           throws ClientException
Parses a line of text into a statement that can be sent to the server. This method will do its best to find a match, to begin with it will try to find a file that matches exactly the name given interfacethe command, failing that it will try a caps-insensitive search of the relevant listing and if a file matches this will be used, interfacecase there is no match an exception will be thrown

Specified by:
parse in interface ClientInterface
Parameters:
line - The text to parse
Throws:
ClientException - When something goes wrong

help

public void help()
Help for the client


version

public void version()
version of the client


info

public void info()
info for the client


license

public void license()
license for the client


println

public void println(java.lang.String line)
Prints a line to the server

Specified by:
println in interface ClientInterface
Parameters:
line - The line to print

resolveDocName

public java.lang.String resolveDocName(java.lang.String type,
                                       java.lang.String name)
Check whether the doc name we have actually exists, and return a modified version in the correct case-sensitive syntax. VERY IMPORTANT for file access later on

Parameters:
type - The type of document we want
name - The tentative name of the document
Returns:
The actual name of the document or null when the name required does not exist

docReceived

public abstract void docReceived(java.io.BufferedReader doc)
Fired whenever a document from the server is received.

Specified by:
docReceived in interface ClientInterface
Parameters:
doc - A Reader pointing top the beginning of the document

errorReceived

public abstract void errorReceived(java.io.BufferedReader err)
Fired whenever an error from the server is received.

Specified by:
errorReceived in interface ClientInterface
Parameters:
err - A Reader pointing top the beginning of the error

exceptionGenerated

public abstract void exceptionGenerated(java.lang.Exception e)
Fired whenever an exception is generated and for some reason the program should not be halted whilst alerting the user anyway

Specified by:
exceptionGenerated in interface ClientInterface
Parameters:
e - The exception

helpReceived

public abstract void helpReceived(java.io.BufferedReader hlp)
Fired whenever a help document from the server is received.

Specified by:
helpReceived in interface ClientInterface
Parameters:
hlp - A Reader pointing top the beginning of the document

licenseReceived

public abstract void licenseReceived(java.io.BufferedReader lic)
Fired whenever a license document from the server is received.

Specified by:
licenseReceived in interface ClientInterface
Parameters:
lic - A Reader pointing top the beginning of the document

listReceived

public abstract void listReceived(java.io.BufferedReader list)
Fired whenever a list from the server is received.

Specified by:
listReceived in interface ClientInterface
Parameters:
list - A Reader pointing top the beginning of the list

messageReceived

public abstract void messageReceived(java.io.BufferedReader msg)
Fired whenever a message from the server is received.

Specified by:
messageReceived in interface ClientInterface
Parameters:
msg - A Reader pointing top the beginning of the document

warningReceived

public abstract void warningReceived(java.io.BufferedReader warn)
Fired whenever a warning from the server is received.

Specified by:
warningReceived in interface ClientInterface
Parameters:
warn - A Reader pointing top the beginning of the warning

clientConnected

public abstract void clientConnected()
Fired whenever the client connects to a server

Specified by:
clientConnected in interface ClientInterface

clientDisconnected

public abstract void clientDisconnected()
Fired whenever the client closes the conection to the server

Specified by:
clientDisconnected in interface ClientInterface