cz.zcu.fav.kiv.cs
Class CSSimulation

java.lang.Object
  extended bycz.zcu.fav.kiv.jsim.JSimSimulation
      extended bycz.zcu.fav.kiv.cs.CSSimulation

public class CSSimulation
extends cz.zcu.fav.kiv.jsim.JSimSimulation

The main class describing the simulation submodel.

Version:
2.0
Author:
Antonin Kunes

Field Summary
private  java.util.LinkedList calFinal
          The list of events that will be simulated in the current step of the distributed simulation.
private  java.util.LinkedList calTemp
          Into that list insert other processes events of local proceses.
private  int counterOfDist
          Counter of received distances to submodels.
private  java.lang.Object distanceLock
          His wait() and notify() methods are used to wait for message from server with distances to the other submodels .
private  double[][] distances
          Distances of internal processes to the other submodels.
private  boolean distancesReceived
          True, if distances to all submodels have been received from server.
private  int ID
          ID of this submodel assigned by the server.
private  java.io.BufferedReader in
          The input channel for messages from the server or other submodels.
private  java.util.ArrayList internalProc
          Array of CSProcesses located in that submodel
private  java.util.HashMap internalProcIDs
          The left side of this map is an ID of a process assigned by user and the right side is the ID assigned by submodel to this process.
private  double lastTimeFromServer
          The value of the safe time to simulate in this step of the distributed simulation.
private  int numberOfInternalProc
          The number of CSProcesses in that submodel.
private  int numberOfProc
          The number of all CSProcesses in the distributed simulation.
private  int numberOfSimul
          The number of all simulation submodels in the distributed simulation.
private  java.io.PrintWriter out
          The output channel for messages to the server or other submodels.
private  CSReadingThread readT
          the thread taking care of reading messages on input channel.
private  java.lang.String remoteERROR
          Indicates an error in remote submodel.
private  java.lang.Object safeTimeLock
          His wait() and notify() methods are used to wait for message from server with next safe time.
private  java.net.Socket socket
          The socket for a communication between the submodel and the thread of the server.
private  boolean timeFromServerAvailable
          True, if the reading thread has received a message with an actual safe time.
private  double timeWindow
          No submodel can advance its local simulation time more than this value ahead of another process.
private  java.util.LinkedList toCancel
          The list of requests to cancel a local process.
private  java.lang.Object waitForOthersLock
          His wait() and notify() methods are used to wait for message from server indicating that all submodels have finished current step of the distributes simulation.
 
Fields inherited from class cz.zcu.fav.kiv.jsim.JSimSimulation
calendar, MODE_GUI_BATCH, MODE_GUI_INTERACTIVE, MODE_TXT, NEW_PROCESS_FORBIDDEN, NEW_QUEUE_FORBIDDEN, NEW_SEMAPHORE_FORBIDDEN, PRN_ERROR, PRN_MESSAGE, SIMULATION_IN_PROGRESS, SIMULATION_NOT_STARTED, SIMULATION_TERMINATED, time
 
Constructor Summary
CSSimulation(java.lang.String name, int mode, double whenUpdate, java.lang.String host, int port)
          Creates a new class representing the simulation submodel.
CSSimulation(java.lang.String name, int mode, java.lang.String host, int port)
          Creates a new class representing the simulation submodel.
CSSimulation(java.lang.String name, java.lang.String host, int port)
          Creates a new class representing the simulation submodel.
 
Method Summary
private  void activateShadows()
          Activates the processes, that should run in the next step of the distributed simulation.
(package private)  void addInternal(int ID, CSProcess proc)
          Adds references to the internal process.
(package private)  void addToCancel(int who, java.lang.String message)
          Adds a request to cancel a process to the list toCancel.
(package private)  void addToCSCalFinal(double when, int whichOne, java.lang.String message, int fromWho)
          Adds a specified new event to the final calendar.
(package private)  void addToCSCalTemp(double when, int whichOne, java.lang.String message, int fromWho)
          Adds a specified new event to the temporary calendar.
(package private)  void calendarJump(int whichOne, java.lang.String message, int fromWho)
          Removes the first specified event from the final calender.
private  void carryOutCancel()
          Removes requested events from temporary calendar.
private  void construct(java.lang.String host, int port)
          This method is called in constructor, in principle this is the constructor.
private  java.lang.String decode(java.lang.String line)
          Decodes the incoming message.
(package private)  int getIDForCSProc()
          Returns an unique number for every CSProcess in this submodel.
 int getInternalProcID(int who)
          Returns the ID assigned by submodel of process specified by ID assigned by user.
 CSCalendarEvent getNextCEForMe(int ID)
          Returns the event for the next execution of the specified process in this step of the distributed simulation.
 int getNextFromWhoForMe(int ID)
          Returns the ID of the neighbour who has scheduled the next execution of the specified process in this step of the distributed simulation.
 java.lang.String getNextMessageForMe(int ID)
          Returns the message for the next execution of the specified process in this step of the distributed simulation.
 double getNextTimeForMe(int ID)
          Returns the time of next execution of the specified process in this step of the distributed simulation.
(package private)  java.io.PrintWriter getOut()
          Returns the output channel for messages to the server or other submodels.
(package private)  CSProcess getProc(int ID)
          Returns a reference to the specified process.
(package private)  double getSafeTime()
          Returns the last safe time from the server.
private  double getSafeTimeFromServer()
          Waits for and returns the next safe time from the server.
private  double[] getTimesForOthers()
          Goes through sorted temporary calendar and sets safe times for other submodels.
private  void changeCal()
          Removes events for the next step of the distributed simulation (with time stamp smaller than the safe time from the server) from the temporary calendar and puts them to the final calendar.
 boolean isInternal(int ID)
          True, if a process with specified ID is located in this submodel.
private  void othersReady()
          Tells that the others has finished the current step of the distributed simulation too.
(package private)  boolean readFromServer()
          Takes care of receiving of all messages from the sever.
private  void receiveBeginning(java.lang.String line)
          Takes care of the beginning message.
private  void receiveCancel(java.lang.String line)
          Takes care of receiving of a request to cancel a process.
private  void receiveDistance(java.lang.String line)
          Takes care of receiving distances from a process to other submodels.
private  void receiveMessage(java.lang.String line)
          Takes care of receiving of a message from a neighbour.
private  void receiveSafeTime(java.lang.String line)
          Takes care of receiving of the safe time from the server.
private  void sendTimesForOthers(double[] timesForOthers)
          Sends to the server the safe times for other submodels.
 void simulate(double maxTime)
          Executes the simulation.
 boolean somethingForMe(int ID)
          Returns true, if there is some event for the specified process in this step of the distributed simulation.
private  void waitForDistances()
          Waits untill distances from all processes to all submodels are received.
private  void waitForOthers()
          Sends to the server a message, that this submodel has finished the current step of the distributed simulation and waits for the others.
 
Methods inherited from class cz.zcu.fav.kiv.jsim.JSimSimulation
addEntryToCalendar, addProcess, addQueue, addSemaphore, deleteEntriesInCalendar, deleteProcess, error, getCurrentTime, getFreeProcessNumber, getFreeQueueNumber, getFreeSemaphoreNumber, getGraphicLock, getChange, getName, getNumber, getNumberOfProcesses, getNumberOfQueues, getNumberOfSemaphores, getObjectsToBeDisplayed, getRunningProcess, getSimulationMode, getSimulationState, getStepLock, getTalking, getTalkStream, getWaitingForWindowClosure, getWindowClosureLock, changeTalkStream, isRunningGUI, message, messageNoNL, printString, runGUI, setTalking, shutdown, step, switchToNobody, talk, talk, talk, talk, talk, talk, talk, talk, talk, waitForWindowClosure, windowIsClosing
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ID

private int ID
ID of this submodel assigned by the server.


socket

private java.net.Socket socket
The socket for a communication between the submodel and the thread of the server.


out

private java.io.PrintWriter out
The output channel for messages to the server or other submodels.


in

private java.io.BufferedReader in
The input channel for messages from the server or other submodels.


readT

private CSReadingThread readT
the thread taking care of reading messages on input channel.


remoteERROR

private java.lang.String remoteERROR
Indicates an error in remote submodel.


distanceLock

private java.lang.Object distanceLock
His wait() and notify() methods are used to wait for message from server with distances to the other submodels .


distancesReceived

private boolean distancesReceived
True, if distances to all submodels have been received from server.


counterOfDist

private int counterOfDist
Counter of received distances to submodels.


distances

private double[][] distances
Distances of internal processes to the other submodels.


safeTimeLock

private java.lang.Object safeTimeLock
His wait() and notify() methods are used to wait for message from server with next safe time.


timeFromServerAvailable

private boolean timeFromServerAvailable
True, if the reading thread has received a message with an actual safe time.


lastTimeFromServer

private double lastTimeFromServer
The value of the safe time to simulate in this step of the distributed simulation.


waitForOthersLock

private java.lang.Object waitForOthersLock
His wait() and notify() methods are used to wait for message from server indicating that all submodels have finished current step of the distributes simulation.


numberOfProc

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


numberOfSimul

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


timeWindow

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


numberOfInternalProc

private int numberOfInternalProc
The number of CSProcesses in that submodel.


internalProc

private java.util.ArrayList internalProc
Array of CSProcesses located in that submodel


internalProcIDs

private java.util.HashMap internalProcIDs
The left side of this map is an ID of a process assigned by user and the right side is the ID assigned by submodel to this process.


calFinal

private java.util.LinkedList calFinal
The list of events that will be simulated in the current step of the distributed simulation.


calTemp

private java.util.LinkedList calTemp
Into that list insert other processes events of local proceses. After the current step of the distributed simulation is this list sorted and events with time stamp smaller that safe time from server are removed to the calFinal.


toCancel

private java.util.LinkedList toCancel
The list of requests to cancel a local process.

Constructor Detail

CSSimulation

public CSSimulation(java.lang.String name,
                    java.lang.String host,
                    int port)
             throws cz.zcu.fav.kiv.jsim.JSimInvalidParametersException
Creates a new class representing the simulation submodel. The simulation will run in the text (console) mode and no graphic output will be allowed.

Parameters:
name - String The name of this submodel.
host - String The address of the server.
port - int The port is the server connected to.

CSSimulation

public CSSimulation(java.lang.String name,
                    int mode,
                    java.lang.String host,
                    int port)
             throws cz.zcu.fav.kiv.jsim.JSimInvalidParametersException
Creates a new class representing the simulation submodel. According to the simulation mode specified as a parameter, it will / will not be allowed to use graphic output later.

Parameters:
name - String The name of this submodel.
mode - int The simulation mode. Possible values are JSimSimulation.MODE_TXT, JSimSimulation.MODE_GUI_BATCH.
host - String The address of the server.
port - int The port is the server connected to.

CSSimulation

public CSSimulation(java.lang.String name,
                    int mode,
                    double whenUpdate,
                    java.lang.String host,
                    int port)
             throws cz.zcu.fav.kiv.jsim.JSimInvalidParametersException
Creates a new class representing the simulation submodel. According to the simulation mode specified as a parameter, it will / will not be allowed to use graphic output later.

Parameters:
name - String The name of this submodel.
mode - int The simulation mode. Possible values are JSimSimulation.MODE_TXT, JSimSimulation.MODE_GUI_BATCH.
whenUpdate - double A simulation time interval after which the GUI output will be updated.
host - String The address of the server.
port - int The port is the server connected to.
Method Detail

construct

private final void construct(java.lang.String host,
                             int port)
This method is called in constructor, in principle this is the constructor.

Parameters:
host - String The address of the server.
port - int The port is the server connected to.

simulate

public final void simulate(double maxTime)
                    throws CSRemoteException,
                           cz.zcu.fav.kiv.jsim.JSimMethodNotSupportedException,
                           cz.zcu.fav.kiv.jsim.JSimTooManyProcessesException,
                           cz.zcu.fav.kiv.jsim.JSimInvalidParametersException,
                           cz.zcu.fav.kiv.jsim.JSimSimulationAlreadyTerminatedException,
                           cz.zcu.fav.kiv.jsim.JSimSecurityException
Executes the simulation. Repeatedly calls method step() from J-Sim. At first are simulated events with time stamp 0.0 and one JSimProcess is scheduled for time maxTime + 1. It ensures, that method step() will never terminate the simulation because of empty calendar. Then are simulated all steps of the distributed simulation. In the beginning of the next step we have to wait for others till they finish the current step too. Then we have to wait for the safe time from the server. It means the ammount of simulation time, we will simulate in the next step.

Parameters:
maxTime - double The simulation time till the user wish to simulate.
Throws:
CSRemoteException - Means failure of other submodel.
cz.zcu.fav.kiv.jsim.JSimMethodNotSupportedException
cz.zcu.fav.kiv.jsim.JSimTooManyProcessesException
cz.zcu.fav.kiv.jsim.JSimInvalidParametersException
cz.zcu.fav.kiv.jsim.JSimSimulationAlreadyTerminatedException
cz.zcu.fav.kiv.jsim.JSimSecurityException

getSafeTimeFromServer

private final double getSafeTimeFromServer()
Waits for and returns the next safe time from the server. At first sends to the server safe times for other submodels. If server receives these values from all, sends the minimum value for every submodel. When we know our next safe time, we can remove events with smaller time stamp than the safe time from temporary calendar and put them to the final calendar, where are the events for the next step of distributed simulation. Then we have to activate processes from final calendar and next step can begin.

Returns:
double

getTimesForOthers

private final double[] getTimesForOthers()
Goes through sorted temporary calendar and sets safe times for other submodels. If there are any requests to cancel some processes, they are canceled at first.

Returns:
double[] Safe times for other submodels.

waitForOthers

private final void waitForOthers()
Sends to the server a message, that this submodel has finished the current step of the distributed simulation and waits for the others.


waitForDistances

private final void waitForDistances()
Waits untill distances from all processes to all submodels are received.


calendarJump

final void calendarJump(int whichOne,
                        java.lang.String message,
                        int fromWho)
Removes the first specified event from the final calender. CSProcesses call this method at the end of part of their life.

Parameters:
whichOne - int Whos event should be removed.
message - String What message has this event.
fromWho - int Who has scheduled this event.

changeCal

private final void changeCal()
Removes events for the next step of the distributed simulation (with time stamp smaller than the safe time from the server) from the temporary calendar and puts them to the final calendar.


carryOutCancel

private final void carryOutCancel()
Removes requested events from temporary calendar. This method is called at the beginning of the next step of the distributed simulation. Requests to cancel are stored in the list toCancel. Events to cancel are specified by ID and message. Request with CSConst.ALL means, that should be canceled events with any message.


activateShadows

private final void activateShadows()
Activates the processes, that should run in the next step of the distributed simulation. These processe have an event in the final calendar.


sendTimesForOthers

private final void sendTimesForOthers(double[] timesForOthers)
Sends to the server the safe times for other submodels.

Parameters:
timesForOthers - double[] Safe times for other submodels.

getSafeTime

final double getSafeTime()
Returns the last safe time from the server.


addToCSCalTemp

final void addToCSCalTemp(double when,
                          int whichOne,
                          java.lang.String message,
                          int fromWho)
Adds a specified new event to the temporary calendar.


addToCSCalFinal

final void addToCSCalFinal(double when,
                           int whichOne,
                           java.lang.String message,
                           int fromWho)
Adds a specified new event to the final calendar. It' necessary to sort him then.


addToCancel

final void addToCancel(int who,
                       java.lang.String message)
Adds a request to cancel a process to the list toCancel. This list is checked in every step of the distributed simulation.

Parameters:
who - int Events of what process should be canceled.
message - String Only events with this message will be canceled.

getIDForCSProc

final int getIDForCSProc()
Returns an unique number for every CSProcess in this submodel.


isInternal

public final boolean isInternal(int ID)
True, if a process with specified ID is located in this submodel.


addInternal

final void addInternal(int ID,
                       CSProcess proc)
Adds references to the internal process.

Parameters:
ID - int ID of beeing inserted process.
proc - CSProcess Reference to him.

getInternalProcID

public final int getInternalProcID(int who)
Returns the ID assigned by submodel of process specified by ID assigned by user.

Parameters:
who - int ID assigned by user.
Returns:
int Local ID assigned by submodel.

getProc

final CSProcess getProc(int ID)
Returns a reference to the specified process.

Parameters:
ID - int Local ID of process.

getOut

final java.io.PrintWriter getOut()
Returns the output channel for messages to the server or other submodels.


somethingForMe

public final boolean somethingForMe(int ID)
Returns true, if there is some event for the specified process in this step of the distributed simulation.

Parameters:
ID - int Local ID assigned by submodel.

getNextTimeForMe

public final double getNextTimeForMe(int ID)
Returns the time of next execution of the specified process in this step of the distributed simulation.

Parameters:
ID - int Local ID assigned by submodel.

getNextMessageForMe

public final java.lang.String getNextMessageForMe(int ID)
Returns the message for the next execution of the specified process in this step of the distributed simulation.

Parameters:
ID - int Local ID assigned by submodel.

getNextFromWhoForMe

public final int getNextFromWhoForMe(int ID)
Returns the ID of the neighbour who has scheduled the next execution of the specified process in this step of the distributed simulation.

Parameters:
ID - int Local ID assigned by submodel.

getNextCEForMe

public final CSCalendarEvent getNextCEForMe(int ID)
Returns the event for the next execution of the specified process in this step of the distributed simulation.

Parameters:
ID - int Local ID assigned by submodel.

decode

private final java.lang.String decode(java.lang.String line)
Decodes the incoming message. CSConst.NULLMSG is null and CSConst.SHORTMSG is "".


receiveBeginning

private final void receiveBeginning(java.lang.String line)
Takes care of the beginning message.

Parameters:
line - String A message with ID of this submodel, number of process, number of submodels and time window.

receiveSafeTime

private final void receiveSafeTime(java.lang.String line)
Takes care of receiving of the safe time from the server.

Parameters:
line - String A message with safe time from server.

receiveMessage

private final void receiveMessage(java.lang.String line)
Takes care of receiving of a message from a neighbour.

Parameters:
line - String A message with specification of a process that a neighbour wish to activate.

receiveDistance

private final void receiveDistance(java.lang.String line)
Takes care of receiving distances from a process to other submodels.

Parameters:
line - String A message with distances from a process to other submodels.

othersReady

private final void othersReady()
Tells that the others has finished the current step of the distributed simulation too.


receiveCancel

private final void receiveCancel(java.lang.String line)
Takes care of receiving of a request to cancel a process.

Parameters:
line - String A message with requested ID and message.

readFromServer

final boolean readFromServer()
Takes care of receiving of all messages from the sever.

Returns:
boolean False, if the simulation has finished and the reading thread should stop.