Reputation:
I have a client-side driven GUI (Swing) and I need to process more that one request. I can process one request, For example, 'logging in, the user' but I am unable to make any other request like 'fetch all users' from the server.
How can I process more than one request on the server and how would I set it up on the client-side.
Client-Side
private Socket server;
public ClientConnection() {
try {
server = new Socket("127.0.0.1", 12345);
} catch(IOException e) {
System.out.println("Connection Error - Client: " + e.getMessage());
}
}
public void loginUser(User user) {
try {
ObjectOutputStream out = new ObjectOutputStream(server.getOutputStream());
out.flush();
ObjectInputStream in = new ObjectInputStream(server.getInputStream());
//How would I write the request type to the server
out.writeObject((User) user);
out.flush();
User getUserInfo = (User) in.readObject();
//System.out.println("Response from the server: " + getUserInfo.getRole());
out.close();
in.close();
server.close();
} catch(IOException e) {
System.out.println(e.getMessage());
}
}
Server-side
private ServerSocket serverListener;
private Socket client;
public ServerConnection() {
try {
serverListener = new ServerSocket(12345, 10);
} catch(IOException e) {
System.out.println("Server-Side Error: Connection" + e.getMessage());
}
}
public void server() {
try {
System.out.println("Server is listening");
client = serverListener.accept();
//How can I make a request and then process it depending on the type of
//request
//Gets the ObjectOutputStream and the ObjectInputStream and send a User object back to
//the client-side
loginUser();
} catch(IOException e) {
System.out.println(e.getMessage());
e.getStackTrace();
}
}
My initial thinking is to have an Enum class with different request types and then have a switch statement in my server()
method and depending on the type of request a method is called to perform those actions but I am not sure on how to send a request type to the server and if handling the request type in the server()
method would be best practices.
Upvotes: 0
Views: 427
Reputation: 250
What you need to think about is how to define the communication between your client and server. Define protocols and message structures.
Let's say you want to introduce multiple request types. It is up to you what those request types will be, but what is going to happen is that you will be introducing the logic to handle the new types of requests on both the client and server.
Just like in REST, both client and server know what GET/POST is - you will also need to have these protocols defined for your communication.
An example would be, your client is sending a request of type fetch-all-users
. One way to structure this would be:
{
"requestType": "fetch-all-users",
"body": "..."
}
You can have a common module for the enum types, that is up to you.
Further on, we get to the question on how you would like to process the request on the server side.
A straightforward solution would be adding if-else
logic to process the correct request type based on the provided type.
But to avoid the if else logic, you could play around with design patterns.
Create a factory for your requestProcessingActions
:
class ActionFactory {
public Action resolve(RequestType requestType) {
final Action resolvedAction;
switch(requestType) {
case fetch-all-users:
resolvedAction = fetchAllUsers();
break;
case log-in-user:
resolvedAction = logInUser();
break;
}
return resolvedAction;
}
}
And call it in your server:
public void server() {
try {
System.out.println("Server is listening");
client = serverListener.accept();
RequestType type = getRequestTypeFromRequest();
ActionFactory.resolve(type);
}
}
Upvotes: 1