package wserver; import flexjson.JSONSerializer; import index.KIndex; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.util.Set; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import com.sun.grizzly.tcp.ActionCode; import com.sun.grizzly.tcp.Adapter; import com.sun.grizzly.tcp.Request; import com.sun.grizzly.tcp.Response; import com.sun.grizzly.util.buf.ByteChunk; import com.sun.grizzly.util.buf.UDecoder; public class YBAdapter implements Adapter { private final String CALLBACK_KEY = "wserver.FINISH_REQUEST"; private final ExecutorService exec = Executors.newCachedThreadPool(); private KIndex kindex; private JSONSerializer serializer; public YBAdapter(KIndex kindex) { this.kindex = kindex; serializer = new JSONSerializer(); } public void service(final Request req, final Response res) throws Exception { String command = req.requestURI().toString(); System.out.println(command); if(command.equals("/aPair")) { final String src = decode(req.getParameters().getParameter("src")); final String dst = decode(req.getParameters().getParameter("dst")); final String id = req.getParameters().getParameter("id"); if(src==null || dst==null || id==null) { return; } exec.submit(new Callable() { public Void call() throws Exception { res.setStatus(200); int count = kindex.postWordPair(src,dst,id); byte[] bytes = serializer.serialize(count).getBytes("UTF-8"); ByteChunk chunk = new ByteChunk(); res.setContentLength(bytes.length); res.setContentType("application/json"); chunk.append(bytes, 0, bytes.length); res.doWrite(chunk); Runnable completion = (Runnable) req.getAttribute(CALLBACK_KEY); completion.run(); return null; } }); } else if(command.equals("/aSession")) { final String id = req.getParameters().getParameter("id"); final String rum = req.getParameters().getParameter("rum"); if(id==null || rum==null) { return; } exec.submit(new Callable() { public Void call() throws Exception { res.setStatus(200); kindex.addSession(id, rum); byte[] bytes = serializer.serialize("ok").getBytes("UTF-8"); ByteChunk chunk = new ByteChunk(); res.setContentLength(bytes.length); res.setContentType("application/json"); chunk.append(bytes, 0, bytes.length); res.doWrite(chunk); Runnable completion = (Runnable) req.getAttribute(CALLBACK_KEY); completion.run(); return null; } }); } else if(command.equals("/uSession")) { final String id = req.getParameters().getParameter("id"); final String rum = req.getParameters().getParameter("rum"); if(id==null || rum==null) { return; } exec.submit(new Callable() { public Void call() throws Exception { res.setStatus(200); kindex.updateSession(id, rum); byte[] bytes = serializer.serialize("ok").getBytes("UTF-8"); ByteChunk chunk = new ByteChunk(); res.setContentLength(bytes.length); res.setContentType("application/json"); chunk.append(bytes, 0, bytes.length); res.doWrite(chunk); Runnable completion = (Runnable) req.getAttribute(CALLBACK_KEY); completion.run(); return null; } }); } else if(command.equals("/aTag")) { final String id = req.getParameters().getParameter("id"); final String word = req.getParameters().getParameter("word"); final String tag = req.getParameters().getParameter("tag"); if(id==null || word==null) { return; } exec.submit(new Callable() { public Void call() throws Exception { res.setStatus(200); kindex.addTag(id, word, tag); byte[] bytes = serializer.serialize("ok").getBytes("UTF-8"); ByteChunk chunk = new ByteChunk(); res.setContentLength(bytes.length); res.setContentType("application/json"); chunk.append(bytes, 0, bytes.length); res.doWrite(chunk); Runnable completion = (Runnable) req.getAttribute(CALLBACK_KEY); completion.run(); return null; } }); } else if(command.equals("/random")) { exec.submit(new Callable() { public Void call() throws Exception { res.setStatus(200); String randomWord = kindex.getRandomWord(); byte[] bytes = serializer.serialize(randomWord).getBytes("UTF-8"); ByteChunk chunk = new ByteChunk(); res.setContentLength(bytes.length); res.setContentType("application/json"); chunk.append(bytes, 0, bytes.length); res.doWrite(chunk); Runnable completion = (Runnable) req.getAttribute(CALLBACK_KEY); completion.run(); return null; } }); } else if(command.equals("/everyone")) { exec.submit(new Callable() { public Void call() throws Exception { res.setStatus(200); Set everyone = kindex.getEveryone(); byte[] bytes = serializer.serialize(everyone).getBytes("UTF-8"); ByteChunk chunk = new ByteChunk(); res.setContentLength(bytes.length); res.setContentType("application/json"); chunk.append(bytes, 0, bytes.length); res.doWrite(chunk); Runnable completion = (Runnable) req.getAttribute(CALLBACK_KEY); completion.run(); return null; } }); } else if(command.equals("/everyones")) { exec.submit(new Callable() { public Void call() throws Exception { res.setStatus(200); Set everyone = kindex.getEveryone(); StringBuilder buffer = new StringBuilder(); for(String one: everyone) { buffer.append(one).append(","); } byte[] bytes = buffer.toString().getBytes("UTF-8"); ByteChunk chunk = new ByteChunk(); res.setContentLength(bytes.length); res.setContentType("text/plain"); chunk.append(bytes, 0, bytes.length); res.doWrite(chunk); Runnable completion = (Runnable) req.getAttribute(CALLBACK_KEY); completion.run(); return null; } }); } else if(command.equals("/refresh")) { exec.submit(new Callable() { public Void call() throws Exception { res.setStatus(200); kindex.refresh(); byte[] bytes = serializer.serialize("ok").getBytes("UTF-8"); ByteChunk chunk = new ByteChunk(); res.setContentLength(bytes.length); res.setContentType("application/json"); chunk.append(bytes, 0, bytes.length); res.doWrite(chunk); Runnable completion = (Runnable) req.getAttribute(CALLBACK_KEY); completion.run(); return null; } }); } } public void afterService(Request req, Response res) throws Exception { req.action(ActionCode.ACTION_POST_REQUEST , null); } public void fireAdapterEvent(String type, Object data) { } public String decode(String in) throws UnsupportedEncodingException { return new String(in.getBytes(),"UTF-8"); } }