Last active
December 14, 2015 20:19
-
-
Save shlomiv/5142879 to your computer and use it in GitHub Desktop.
netty 4 echo server in clojure https://github.com/netty/netty/blob/master/example/src/main/java/io/netty/example/echo/EchoServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; based on the discussion at https://groups.google.com/d/msg/netty/mcGswAZ5NeQ/ivG9snRSFy4J | |
(ns netty4.core | |
(:import [io.netty.bootstrap AbstractBootstrap ServerBootstrap] | |
[io.netty.channel ChannelFuture ChannelInitializer ChannelOption | |
ChannelHandlerContext ChannelInboundHandlerAdapter ChannelHandler] | |
[io.netty.handler.logging LogLevel LoggingHandler] | |
io.netty.buffer.ByteBuf | |
io.netty.channel.socket.SocketChannel | |
io.netty.channel.nio.NioEventLoopGroup | |
io.netty.channel.socket.nio.NioServerSocketChannel | |
java.util.concurrent.atomic.AtomicInteger) | |
(:use [clojure.stacktrace]) | |
(:gen-class)) | |
(def echo-server-handler | |
(proxy [ChannelInboundHandlerAdapter] [] | |
(channelRead [ctx msg] (.write ctx msg)) | |
(channelReadComplete [ctx] (.flush ctx)) | |
(exceptionCaught [ctx cause] | |
(print-stack-trace cause) | |
(.close ctx)))) | |
(defn -main [] | |
(let [b (ServerBootstrap.) | |
elg1 (NioEventLoopGroup.) | |
elg2 (NioEventLoopGroup.) | |
shutdown #(do (.shutdownGracefully elg1) | |
(.shutdownGracefully elg2))] | |
(-> b | |
(.group elg1 elg2) | |
(.channel io.netty.channel.socket.nio.NioServerSocketChannel) | |
(.option ChannelOption/SO_BACKLOG (int 100)) | |
(.childHandler | |
(proxy [ChannelInitializer] [] | |
(initChannel [ch] | |
(-> ch (.pipeline) | |
(.addLast | |
(into-array ChannelHandler [echo-server-handler])))))) | |
(.bind 2424)) | |
(println b) ; print the server bootstrap object | |
shutdown)) | |
(def r (-main)) | |
(r) ;; call to terminate server |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns index-server.netty | |
(:import [io.netty.bootstrap AbstractBootstrap ServerBootstrap] | |
[io.netty.channel ChannelFuture ChannelInitializer ChannelOption | |
ChannelHandlerContext ChannelInboundByteHandlerAdapter] | |
[io.netty.handler.logging LogLevel LoggingHandler] | |
io.netty.buffer.ByteBuf | |
io.netty.channel.socket.SocketChannel | |
io.netty.channel.nio.NioEventLoopGroup | |
io.netty.channel.socket.nio.NioServerSocketChannel | |
java.util.concurrent.atomic.AtomicInteger) | |
(:use [clojure.stacktrace]) | |
(:gen-class)) | |
(defn echo-server-handler [] | |
(proxy [ChannelInboundByteHandlerAdapter] [] | |
(inboundBufferUpdated [this ctx in] | |
(let [out (.nextOutboundByteBuffer ctx)] | |
(.writeBytes out in) | |
(.flust ctx))) | |
(exceptionCaught [this ctx cause] | |
(print-stack-trace cause) | |
(.close ctx)))) | |
(defn -main [] | |
(let [b (ServerBootstrap.)] | |
(try | |
(-> b | |
(.group (NioEventLoopGroup.) (NioEventLoopGroup.)) | |
(.channel io.netty.channel.socket.nio.NioServerSocketChannel) | |
(.option ChannelOption/SO_BACKLOG 100) | |
(.childHandler | |
(proxy [ChannelInitializer] [] | |
(initChannel [this ch] | |
(-> ch | |
(.pipeline) | |
(.addLast (LoggingHandler. LogLevel/INFO) | |
(echo-server-handler))))))) | |
(println b) ; print the server bootstrap object | |
(let [f (-> b | |
(.bind 2424) | |
(.sync))] | |
(-> f | |
(.channel) | |
(.closeFuture) | |
(.sync))) | |
(finally (.shutdown b))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns test-app.core | |
(:import [io.netty.bootstrap AbstractBootstrap ServerBootstrap] | |
[io.netty.channel ChannelFuture ChannelInitializer ChannelOption | |
ChannelHandlerContext ChannelInboundByteHandlerAdapter] | |
[io.netty.handler.logging LogLevel LoggingHandler] | |
io.netty.buffer.ByteBuf | |
io.netty.channel.socket.SocketChannel | |
io.netty.channel.nio.NioEventLoopGroup | |
io.netty.channel.socket.nio.NioServerSocketChannel | |
java.util.concurrent.atomic.AtomicInteger) | |
(:use [clojure.stacktrace]) | |
(:gen-class)) | |
(def b (ServerBootstrap.)) | |
(.group b (NioEventLoopGroup.)) | |
(.channel b NioServerSocketChannel) | |
;(.option b ChannelOption/SO_BACKLOG 100) | |
(.childHandler b | |
(proxy [ChannelInitializer] [] | |
(initChannel [ch] (println "new connection")))) | |
(.bind b 9999) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
gistfile2.clj - this is a first working version