@Override public void run() { Channel conn = sq.channel; if (conn == null || !conn.isOpen()) { PerChannelQueue.logger.error("connection missing, no inbound communication"); return; } while (true) { if (!forever && sq.inbound.size() == 0) break; try { // block until a message is enqueued GeneratedMessage msg = sq.inbound.take(); // process request and enqueue response if (msg instanceof Request) { Request req = ((Request) msg); // do we need to route the request? System.out.println("#####Inside msg instanceof Request######"); // handle it locally Resource rsc = ResourceFactory.getInstance().resourceInstance(req.getHeader()); Response reply = null; if (rsc == null) { logger.error("failed to obtain resource for " + req); reply = ResourceUtil.buildError( req.getHeader(), ReplyStatus.FAILURE, "Request not processed"); } else { System.out.println( "#####Before Processing a request for reply######" + rsc.getClass()); reply = rsc.process(req); } sq.enqueueResponse(reply); } } catch (InterruptedException ie) { System.out.println("####error in processing - interruptedException####"); break; } catch (Exception e) { System.out.println("####error in processing####"); PerChannelQueue.logger.error("Unexpected processing failure", e); break; } } if (!forever) { PerChannelQueue.logger.info("connection queue closing"); } }
/* * (non-Javadoc) * * @see poke.server.resources.Resource#process(eye.Comm.Finger) */ public Request process(Request request) { // TODO add code to process the message/event received logger.info("poke: " + request.getBody().getPing().getTag()); Request.Builder rb = Request.newBuilder(); // metadata rb.setHeader(ResourceUtil.buildHeaderFrom(request.getHeader(), PokeStatus.SUCCESS, null)); // payload Payload.Builder pb = Payload.newBuilder(); Ping.Builder fb = Ping.newBuilder(); fb.setTag(request.getBody().getPing().getTag()); fb.setNumber(request.getBody().getPing().getNumber()); pb.setPing(fb.build()); rb.setBody(pb.build()); Request reply = rb.build(); return reply; }