Commits

Ning Sun committed 9cce30a

add shutdown on echo server

Comments (0)

Files changed (2)

src/main/java/info/sunng/soldat/examples/echoserver/EchoHandler.java

 
 import info.sunng.soldat.Client;
 import info.sunng.soldat.HandleAdapter;
+import info.sunng.soldat.SoldatException;
+import info.sunng.soldat.engines.SoldatEngine;
 import java.io.UnsupportedEncodingException;
 import java.nio.ByteBuffer;
-import java.util.Arrays;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
  */
 public class EchoHandler extends HandleAdapter {
 
+    private SoldatEngine engine;
+
+    public EchoHandler(SoldatEngine engine) {
+        this.engine = engine;
+    }
+
     private Logger logger  = LoggerFactory.getLogger(EchoHandler.class);
 
     @Override
             if (logger.isDebugEnabled()){
                 logger.debug(content);
             }
-            ByteBuffer response = ByteBuffer.wrap(content.getBytes("UTF-8"));
-            client.setBuffer(response);
+            if (content.trim().equals("shutdown")) {
+                try {
+                    engine.shutdown();
+                } catch(SoldatException e) {}
+            } else if (content.trim().equals("bye")) {
+                // TODO
+            } else {
+                ByteBuffer response = ByteBuffer.wrap(content.getBytes("UTF-8"));
+                client.setBuffer(response);
+            }
         } catch (UnsupportedEncodingException e) {}
     }
 

src/main/java/info/sunng/soldat/examples/echoserver/EchoServer.java

             public void run(){
                 try {
                     InetSocketAddress addr = new InetSocketAddress(8705);
-                    final Soldat s = new Soldat(new MultithreadAcceptorEngine(
-                            addr, SoldatEngine.TransportType.TCP));
-                    s.setHandler(new EchoHandler());
+                    SoldatEngine engine  = new MultithreadAcceptorEngine(
+                            addr, SoldatEngine.TransportType.TCP);
+                    final Soldat s = new Soldat(engine);
+                    s.setHandler(new EchoHandler(engine));
                     s.setProtocol(new EchoProtocol());
                     s.start();
                 } catch (SoldatException ex) {