@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"); } }
/** * Our network's equivalent to ping * * @param tag * @param num */ public void poke(String tag, int num) { // data to send Ping.Builder f = eye.Comm.Ping.newBuilder(); f.setTag(tag); f.setNumber(num); // payload containing data Request.Builder r = Request.newBuilder(); eye.Comm.Payload.Builder p = Payload.newBuilder(); p.setPing(f.build()); r.setBody(p.build()); // header with routing info eye.Comm.Header.Builder h = Header.newBuilder(); h.setOriginator("client"); h.setTag("test finger"); h.setTime(System.currentTimeMillis()); h.setRoutingId(eye.Comm.Header.Routing.PING); r.setHeader(h.build()); eye.Comm.Request req = r.build(); try { comm.sendMessage(req); } catch (Exception e) { logger.warn("Unable to deliver message, queuing"); } }