Class FishServerHandler

java.lang.Object
  |
  +--java.lang.Thread
        |
        +--FishServerHandler
All Implemented Interfaces:
java.lang.Runnable

public class FishServerHandler
extends java.lang.Thread

Thread to handle clients interaction with the server.

Version:
1.0
Author:
Björn Bertilsson

Nested Class Summary
 class FishServerHandler.FilePost
          Post which contains information on the files
 class FishServerHandler.SharePost
          Post to put in the global dir with info. on the client and its files.
 
Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
FishServerHandler(java.net.Socket cSocket)
          Initializing the in- and outputstream between the client and the server.
 
Method Summary
 void addClientShare(java.lang.String ipAddress, int cSharePort, java.util.Vector cShare)
          Adds a share for the client to the global dir.
static void addFilesSize(long num)
          Increase statistics on total size of shared files.
static void addNumFiles(int num)
          Increase statistics on total number of shared files.
static void decConnectedClients()
          Decrease statistics on number of currently connected clients.
static int getClientsDuringUptime()
          Number of clients that have connected to the server since it started.
static int getConnectedClients()
          Statistics on number of currently connected clients.
static long getFilesSize()
          Statistics on total size of shared files.
static int getNumFiles()
          Statistics on total number of shared files.
static void incConnectedClients()
          Increase statistics on number of currently connected clients.
 void ping()
          Respond with statistics when the server is pinged.
 void pong()
          Check if client is still alive, if not disconnect.
 void printLog(java.lang.String text)
          Print the unique number for the client and the message in the log window.
 java.lang.String readLineLow()
          Reads a line from the client and convert it to lowercase.
 java.lang.String readLineLowExc()
          Reads a line from the client and convert it to lowercase.
 void removeClientShare(java.lang.String ipAndPort)
          Removes a share for the client in the global dir, by searching the global list with the unique combination of clients IP-address and portnumber.
static void removeFilesSize(long num)
          Decrease statistics on total size of shared files.
static void removeNumFiles(int num)
          Decrease statistics on total number of shared files.
 void run()
          Start the thread, and accept incomming commands.
 void search()
          Search for files in the global list with all clients files.
 void shareFiles()
          Shared files are put in a vector and associated with client, then to be put in the global searchable list.
 void unknownCommand(java.lang.String cmd)
          Function to handle invalid or unknown requests.
 void unshareFiles()
          Unshare clients files when they are not available any longer.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

FishServerHandler

public FishServerHandler(java.net.Socket cSocket)
Initializing the in- and outputstream between the client and the server.

Parameters:
cSocket - Socket from the connected client.
Method Detail

run

public void run()
Start the thread, and accept incomming commands.

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread

readLineLow

public java.lang.String readLineLow()
Reads a line from the client and convert it to lowercase.

Returns:
Read line from the client. Null if exception is thrown.

readLineLowExc

public java.lang.String readLineLowExc()
                                throws java.io.IOException
Reads a line from the client and convert it to lowercase.

Returns:
Read line from the client.
Throws:
java.io.IOException - Thrown when read from client fails.

printLog

public void printLog(java.lang.String text)
Print the unique number for the client and the message in the log window.

Parameters:
text - Text to be displayd.

pong

public void pong()
Check if client is still alive, if not disconnect. And if it isn't sharing any files disconnect too. All clients should share something, otherwise there wouldn't be any files to download.


shareFiles

public void shareFiles()
Shared files are put in a vector and associated with client, then to be put in the global searchable list.


unshareFiles

public void unshareFiles()
Unshare clients files when they are not available any longer.


ping

public void ping()
Respond with statistics when the server is pinged.


search

public void search()
Search for files in the global list with all clients files. Search of parts of filenames are possible as well as multiple searchwords to come closer to the desired file.


unknownCommand

public void unknownCommand(java.lang.String cmd)
Function to handle invalid or unknown requests.

Parameters:
cmd - Command sent to server.

addClientShare

public void addClientShare(java.lang.String ipAddress,
                           int cSharePort,
                           java.util.Vector cShare)
Adds a share for the client to the global dir.

Parameters:
ipAddress - IP-address where client can be reached.
cSharePort - Portnumber on which client serves download requests.
cShare - Vector containing all files shared from client.

removeClientShare

public void removeClientShare(java.lang.String ipAndPort)
Removes a share for the client in the global dir, by searching the global list with the unique combination of clients IP-address and portnumber.

Parameters:
ipAndPort - IP-address and portnumber of client connection concatenated together.

incConnectedClients

public static void incConnectedClients()
Increase statistics on number of currently connected clients.


decConnectedClients

public static void decConnectedClients()
Decrease statistics on number of currently connected clients.


getConnectedClients

public static int getConnectedClients()
Statistics on number of currently connected clients.

Returns:
Number of currently connected clients.

getClientsDuringUptime

public static int getClientsDuringUptime()
Number of clients that have connected to the server since it started.

Returns:
Number of clients that have been connected to the server since it started.

addNumFiles

public static void addNumFiles(int num)
Increase statistics on total number of shared files.

Parameters:
num - Number of files added.

removeNumFiles

public static void removeNumFiles(int num)
Decrease statistics on total number of shared files.

Parameters:
num - Number of files removed.

getNumFiles

public static int getNumFiles()
Statistics on total number of shared files.

Returns:
Number of shared files.

addFilesSize

public static void addFilesSize(long num)
Increase statistics on total size of shared files.

Parameters:
num - Total size in bytes of share added.

removeFilesSize

public static void removeFilesSize(long num)
Decrease statistics on total size of shared files.

Parameters:
num - Total size in bytes of share removed.

getFilesSize

public static long getFilesSize()
Statistics on total size of shared files.

Returns:
Total size in bytes.