diff --git a/.gitignore b/.gitignore index 14801fc3..5710157d 100644 --- a/.gitignore +++ b/.gitignore @@ -221,3 +221,4 @@ target/ .gradle/ *.iml *.bin +tempgen/ \ No newline at end of file diff --git a/minbin-generate/pom.xml b/minbin-generate/pom.xml index c7c74909..df8c5bfd 100644 --- a/minbin-generate/pom.xml +++ b/minbin-generate/pom.xml @@ -29,6 +29,12 @@ 2.00 + + de.ruedigermoeller + temp-late + 1.0 + + com.beust jcommander diff --git a/minbin-generate/src/main/java/minbin/gen/GenContext.java b/minbin-generate/src/main/java/minbin/gen/GenContext.java new file mode 100644 index 00000000..d6cb20e7 --- /dev/null +++ b/minbin-generate/src/main/java/minbin/gen/GenContext.java @@ -0,0 +1,12 @@ +package minbin.gen; + +import de.ruedigermoeller.serialization.FSTClazzInfo; + +/** + * Created by ruedi on 27.05.2014. + */ +public class GenContext { + + public FSTClazzInfo clazz; + +} diff --git a/minbin-generate/src/main/java/minbin/gen/MBGen.java b/minbin-generate/src/main/java/minbin/gen/MBGen.java index 52373ed3..8912905c 100644 --- a/minbin-generate/src/main/java/minbin/gen/MBGen.java +++ b/minbin-generate/src/main/java/minbin/gen/MBGen.java @@ -2,6 +2,10 @@ import com.beust.jcommander.JCommander; import com.beust.jcommander.Parameter; +import de.rm.testserver.protocol.BasicValues; +import de.rm.testserver.protocol.Meta; +import de.ruedigermoeller.serialization.FSTConfiguration; +import de.ruedigermoeller.template.TemplateExecutor; import java.util.List; @@ -12,8 +16,11 @@ public class MBGen { private void generate() { + FSTConfiguration conf = FSTConfiguration.createDefaultConfiguration(); + GenContext ctx = new GenContext(); + ctx.clazz = conf.getClassInfo(BasicValues.class); if ( lang == Lang.javascript ) { - + TemplateExecutor.Run("./src/main/resources/js/js.jsp",ctx); } } diff --git a/minbin-generate/src/main/resources/js/js.jsp b/minbin-generate/src/main/resources/js/js.jsp new file mode 100644 index 00000000..fc867200 --- /dev/null +++ b/minbin-generate/src/main/resources/js/js.jsp @@ -0,0 +1,29 @@ +<% + import java.util.*; + import java.io.*; + import de.ruedigermoeller.template.*; + +// add imports you need during generation => + import minbin.gen.*; + import de.ruedigermoeller.serialization.*; + +// this header is always required to make it work. Cut & Paste this as template + public class CLAZZNAME implements IContextReceiver + { + public void receiveContext(Object o, PrintStream out) throws Exception + { + // asign your context + GenContext CTX = (GenContext)o; + FSTClazzInfo CLZ = CTX.clazz; + FSTClazzInfo.FSTFieldInfo fi[] = CLZ.getFieldInfo(); +%> +function J<%+CLZ.getClazz().getSimpleName()%>(map) { +<% for (int i = 0; i < fi.length; i++ ) { +%> this.__<%+fi[i].getField().getName()%> = ; +<% } /*for*/ +%>} +<% + // this footer is always required (to match opening braces in header + } + } +%> \ No newline at end of file diff --git a/testshell/pom.xml b/testshell/pom.xml index 653c6b4a..c1aae319 100644 --- a/testshell/pom.xml +++ b/testshell/pom.xml @@ -39,9 +39,9 @@ - io.netty - netty-all - 4.0.19.Final + de.ruedigermoeller + netty2go + 1.01 diff --git a/testshell/src/main/java/de/rm/testserver/TestServer.java b/testshell/src/main/java/de/rm/testserver/TestServer.java new file mode 100644 index 00000000..ecb30c7d --- /dev/null +++ b/testshell/src/main/java/de/rm/testserver/TestServer.java @@ -0,0 +1,64 @@ +package de.rm.testserver; + +import de.rm.testserver.protocol.BasicValues; +import de.rm.testserver.protocol.MirrorRequest; +import de.rm.testserver.protocol.Person; +import de.rm.testserver.protocol.TestRequest; +import de.ruedigermoeller.serialization.FSTConfiguration; +import io.netty.channel.ChannelHandlerContext; +import org.nustaq.netty2go.NettyWSHttpServer; +import org.nustaq.webserver.WebSocketHttpServer; + +import java.io.File; +import java.io.Serializable; + +/** + * Created by ruedi on 27.05.14. + */ +public class TestServer extends WebSocketHttpServer { + + FSTConfiguration conf = FSTConfiguration.createCrossPlatformConfiguration(); + static String ClassMap[][] = new String[][] { + { "person", Person.class.getName() }, + { "basicVals", BasicValues.class.getName() }, + { "mirror", MirrorRequest.class.getName() }, + { "testReq", TestRequest.class.getName() }, + }; + + + public TestServer(File contentRoot) { + super(contentRoot); + conf.registerCrossPlatformClassMapping(ClassMap); + } + + @Override + public void onOpen(ChannelHandlerContext ctx) { + sendWSBinaryMessage( ctx, conf.asByteArray(new BasicValues()) ); + } + + @Override + public void onBinaryMessage(ChannelHandlerContext ctx, byte[] buffer) { + Object msg = null; + try { + msg = conf.asObject(buffer); + } catch (Throwable ex) { + ex.printStackTrace(); + } + if (msg instanceof MirrorRequest) { + sendWSBinaryMessage(ctx, conf.asByteArray((Serializable) msg)); + } else { + byte error[] = conf.asByteArray("Error"); + sendWSBinaryMessage(ctx,error); + } + } + + public static void main(String[] args) throws Exception { + int port; + if (args.length > 0) { + port = Integer.parseInt(args[0]); + } else { + port = 8887; + } + new NettyWSHttpServer(port, new TestServer(new File(".") )).run(); + } +} diff --git a/testshell/src/main/java/de/rm/testserver/TestWSServer.java b/testshell/src/main/java/de/rm/testserver/TestWSServer.java deleted file mode 100644 index 71cedc67..00000000 --- a/testshell/src/main/java/de/rm/testserver/TestWSServer.java +++ /dev/null @@ -1,266 +0,0 @@ -package de.rm.testserver; - -import de.rm.testserver.protocol.BasicValues; -import de.rm.testserver.protocol.MirrorRequest; -import de.rm.testserver.protocol.Person; -import de.rm.testserver.protocol.TestRequest; -import de.ruedigermoeller.serialization.FSTConfiguration; -import io.netty.bootstrap.ServerBootstrap; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; -import io.netty.channel.*; -import io.netty.channel.nio.NioEventLoopGroup; -import io.netty.channel.socket.SocketChannel; -import io.netty.channel.socket.nio.NioServerSocketChannel; -import io.netty.handler.codec.http.*; -import io.netty.handler.codec.http.websocketx.*; -import io.netty.util.CharsetUtil; - -import java.io.Serializable; -import java.util.logging.Level; -import java.util.logging.Logger; - -import static io.netty.handler.codec.http.HttpHeaders.Names.*; -import static io.netty.handler.codec.http.HttpHeaders.*; -import static io.netty.handler.codec.http.HttpMethod.*; -import static io.netty.handler.codec.http.HttpResponseStatus.*; -import static io.netty.handler.codec.http.HttpVersion.*; - - -public class TestWSServer { - - public static String ClassMap[][] = new String[][] { - { "person", Person.class.getName() }, - { "basicVals", BasicValues.class.getName() }, - { "mirror", MirrorRequest.class.getName() }, - { "testReq", TestRequest.class.getName() }, - }; - - private final int port; - FSTConfiguration conf; - - public TestWSServer(int port) { - this.port = port; - conf = FSTConfiguration.createCrossPlatformConfiguration(); - conf.registerCrossPlatformClassMapping( ClassMap ); - } - - public void run() throws Exception { - EventLoopGroup bossGroup = new NioEventLoopGroup(1); - EventLoopGroup workerGroup = new NioEventLoopGroup(); - try { - ServerBootstrap b = new ServerBootstrap(); - b.group(bossGroup, workerGroup) - .channel(NioServerSocketChannel.class) - .childHandler(new WebSocketServerInitializer()); - - Channel ch = b.bind(port).sync().channel(); - System.out.println("Web socket server started at port " + port + '.'); - System.out.println("Open your browser and navigate to http://localhost:" + port + '/'); - - ch.closeFuture().sync(); - } finally { - bossGroup.shutdownGracefully(); - workerGroup.shutdownGracefully(); - } - } - - public class WebSocketServerInitializer extends ChannelInitializer { - @Override - public void initChannel(SocketChannel ch) throws Exception { - ChannelPipeline pipeline = ch.pipeline(); - pipeline.addLast("codec-http", new HttpServerCodec()); - pipeline.addLast("aggregator", new HttpObjectAggregator(65536)); - WebSocketServerHandler handler = new WebSocketServerHandler(); - pipeline.addLast("handler", handler); - } - } - - private static final Logger logger = Logger.getLogger(WebSocketServerHandler.class.getName()); - private static final String WEBSOCKET_PATH = "/websocket"; - - public class WebSocketServerHandler extends SimpleChannelInboundHandler { - - private WebSocketServerHandshaker handshaker; - - @Override - public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { - if (msg instanceof FullHttpRequest) { - handleHttpRequest(ctx, (FullHttpRequest) msg); - } else - if (msg instanceof WebSocketFrame) { - handleWebSocketFrame(ctx, (WebSocketFrame) msg); - } - } - - @Override - public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { - ctx.flush(); - } - - private void handleHttpRequest(final ChannelHandlerContext ctx, FullHttpRequest req) throws Exception { - // Handle a bad request. - if (!req.getDecoderResult().isSuccess()) { - sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST)); - return; - } - - // Allow only GET methods. - if (req.getMethod() != GET) { - sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN)); - return; - } - - // Send the demo page and favicon.ico - if ("/".equals(req.getUri())) { - ByteBuf content = WebSocketServerIndexPage.getContent(getWebSocketLocation(req)); - FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, OK, content); - res.headers().set(CONTENT_TYPE, "text/html; charset=UTF-8"); - setContentLength(res, content.readableBytes()); - - sendHttpResponse(ctx, req, res); - return; - } - if ("/favicon.ico".equals(req.getUri())) { - FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, NOT_FOUND); - sendHttpResponse(ctx, req, res); - return; - } - - // Handshake - WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory( - getWebSocketLocation(req), null, false); - handshaker = wsFactory.newHandshaker(req); - if (handshaker == null) { - WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel()); - } else { - handshaker.handshake(ctx.channel(), req).addListener( - new ChannelFutureListener() { - public void operationComplete(ChannelFuture future) { - onOpen(ctx.channel()); - } - } - ); - } - } - - private void handleWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) { - // Check for closing frame - if (frame instanceof CloseWebSocketFrame) { - handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain()); - onClose(this); - return; - } - if (frame instanceof PingWebSocketFrame) { - ctx.channel().write(new PongWebSocketFrame(frame.content().retain())); - return; - } - if (frame instanceof BinaryWebSocketFrame) { - ByteBuf rawMessage = frame.content(); - int size = rawMessage.readableBytes(); - byte[] buffer = new byte[size]; - rawMessage.readBytes(buffer); - onMessage(ctx.channel(), buffer); - return; - } - if (!(frame instanceof TextWebSocketFrame)) { - throw new UnsupportedOperationException(String.format("%s frame types not supported", frame.getClass() - .getName())); - } - - // Send the uppercase string back. - String request = ((TextWebSocketFrame) frame).text(); - if (logger.isLoggable(Level.FINE)) { - logger.fine(String.format("%s received %s", ctx.channel(), request)); - } - ctx.channel().write(new TextWebSocketFrame(request.toUpperCase())); - } - - private void sendHttpResponse( - ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) { - // Generate an error page if response getStatus code is not OK (200). - if (res.getStatus().code() != 200) { - ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8); - res.content().writeBytes(buf); - buf.release(); - setContentLength(res, res.content().readableBytes()); - } - - // Send the response and close the connection if necessary. - ChannelFuture f = ctx.channel().writeAndFlush(res); - if (!isKeepAlive(req) || res.getStatus().code() != 200) { - f.addListener(ChannelFutureListener.CLOSE); - } - } - - @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - cause.printStackTrace(); - ctx.close(); - } - - private String getWebSocketLocation(FullHttpRequest req) { - return "ws://" + req.headers().get(HOST) + WEBSOCKET_PATH; - } - } - - public void onOpen(final Channel webSocket) { - System.out.println("onOpen"); - webSocket.writeAndFlush(new BinaryWebSocketFrame(Unpooled.wrappedBuffer(conf.asByteArray(new BasicValues())))); - -// final Thread thread = new Thread() { -// public void run() { -// while( true ) { -// try { -// Thread.sleep(1000); -// } catch (InterruptedException e) { -// e.printStackTrace(); -// } -// webSocket.send(conf.asByteArray(map)); -// } -// } -// }; -// thread.start(); - } - - public void onClose(WebSocketServerHandler webSocketServerHandler) { - System.out.println("onClose"); - } - - public void onMessage(Channel conn, byte[] b) { - System.out.println("onMessage "+conn); - Object msg = null; - try { - msg = conf.asObject(b); - } catch (Throwable ex) { - ex.printStackTrace(); - } - if (msg instanceof MirrorRequest) { - conn.writeAndFlush(new BinaryWebSocketFrame(Unpooled.wrappedBuffer(conf.asByteArray((Serializable) msg)))); - } else { - byte error[] = conf.asByteArray("Error"); - conn.writeAndFlush(new BinaryWebSocketFrame(Unpooled.wrappedBuffer(error))); - } - } - -// @Override -// public void onMessage(WebSocket webSocket, String s) { -// System.out.println("onMessage "+s); -// } -// -// @Override -// public void onError(WebSocket webSocket, Exception e) { -// System.out.println("onError"); -// } - - public static void main(String[] args) throws Exception { - int port; - if (args.length > 0) { - port = Integer.parseInt(args[0]); - } else { - port = 8887; - } - new TestWSServer(port).run(); - } - -} \ No newline at end of file diff --git a/testshell/src/main/java/de/rm/testserver/WebSocketServerIndexPage.java b/testshell/src/main/java/de/rm/testserver/WebSocketServerIndexPage.java deleted file mode 100644 index 2396ea38..00000000 --- a/testshell/src/main/java/de/rm/testserver/WebSocketServerIndexPage.java +++ /dev/null @@ -1,80 +0,0 @@ -package de.rm.testserver; - -/* - * Copyright 2012 The Netty Project - * - * The Netty Project licenses this file to you under the Apache License, - * version 2.0 (the "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - */ - -import io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; -import io.netty.util.CharsetUtil; - -/** - * Generates the demo HTML page which is served at http://localhost:8080/ - */ -public final class WebSocketServerIndexPage { - - private static final String NEWLINE = "\r\n"; - - public static ByteBuf getContent(String webSocketLocation) { - return Unpooled.copiedBuffer( - "Web Socket Test" + NEWLINE + - "" + NEWLINE + - "" + NEWLINE + - "
" + NEWLINE + - "" + - "" + NEWLINE + - "

Output

" + NEWLINE + - "" + NEWLINE + - "
" + NEWLINE + - "" + NEWLINE + - "" + NEWLINE, CharsetUtil.US_ASCII); - } - - private WebSocketServerIndexPage() { - // Unused - } -} diff --git a/testshell/src/main/java/de/rm/testserver/protocol/Meta.java b/testshell/src/main/java/de/rm/testserver/protocol/Meta.java new file mode 100644 index 00000000..5c67c63b --- /dev/null +++ b/testshell/src/main/java/de/rm/testserver/protocol/Meta.java @@ -0,0 +1,14 @@ +package de.rm.testserver.protocol; + +import de.ruedigermoeller.serialization.annotations.Serialize; + +import java.io.Serializable; + +/** + * Created by ruedi on 27.05.2014. + */ +public class Meta implements Serializable { + + Class classes[] = { BasicValues.class, MirrorRequest.class, Person.class, TestRequest.class }; + +}