0

I have modeled a Client Server application using Java Threads and BlockingQueues (producer and consumer queues) as recommended in a previous question.

What I have is server has a BlockingQueue<Request> and it will push responses to clients' BlockingQueue<Response>. Problem now is how do I setup the client to call server functions at all. I have client's run()

while (true) {
    Response res = responses.take();
    switch (res.function) {
    case "getReservationStatus":
        TreeMap<Integer, Boolean> reservations = (TreeMap<Integer, Boolean>) res.data;
        //System.out.println("getReservationStatus");
        for (Entry<Integer, Boolean> reservation : reservations.entrySet()) {
            //System.out.println(" - " + reservation.getKey() + " \t " + (reservation.getValue() ? "Booked" : "Available"));
        }
        break;
    case "book":
        SimpleEntry<Integer, Boolean> data = (SimpleEntry<Integer, Boolean>) res.data;
        System.out.println(terminalId + ": Booking seat " + data.getKey() + " " + (data.getValue() ? "Successful" : "Unsucessful"));
        break;
    }
}

How do I somehow from my main() allow user to control what commands to send to the client? For now I hardcoded my "simulation" before the while

Community
  • 1
  • 1
Jiew Meng
  • 84,767
  • 185
  • 495
  • 805
  • I don't understand why client should send a response since the client job is to send requests. Please clarify your question. – didierc Mar 02 '13 at 01:50
  • Is your question a User Interface question? Do you want to make your program interactive? – didierc Mar 02 '13 at 01:54
  • You could say so. I currently I just hardcode client to put Requests in server's queue. But I want to be able to somehow trigger the client to send requests instead. If its possible, seems like with the current setup its hard – Jiew Meng Mar 02 '13 at 02:11
  • It is not hard, but if you have a single user, there is no point in having multiple clients threads for sending requests. UI is generally based on the concept of events. Since you are using Java, try designing a simple graphical interface which covers your idea of how user would launch queries first, then try to hook both programs together, and if you're stuck at some point, throw another question with your code on SO. – didierc Mar 02 '13 at 02:24
  • you can easily use an IDE like eclipse or netbeans to do your UI designs, and there are numerous tutorials on internet to do that. – didierc Mar 02 '13 at 02:27
  • Ok I might try that, tho since this is more of a school assignment, I dont think a GUI is required, something more like a terminal app will be fine. I'll think about that. – Jiew Meng Mar 02 '13 at 02:47

1 Answers1

0

I am not sure if I understand your question, but I'll try to give an answer and a few ideas:

If you wish to implement a user interface for your programme, you must first define what your user can do with your programme. One popular way is to define use cases, and infer the interface from it. Let's say rather that we define functionalities from these use cases. So clearly you don't need that step anymore, since you have the kernel of your app done.

Your app is about pushing requests to a server to have them processed, and get responses. These requests, and their parameters, constitute a language understood by the server. In the same manner, the responses are a language the clients can understand, so you can model your UI around the requests language. If that language is simple enough, you can represent it as a tree, which is easy to implement in a UI.

You have basically three options:

  • text input UII: it's supposedly the simplest of all,

  • graphical UII (swing for instance),

  • web UI

In all three cases, the interface can be modeled as a list of menus and submenus to cover all the possibilities of requests: each request is an entry of your first menu, then each submenu level represent a refinement of the selected request. You can see here the resemblance with a tree I hinted at earlier. for a graphical UI, or a web one (after all it's a graphical UI too), this is trivial almost to achieve: you will find plenty of tutorials on the web for that. For text input, you can just display a numbered list of items (the menu), and ask the user to pick a choice.

Some notes:

Your app as it stands, models the interaction of several clients with one server. I am not sure how you can have that in your UI: perhaps you can enhance the language with a set of commands to spawn and manage clients, as well as indicate which client should send which requests.

If you feel adventurous and chose the text input, you could try modeling a real language to represent your queries, and implement a parser for that, this is a very broad topic too, but quite interesting. You might get into that later on in your studies anyway.

If the requests are complex or tedious to input, you could devise a template system, where the user could create a request template, name it, and then refer to it later on instead of inputing the same request again. See how we're getting closer to a small programming language here.

Web apps in the java world are usually based on fairly complex frameworks and toolings, you'll probably want to start with the other options first to get a good idea of UI.

As mentioned in my comments, for a graphical UI, try an IDE like eclipse or netbeans which let you easily define everything graphically and hook code to specific events (like pushing a button).

didierc
  • 14,572
  • 3
  • 32
  • 52
  • Perhaps u misunderstood my question? I meant to ask, currently when the client `run()`, it just waits for responses. How can it then send responses triggered by a user? Currently I "simulate" that by sending requests before the `while` – Jiew Meng Mar 02 '13 at 01:43
  • completely rewritten my answer given your recent input, sorry for the misunderstanding. – didierc Mar 02 '13 at 12:45
  • let me know if there's a problem again. – didierc Mar 02 '13 at 12:49