Skip to content

Instantly share code, notes, and snippets.

@EranSch
Last active August 29, 2015 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EranSch/8701067 to your computer and use it in GitHub Desktop.
Save EranSch/8701067 to your computer and use it in GitHub Desktop.
try {
// Get hostname of client
String clientName = connection.getInetAddress().getHostName()
+ "[" + threadID + "]";
// Announce connection
BizTrackMEServer.logEvent("event", clientName + " connected.");
// This streams data FROM the client
ObjectInputStream in = new ObjectInputStream(
connection.getInputStream()
);
// This streams data TO the client
ObjectOutputStream out = new ObjectOutputStream(
connection.getOutputStream()
);
out.flush();
// This will store requests as they come
String req,
ID;
/**
* This is the routing loop, it should be repeated for the duration of a
* connection if all goes correctly.
*/
do{
// Incoming request from client
req = in.readUTF();
// Log request
BizTrackMEServer.logEvent("event", clientName + " >>> " + req );
switch (req) {
case "VIEW_PROD":
out.writeObject(db.getProducts());
out.flush();
break;
case "VIEW_CUST":
out.writeObject(db.getCustomers());
out.flush();
break;
case "ADD_PROD":
db.addProduct((Product) this.readObject(in));
BizTrackMEServer.logEvent("event", "Product received");
break;
case "ADD_CUST":
db.addCustomer((Customer) this.readObject(in));
BizTrackMEServer.logEvent("event", "Product received");
break;
case "SEARCH_CUST":
out.writeObject(db.searchCustomers( in.readUTF() ));
break;
case "SEARCH_PROD":
out.writeObject(db.searchProducts( in.readUTF() ));
break;
case "UPDATE_PROD":
ID = in.readUTF();
db.updateProduct(ID, (Product)this.readObject(in));
break;
case "UPDATE_CUST":
ID = in.readUTF();
db.updateCustomer(ID, (Customer)this.readObject(in));
break;
case "DELETE_PROD":
db.remove("products",in.readUTF());
break;
case "DELETE_CUST":
db.remove("customers",in.readUTF());
break;
case "TERMINATE":
BizTrackMEServer.logEvent("event", "Client initiated kill.");
db.close();
System.exit(0);
case "CLIENT_DISCONNECT":
BizTrackMEServer.logEvent("event", clientName + " disconnected.");
break;
default:
out.writeUTF("MESSAGE NOT RECOGNIZED");
break;
}
}while(!req.equals("CLIENT_DISCONNECT"));
} catch (IOException ex) {
BizTrackMEServer.logEvent("error", "Router IO Error!\n" + ex.getMessage());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment