0

I have a Server/Client application that also includes a timer. Communication is allowed between the server and client until a deadline time (using a Calendar object) is reached. The reaching of this deadline is monitored in a separate thread, which is called from the server:

//create and run a timer on bid items
public static void startClock()
{
    //thread to track item deadlines
    DeadlineClock deadlineClock =
            new DeadlineClock(deadline);
    //begin running the clock
    deadlineClock.start();
}

The deadline is then detected like so:

    //inside DeadlineClock's run() method

    //Retrieve current date and time...
    Calendar now = Calendar.getInstance();

    //deadline not yet reached
    while(now.before(deadline))
    {
        try
        {
            //wait a second and try again
            sleep(1000);
        }
        catch (InterruptedException intEx)
        {
            //Do nothing.
        }

        //Update current date and time...
        now = Calendar.getInstance();
        //run loop again
    }

What I would like to do is have a way of detecting (in the Server) when the deadline has been reached in the DeadlineClock, but I'm completely unable to find an implementable way of doing this, without completely duplicating the whole timing mechanism in the server.

From what I know, it would essentially take some kind of output or return of a value on the DeadlineClock side, but I have no idea how this would be read in or detected on the Server-side, and this would also be difficult to scale if the program ever had more than one deadline involved.

My only other idea was to pass a boolean variable into the DeadlineClock constructor, and then try and wait to detect if this changed, something like this (assuming that the variable's value was changed once the deadline was reached):

//create and run a timer on bid items
public static void startClock()
{
    //initialise as false before items run
    boolean deadlineReached = false;

    //thread to track item deadlines
    DeadlineClock deadlineClock =
            new DeadlineClock(deadline, deadlineReached);
    //begin running the clock
    deadlineClock.start();

    //monitor other thread for value change
    while (deadlineReached != true)
    {
        //do nothing until changed
        wait();
    }
    ///////////////////////////
    ///CHANGE DECTECTED HERE///
    ///////////////////////////
}

This is pretty rough, but hopefully somewhere along the right lines. Can anyone suggest how I might be able to implement the functionality I'm after?

Thanks, Mark

marcuthh
  • 592
  • 3
  • 16
  • 42

1 Answers1

0

I think the issue here is an architectural one, than anything else. If you prefer to use Java's queuing mechanism the solution here is pretty simple.

Establish a messaging structure between the client and the server. This can be easily achieved using a BlockingQueue.

Initialize the BlockingQueue in the main app:

BlockingQueue<String> queue = new ArrayBlockingQueue<String>();

Provide the queue to both client and server apps.

In the while loop of the server app do the following:

String clientMessage = """;


while(true){
clientMessage = queue.take();
}

Note that queue on take blocks until a string element becomes available.

In the client app now simply insert a string in the queue when the deadline is reached and the server will be automatically notified.

rahsan
  • 170
  • 9
  • I've seen this architecture all over Stack Overflow while I was researching, but this project is part of a graded assignment and I can't use Java features that haven't been covered in the course. Do you know of a way to do it something similar to what I've shown? Thanks – marcuthh Mar 15 '16 at 15:39
  • In that case you have to use wait and notify respectably. – rahsan Mar 15 '16 at 15:45
  • To elaborate a little bit without going into too much details since it's an assignment, look into how these mechanisms work. Assuming it's a two threaded application if a thread is waiting, calling notify() from another thread would release it from it's waiting state. You will also need to synchronize on the same object. So essentially the client and server needs to synchronize on a dummy object in your case – rahsan Mar 15 '16 at 15:49
  • Yeah, I figured as much but I was unsure about where they would go. My instinct was to use `wait()` as it is in my final piece of code, and then `notify()` after the `while` loop. Is this along the right lines? @rahsan – marcuthh Mar 15 '16 at 15:54
  • You have the right idea, but you are using it in the wrong places. Since the client has the timer it should not wait, but rather notify. Since the server is waiting for a message or notification from the client, it should wait. Also don't forget to synchronize in both cases( client/server) on the same dummy object, before calling wait or notify – rahsan Mar 15 '16 at 16:01
  • Hi, attempting to implement this now but it's still a tad unclear as I'm not overly experienced with `wait()` and `notify()`. Do you know of anything that I could use as a good example to relate to my own code? @Rahsan – marcuthh Mar 15 '16 at 16:21
  • Here is a thread related to wait and notify, look at the first answer you should be implementing something similar in your code: http://stackoverflow.com/questions/886722/how-to-use-wait-and-notify-in-java – rahsan Mar 15 '16 at 16:26
  • Thanks for all your help, really appreciate it! – marcuthh Mar 15 '16 at 16:28