01 package net.sf.jbaobab.tutorial;
02 
03 import java.nio.channels.SocketChannel;
04 import java.nio.charset.Charset;
05 import java.util.concurrent.ExecutorService;
06 import java.util.concurrent.Executors;
07 
08 import net.sf.jbaobab.helper.IOHelper;
09 import net.sf.jbaobab.helper.ProtocolHelper;
10 import net.sf.jbaobab.io.ChannelHandler;
11 import net.sf.jbaobab.io.ExpirableSocketCore;
12 import net.sf.jbaobab.io.PacketHandler;
13 import net.sf.jbaobab.io.Protocol;
14 import net.sf.jbaobab.io.ProtocolFactory;
15 import net.sf.jbaobab.io.SimpleSocketCore;
16 import net.sf.jbaobab.io.SocketCore;
17 import net.sf.jbaobab.io.SocketHandler;
18 import net.sf.jbaobab.io.SocketHandlerFactory;
19 import net.sf.jbaobab.io.SocketPool;
20 import net.sf.jbaobab.io.ThreadedSocketCore;
21 import net.sf.jbaobab.io.ThreadedSocketPool;
22 import net.sf.jbaobab.io.impl.CRLFSeperator;
23 import net.sf.jbaobab.io.impl.RoughHttpPacket;
24 import net.sf.jbaobab.io.impl.RoughHttpProtocol;
25 import net.sf.jbaobab.io.impl.StringTranslator;
26 
27 /**
28  <p>
29  * A main class for {@link EchoHandler} and {@link RoughHttpHandler} example.
30  </p>
31  *
32  @see EchoHandler
33  @see RoughHttpHandler
34  @author Oakyoon Cha
35  */
36 public class StartUp {
37 
38     private static PacketHandler<String> ECHO_HANDLER = new EchoHandler();
39 
40     private static ProtocolFactory<RoughHttpPacket> HTTP_FACTORY = new ProtocolFactory<RoughHttpPacket>() {
41         public Protocol<RoughHttpPacket> newProtocol() {
42             return new RoughHttpProtocol();
43         }
44     };
45 
46     public static void main(String[] argsthrows Exception {
47         SocketPool pool = new ThreadedSocketPool();
48         pool.register(IOHelper.serverChannel(7007)new SocketHandlerFactory() {
49             public ChannelHandler<SocketChannel> newSocketHandler() {
50                 Protocol<String> protocol = ProtocolHelper.pack(
51                         new CRLFSeperator()new StringTranslator(
52                                 Charset.defaultCharset()));
53                 SocketCore<String> core = new SimpleSocketCore<String>(protocol);
54                 return new SocketHandler<String>(core, ECHO_HANDLER);
55             }
56         });
57         pool.register(IOHelper.serverChannel(8008)new SocketHandlerFactory() {
58             public ChannelHandler<SocketChannel> newSocketHandler() {
59                 ExecutorService executor = Executors.newSingleThreadExecutor();
60                 SocketCore<RoughHttpPacket> core = new ThreadedSocketCore<RoughHttpPacket>(
61                         HTTP_FACTORY, executor);
62                 SocketCore<RoughHttpPacket> expirable = new ExpirableSocketCore<RoughHttpPacket>(
63                         core, 600000L);
64                 return new SocketHandler<RoughHttpPacket>(expirable,
65                         new RoughHttpHandler());
66             }
67         });
68         pool.start();
69     }
70 
71 }
Java2html