cz.zcu.fav.kiv.cs
Class CSServer

java.lang.Object
  extended bycz.zcu.fav.kiv.cs.CSServer

public class CSServer
extends java.lang.Object

This class represents the main server. Every submodel connects to him.

Version:
2.0
Author:
Antonin Kunes

Field Summary
private  java.util.LinkedList cache
          Only for filling the distance matrix.
private  int counterForTime
          Counter of processes waiting for safe time from server.
private  int counterOfFinished
          Counter of finished processes.
private  int counterOfProc
          Counter of connected processes.
private  int counterOfWaiting
          Counter of processes waiting for others before the next step of the distributed simulation.
private  double[][] distanceMatrix
          Distance matrix with shortest paths between processes.
private  boolean distReceived
          True if server has received all distances between neighbours from all processes.
static int numberOfProc
          The number of all CSProcesses in the distributed simulation.
static int numberOfSimul
          The number of all simulation submodels in the distributed simulation.
private  java.io.PrintWriter[] outs
          Array of output channels to submodels.
private  int[][] procIdentity
          Only for first calculating of distance matrix.
private  java.util.HashMap procInfo
          Knows which process (in what submodel with what ID) represents the ID assigned by user.
private  double[][] safeTimes
          Matrix with safe times from submodels for other submodels.
private  java.net.ServerSocket server
          Describes the server socket.
private  long start
          This is the time, when all submodels have started to simulate.
static double timeWindow
          No submodel can advance its local simulation time more than this value ahead of another process.
private  java.util.LinkedList waitingForOthers
          List of processes waiting for others before the next step of the distributed simulation.
private  java.util.LinkedList waitingForTime
          List of processes waiting for safe time from server.
 
Constructor Summary
CSServer(int port)
          Creates the server and connects him to the specified port.
 
Method Summary
(package private)  void cacheAdd(java.lang.String line)
          Adds a message with distances from a process to the cache.
(package private)  void calculateMatrix()
          Uses Floyd's algorithm to recalculate distance matrix to set shortest paths between processes.
(package private)  void fillMatrix()
          Fills in the distance matrix.
(package private)  int getIDForCSProc()
          Returns an unique ID for process for distance matrix.
(package private)  java.io.PrintWriter[] getOuts()
          Returns an array of output channels to submodels.
(package private)  CSServerProcInfo getProcInfo(int ID)
          Return info about specified process.
(package private)  void incCounterForTime(int ID)
          Increments counter of processes waiting for safe time from server.
(package private)  void incCounterOfFinished()
          Increments counter of finished processes and carries out related actions to that.
(package private)  void incCounterOfWaiting(int ID)
          Increments counter of processes waiting for others before the next step of the distributed simulation.
private  void listenSocket()
          Listens the socket and creates thread for every connected submodel.
static void main(java.lang.String[] args)
          The main method.
private  void printElementOfMatrix(double element)
          Prints element of the distance matrix.
(package private)  void printMatrix()
          Prints the distance matrix.
(package private)  void procInfoPut(int pID, int simID, int autoID, int CSID)
          Puts information about IDs of a process into appropriate fields.
(package private)  void sendDistances()
          Sends an appropriate part of distance matrix to all submodels.
(package private)  void sendERROR(int ID)
          Sends Error message to all submodels, informing them about falling down of a specified submodel and carries out appropriate actions related to that error.
private  void sendSafeTime()
          Sends the appropriate safe time for every submodel.
private  void sendWaiting()
          Sends message to all waiting submodels, informing them, they can continue in the computation.
(package private)  void setSafeT(int m, int n, double value)
          Adds an element to the matrix with safe times.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

numberOfProc

public static int numberOfProc
The number of all CSProcesses in the distributed simulation.


numberOfSimul

public static int numberOfSimul
The number of all simulation submodels in the distributed simulation.


timeWindow

public static double timeWindow
No submodel can advance its local simulation time more than this value ahead of another process.


server

private java.net.ServerSocket server
Describes the server socket.


outs

private java.io.PrintWriter[] outs
Array of output channels to submodels.


safeTimes

private double[][] safeTimes
Matrix with safe times from submodels for other submodels.


distanceMatrix

private double[][] distanceMatrix
Distance matrix with shortest paths between processes.


procIdentity

private int[][] procIdentity
Only for first calculating of distance matrix. Knows which process (in what submodel with what ID) represents the ID assigned by server during filling of the distance matrix.


procInfo

private java.util.HashMap procInfo
Knows which process (in what submodel with what ID) represents the ID assigned by user.


cache

private java.util.LinkedList cache
Only for filling the distance matrix.


waitingForOthers

private java.util.LinkedList waitingForOthers
List of processes waiting for others before the next step of the distributed simulation.


waitingForTime

private java.util.LinkedList waitingForTime
List of processes waiting for safe time from server.


counterOfWaiting

private int counterOfWaiting
Counter of processes waiting for others before the next step of the distributed simulation.


counterForTime

private int counterForTime
Counter of processes waiting for safe time from server.


counterOfFinished

private int counterOfFinished
Counter of finished processes.


counterOfProc

private int counterOfProc
Counter of connected processes.


distReceived

private boolean distReceived
True if server has received all distances between neighbours from all processes.


start

private long start
This is the time, when all submodels have started to simulate.

Constructor Detail

CSServer

public CSServer(int port)
         throws java.io.IOException
Creates the server and connects him to the specified port.

Parameters:
port - int Requested port.
Method Detail

listenSocket

private final void listenSocket()
                         throws java.io.IOException
Listens the socket and creates thread for every connected submodel.

Throws:
java.io.IOException

sendWaiting

private final void sendWaiting()
Sends message to all waiting submodels, informing them, they can continue in the computation.


sendERROR

final void sendERROR(int ID)
Sends Error message to all submodels, informing them about falling down of a specified submodel and carries out appropriate actions related to that error.

Parameters:
ID - int ID of broken submodel.

sendDistances

final void sendDistances()
Sends an appropriate part of distance matrix to all submodels.


sendSafeTime

private final void sendSafeTime()
Sends the appropriate safe time for every submodel.


printMatrix

final void printMatrix()
Prints the distance matrix.


printElementOfMatrix

private final void printElementOfMatrix(double element)
Prints element of the distance matrix.


fillMatrix

final void fillMatrix()
Fills in the distance matrix. The comming distances between processes are stored in cache. Every connected process becomes from server an ID for distance matrix. When are received all distances, IDs of processes assigned by user are replaced by IDs from server and distance matrix can be filled in.


calculateMatrix

final void calculateMatrix()
Uses Floyd's algorithm to recalculate distance matrix to set shortest paths between processes.


incCounterOfFinished

final void incCounterOfFinished()
Increments counter of finished processes and carries out related actions to that.


incCounterForTime

final void incCounterForTime(int ID)
Increments counter of processes waiting for safe time from server.


incCounterOfWaiting

final void incCounterOfWaiting(int ID)
Increments counter of processes waiting for others before the next step of the distributed simulation.


cacheAdd

final void cacheAdd(java.lang.String line)
Adds a message with distances from a process to the cache.


setSafeT

final void setSafeT(int m,
                    int n,
                    double value)
Adds an element to the matrix with safe times.

Parameters:
m - int Row - From Whom
n - int Column - For Whom
value - double Safe time

procInfoPut

final void procInfoPut(int pID,
                       int simID,
                       int autoID,
                       int CSID)
Puts information about IDs of a process into appropriate fields.

Parameters:
pID - int ID of process assigned by user.
simID - int ID of submodel, where is the process located.
autoID - int ID of process assigned by submodel.
CSID - int ID of process for distance matrix assigned by server.

getProcInfo

final CSServerProcInfo getProcInfo(int ID)
Return info about specified process.

Parameters:
ID - int ID assigned by user of requested process.
Returns:
CSServerProcInfo Information about requested process.

getOuts

final java.io.PrintWriter[] getOuts()
Returns an array of output channels to submodels.


getIDForCSProc

final int getIDForCSProc()
Returns an unique ID for process for distance matrix.


main

public static void main(java.lang.String[] args)
The main method. Starts the server.

Parameters:
args - String[] Should be: port, number of processes, number of submodels and time window.