|
| 1 | +package com.atg.openssp.dspSim; |
| 2 | + |
| 3 | +import com.atg.openssp.dspSim.model.ModelException; |
| 4 | +import com.atg.openssp.dspSim.model.client.ClientCommand; |
| 5 | +import com.atg.openssp.dspSim.model.client.ClientCommandType; |
| 6 | +import com.atg.openssp.dspSim.model.client.ClientResponse; |
| 7 | +import com.atg.openssp.dspSim.model.client.ClientResponseStatus; |
| 8 | +import com.atg.openssp.dspSim.model.dsp.DspModel; |
| 9 | +import com.atg.openssp.dspSim.model.dsp.SimBidder; |
| 10 | +import com.google.gson.Gson; |
| 11 | +import com.sun.net.httpserver.HttpExchange; |
| 12 | +import com.sun.net.httpserver.HttpHandler; |
| 13 | +import org.slf4j.Logger; |
| 14 | +import org.slf4j.LoggerFactory; |
| 15 | + |
| 16 | +import java.io.*; |
| 17 | +import java.util.UUID; |
| 18 | + |
| 19 | +public class ClientHandler implements HttpHandler { |
| 20 | + private static final Logger log = LoggerFactory.getLogger(ClientHandler.class); |
| 21 | + private final DspModel model; |
| 22 | + |
| 23 | + public ClientHandler(DspModel model) { |
| 24 | + this.model = model; |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public void handle(HttpExchange httpExchange) throws IOException { |
| 29 | + ClientCommand cc = new Gson().fromJson(new InputStreamReader(httpExchange.getRequestBody()), ClientCommand.class); |
| 30 | + log.info("CC-->"+new Gson().toJson(cc)); |
| 31 | + ClientResponse cr = new ClientResponse(); |
| 32 | + if (cc == null) { |
| 33 | + cr.setStatus(ClientResponseStatus.FAILURE); |
| 34 | + cr.setReason("Command not found"); |
| 35 | + cr.setBidders(model.getBidders()); |
| 36 | + } else if (cc.getType() == ClientCommandType.LIST) { |
| 37 | + cr.setStatus(ClientResponseStatus.SUCCESS); |
| 38 | + cr.setBidders(model.getBidders()); |
| 39 | + } else if (cc.getType() == ClientCommandType.ADD) { |
| 40 | + SimBidder sb = model.lookupBidder(cc.getId()); |
| 41 | + if (sb == null) { |
| 42 | + sb = new SimBidder(cc.getId()); |
| 43 | + sb.setPrice(cc.getPrice()); |
| 44 | + model.add(sb); |
| 45 | + try { |
| 46 | + model.saveModel(); |
| 47 | + cr.setStatus(ClientResponseStatus.SUCCESS); |
| 48 | + } catch (ModelException e) { |
| 49 | + log.error(e.getMessage(), e); |
| 50 | + cr.setStatus(ClientResponseStatus.FAILURE); |
| 51 | + cr.setReason("add save failed: "+e.getMessage()); |
| 52 | + } |
| 53 | + } else { |
| 54 | + cr.setStatus(ClientResponseStatus.FAILURE); |
| 55 | + cr.setReason("SimBidder already exists"); |
| 56 | + } |
| 57 | + cr.setBidders(model.getBidders()); |
| 58 | + } else if (cc.getType() == ClientCommandType.UPDATE) { |
| 59 | + SimBidder sb = model.lookupBidder(cc.getId()); |
| 60 | + if (sb == null) { |
| 61 | + cr.setStatus(ClientResponseStatus.FAILURE); |
| 62 | + cr.setReason("SimBidder not found"); |
| 63 | + } else { |
| 64 | + sb.setPrice(cc.getPrice()); |
| 65 | + cr.setStatus(ClientResponseStatus.SUCCESS); |
| 66 | + try { |
| 67 | + model.saveModel(); |
| 68 | + } catch (ModelException e) { |
| 69 | + log.error(e.getMessage(), e); |
| 70 | + cr.setStatus(ClientResponseStatus.FAILURE); |
| 71 | + cr.setReason("update save failed: "+e.getMessage()); |
| 72 | + } |
| 73 | + } |
| 74 | + cr.setBidders(model.getBidders()); |
| 75 | + } |
| 76 | + String result = new Gson().toJson(cr); |
| 77 | + log.info("CR<--"+result); |
| 78 | + httpExchange.sendResponseHeaders(200, result.length()); |
| 79 | + OutputStream os = httpExchange.getResponseBody(); |
| 80 | + os.write(result.getBytes()); |
| 81 | + os.close(); |
| 82 | + |
| 83 | + } |
| 84 | +} |
0 commit comments