Time transitions in Java

O

omkar.tilak

I am writing a java program which receives messages (over TCP socket)
from other java program. However, program need to do different
activities depending upon the message received. Program has a variable
called as prog_state. When prog starts, it is initialized to 'Start'.
Now in 'Start' state, if program receives message m1, it should change
its state to 'A' (i.e. merely assign value 'A' to prog_state'
variable). In the same start state, it it receives message m2, it
should change state to 'B'. However, if prog remains in 'Start' state
for more than certain time (say 5 seconds), it should automativally
change its state to 'C'. This is similar to FSM specification but it
has a time transition. I am having trouble in simulating the time
behavior (automatically transit after certain time period while still
being in a position to accept message during that period). Any
suggestions / code snippets in this regard will be of great help.
Thanks and regards


---OMKAR
 
M

Matt Humphrey

I am writing a java program which receives messages (over TCP socket)
from other java program. However, program need to do different
activities depending upon the message received. Program has a variable
called as prog_state. When prog starts, it is initialized to 'Start'.
Now in 'Start' state, if program receives message m1, it should change
its state to 'A' (i.e. merely assign value 'A' to prog_state'
variable). In the same start state, it it receives message m2, it
should change state to 'B'. However, if prog remains in 'Start' state
for more than certain time (say 5 seconds), it should automativally
change its state to 'C'. This is similar to FSM specification but it
has a time transition. I am having trouble in simulating the time
behavior (automatically transit after certain time period while still
being in a position to accept message during that period). Any
suggestions / code snippets in this regard will be of great help.
Thanks and regards

When you initialize your machine to the start state, start a 5-second timer.
When you transition to A or B, turn off the timer. Otherwise, when the
timer goes off, transition yourself to C. I'm sure this exercise has to do
with synchronization because you have to ensure that the arrival of the next
message is clearly distinct from the activation of the timer.

Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
O

omkar.tilak

Thanks for the quick reply Matt. But can u show me a code snippet ... I
tried to use Java Timer but I couldn't quite figure out the exact usage
..... also, the timer instruction may be a blocking instruction ... so
while thread is executing the timer, it may not be able to receive
messages .... maybe multiple threads is the answer ... but I'm having
trouble in putting everything together ... regards
 
O

Oliver Wong

I am writing a java program which receives messages (over TCP socket)
from other java program. However, program need to do different
activities depending upon the message received. Program has a variable
called as prog_state. When prog starts, it is initialized to 'Start'.
Now in 'Start' state, if program receives message m1, it should change
its state to 'A' (i.e. merely assign value 'A' to prog_state'
variable). In the same start state, it it receives message m2, it
should change state to 'B'. However, if prog remains in 'Start' state
for more than certain time (say 5 seconds), it should automativally
change its state to 'C'. This is similar to FSM specification but it
has a time transition. I am having trouble in simulating the time
behavior (automatically transit after certain time period while still
being in a position to accept message during that period). Any
suggestions / code snippets in this regard will be of great help.
Thanks and regards

If you have access to an asynchronous IO library, use it. Otherwise,
implement one yourself by having a seperate thread do the reading from the
sockets.

- Oliver
 
O

Oliver Wong

Thanks for the quick reply Matt. But can u show me a code snippet ... I
tried to use Java Timer but I couldn't quite figure out the exact usage
.... also, the timer instruction may be a blocking instruction ... so
while thread is executing the timer, it may not be able to receive
messages .... maybe multiple threads is the answer ... but I'm having
trouble in putting everything together ... regards

By virtue of using a java.util.Timer, you should automatically be using
multiple threads.

- Oliver
 
M

Matt Humphrey

Thanks for the quick reply Matt. But can u show me a code snippet ... I
tried to use Java Timer but I couldn't quite figure out the exact usage
.... also, the timer instruction may be a blocking instruction ... so
while thread is executing the timer, it may not be able to receive
messages .... maybe multiple threads is the answer ... but I'm having
trouble in putting everything together ... regards

First, please don't top-post. Put your reply after prior comments so that
its easier to read your comments in context. (You're using Google, which
has trouble with this concept.)

java.util.Timer is incredibly easy to use--read the API documentation for
exact usage. You can get them at http://java.sun.com/j2se/1.5.0/docs/api/
or if you dig around download them.

Create a TimerTask via anonymous subclass (needed to override the run
method)

TimerTask myTask = new TimerTask () {
public void run () {
// the code in here will run when the time expires.
}
};

// Create the timer and schedule the task for 5 seconds ahead.
Timer timer = new Timer ();
timer.schedule (myTask, 5000L);

If you get your events (A, B) do timer.cancel () or myTask.cancel() to
cancel the timer.

Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
O

omkar.tilak

Matt said:
First, please don't top-post. Put your reply after prior comments so that
its easier to read your comments in context. (You're using Google, which
has trouble with this concept.)

java.util.Timer is incredibly easy to use--read the API documentation for
exact usage. You can get them at http://java.sun.com/j2se/1.5.0/docs/api/
or if you dig around download them.

Create a TimerTask via anonymous subclass (needed to override the run
method)

TimerTask myTask = new TimerTask () {
public void run () {
// the code in here will run when the time expires.
}
};

// Create the timer and schedule the task for 5 seconds ahead.
Timer timer = new Timer ();
timer.schedule (myTask, 5000L);

If you get your events (A, B) do timer.cancel () or myTask.cancel() to
cancel the timer.

Matt Humphrey (e-mail address removed) http://www.iviz.com/

Hi Matt,

thanks for all the help. There is still one small issue though. Here
is my code

class tcpserver
{
String sentence;
String reply;
String state = "START";

public static void main(String args[]) throws Exception
{
Timer timer;


class RemindTask extends TimerTask {
public void run() {
System.out.format("State changed to C");
timer.cancel(); //Terminate the timer thread
}
}



// Create the timer and schedule the task for 5 seconds ahead.

ServerSocket welcome = new ServerSocket(2000);
while(true)
{

Socket connection = welcome.accept();
BufferedReader inClient = new BufferedReader(new
InputStreamReader(connection.getInputStream()));
DataOutputStream out = new
DataOutputStream(connection.getOutputStream());
timer = new Timer();
timer.schedule(new RemindTask(), 5000);


while(timer.isRunning())
{
sentence = inClient.readLine();
timer.cancel();
state = sentence;
System.out.println("State changed to" + sentence);
}
}
}
}

I need to read from socket (inClient.readLine() ) only when the timer
is running (or while the RemindTask is not scheduled). However, Timer
class has no method to do this stuff. Do u think that using javax.swing
Timer might help here ... regards
 
M

Matt Humphrey

Hi Matt,

thanks for all the help. There is still one small issue though. Here
is my code

You're still missing the point below. You don't wait for the timer in a
loop--it defeats the purpose. Your main program waits while reading from
the socket. When the timer goes off, you just have to change state.
class tcpserver
{
String sentence;
String reply;
String state = "START";

public static void main(String args[]) throws Exception
{
Timer timer;


class RemindTask extends TimerTask {
public void run() {
System.out.format("State changed to C");
timer.cancel(); //Terminate the timer thread

You have to actually change state here
state = "C";

And there's no point in terminating the timer because it stops by itself
after this task. The task will run only once anyway. I get the impression
you want to make your main thread stop reading here, though.
}
}



// Create the timer and schedule the task for 5 seconds ahead.

ServerSocket welcome = new ServerSocket(2000);
while(true)
{

Socket connection = welcome.accept();
BufferedReader inClient = new BufferedReader(new
InputStreamReader(connection.getInputStream()));
DataOutputStream out = new
DataOutputStream(connection.getOutputStream());
timer = new Timer();
timer.schedule(new RemindTask(), 5000);


while(timer.isRunning())
{
sentence = inClient.readLine();
timer.cancel();
state = sentence;
System.out.println("State changed to" + sentence);

So instead you really just need

String sentence;
while ((sentence = inClient.readLine()) != null) {
timer.cancel ();
state = sentence;
}

You must be aware that there is a race condition between setting the state
here and setting it in the timer task--what happens if the sentence comes in
just as the timer is activating? You need some synchronization to handle
that.

Also, if the firing of the timer signals that the TCP conversation is over
you should close the socket which will interrupt your main thread, otherwise
it will just wait for the next message. That's done by the code you supply
to the timer.
}
}
}
}

I need to read from socket (inClient.readLine() ) only when the timer
is running (or while the RemindTask is not scheduled).

Sure, but you have to realize that there is necessarily critical point of
transition between when the timer stops and the reading stops. Reading
means waiting and so it cannot stop itself--the timer must stop it. (There
are non-block NIO sockets you can use, but for this case it simply shifts
where the waiting occurs.)
However, Timer
class has no method to do this stuff. Do u think that using javax.swing
Timer might help here ... regards

No. You cannot have a loop that waits on both the timer and on the socket.
The loop you had before will say "timer is running" and so try to read and
then it will wait. Meanwhile the timer will stop and your reader will still
be waiting. You must cancel the reading in the code that signals the timer
has stopped. Furthermore, if the read receives data after the timer has
fired its signal, but before its run method completes, you will get an
incorrect answer. You must look into synchronization so that you can be
sure only one of these happens at a time.

Matt Humphrey (e-mail address removed) http://www.iviz.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top