Question

I have a client process running and doing some stuff, at some point, when the server decides and sends a request, I'd like the client to stop whatever it's doing and do other_stuff instead, client pseudo:

public static void main(String args[])
{
   registerInterruptHandler(other_stuff);
   stuff();
}

void stuff()
{
    // Do stuff
}

void other_stuff()
{
    // Do other stuff
}

server pseudo:

void interrupt_client(client_ip_address)
{
     send_message_to_client(client_ip_address);
}

My questions are:

  1. How do I send the request from the server? I can't use http requests since the client isn't running an http server, do I have to use sockets? or is there some other Apache library to make it easier?

  2. How do I receive the message asynchronously in the client?

Was it helpful?

Solution

  • How do I send the request from the server? I can't use http requests since the client isn't running an http server, do I have to use sockets? or is there some other Apache library to make it easier?

Yes this is a good use for sockets. Apache does have some useful tools to help with this but implementing a basic server client socket connection isn't to difficult by itself

  • How do I receive the message asynchronously in the client?

This part gets into how you implement your threads and send/receive data from the server. You should break apart your work and build out into 2 parts

  1. Create the client and server connections. Build out all the functionality you need to send and receive data from a server, build in some calls that let the client know they need to work on other_stuff.

  2. Create the Threads that are required for other_stuff, stuff and the socket connection. This would at least consist of 2 threads, 1 for the socket connection and 1 for stuff and other_stuff. Depending on what stuff and other_stuff do you may want to break those into 2 different threads that use Thread.notify and Thread.wait to interrupt each other when the socket connection receives the right messages.

Both of these 2 topics have many tutorials and code samples online so I wont make any here. But as I said break apart your project into 2 parts and it will be an easier build out. I think a good starting point might be creating your client and simulating your server by using the System.in for "prentend" responses from the server

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top