Skip to content

Commit

Permalink
Adding http server.
Browse files Browse the repository at this point in the history
  • Loading branch information
gilbva committed Feb 18, 2016
1 parent 1527b07 commit 1be67a8
Show file tree
Hide file tree
Showing 7 changed files with 335 additions and 0 deletions.
29 changes: 29 additions & 0 deletions bridje-http/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.bridje</groupId>
<artifactId>bridje-parent</artifactId>
<version>0.1.3-SNAPSHOT</version>
</parent>

<artifactId>bridje-http</artifactId>

<name>Bridje HTTP</name>
<description>Bridje HTTP Server and Client</description>
<url>http://www.bridje.org</url>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.0.34.Final</version>
</dependency>
</dependencies>
</project>
68 changes: 68 additions & 0 deletions bridje-http/src/main/java/org/bridje/http/HttpClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2016 Bridje Framework.
*
* Licensed 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.
*/

package org.bridje.http;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import java.net.InetSocketAddress;

/**
*
*/
public class HttpClient
{
private final String host;

private final int port;

public HttpClient(String host, int port)
{
this.host = host;
this.port = port;
}

public void start() throws Exception
{
EventLoopGroup group = new NioEventLoopGroup();
try
{
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.remoteAddress(new InetSocketAddress(host, port))
.handler(new ChannelInitializer<SocketChannel>()
{
@Override
public void initChannel(SocketChannel ch) throws Exception
{
ch.pipeline().addLast(new HttpClientHandler());
}
});
ChannelFuture f = b.connect().sync();
f.channel().closeFuture().sync();
}
finally
{
group.shutdownGracefully().sync();
}
}
}
54 changes: 54 additions & 0 deletions bridje-http/src/main/java/org/bridje/http/HttpClientHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2016 Bridje Framework.
*
* Licensed 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.
*/

package org.bridje.http;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
*/
@Sharable
public class HttpClientHandler extends SimpleChannelInboundHandler<ByteBuf>
{
private static final Logger LOG = Logger.getLogger(HttpClientHandler.class.getName());

@Override
public void channelActive(ChannelHandlerContext ctx)
{
ctx.writeAndFlush(Unpooled.copiedBuffer("Netty rocks!", CharsetUtil.UTF_8));
}

@Override
public void channelRead0(ChannelHandlerContext ctx, ByteBuf in)
{
LOG.log(Level.INFO, "Client received: {0}", in.toString(CharsetUtil.UTF_8));
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
{
LOG.log(Level.SEVERE, cause.getMessage(), cause);
ctx.close();
}
}
66 changes: 66 additions & 0 deletions bridje-http/src/main/java/org/bridje/http/HttpServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2016 Bridje Framework.
*
* Licensed 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.
*/

package org.bridje.http;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import java.net.InetSocketAddress;
/**
*
*/
public class HttpServer
{
private final int port;

public HttpServer(int port)
{
this.port = port;
}

public void start() throws Exception
{
final HttpServerHandler serverHandler = new HttpServerHandler();
EventLoopGroup group = new NioEventLoopGroup();
try
{
ServerBootstrap b = new ServerBootstrap();
b.group(group)
.channel(NioServerSocketChannel.class)
.localAddress(new InetSocketAddress(port))
.childHandler(new ChannelInitializer<SocketChannel>()
{
@Override
public void initChannel(SocketChannel ch)
throws Exception
{
ch.pipeline().addLast(serverHandler);
}
});
ChannelFuture f = b.bind(port).sync();
f.channel().closeFuture().sync();
}
finally
{
group.shutdownGracefully().sync();
}
}
}
58 changes: 58 additions & 0 deletions bridje-http/src/main/java/org/bridje/http/HttpServerHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2016 Bridje Framework.
*
* Licensed 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.
*/

package org.bridje.http;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
*/
@Sharable
public class HttpServerHandler extends ChannelInboundHandlerAdapter
{
private static final Logger LOG = Logger.getLogger(HttpServerHandler.class.getName());

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
{
ByteBuf in = (ByteBuf) msg;
LOG.log(Level.INFO, "Server received: {0}", in.toString(CharsetUtil.UTF_8));
ctx.write(in);
}

@Override
public void channelReadComplete(ChannelHandlerContext ctx)
{
ctx.writeAndFlush(Unpooled.EMPTY_BUFFER)
.addListener(ChannelFutureListener.CLOSE);
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
{
LOG.log(Level.SEVERE, cause.getMessage(), cause);
ctx.close();
}
}
59 changes: 59 additions & 0 deletions bridje-http/src/main/java/org/bridje/http/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2016 Bridje Framework.
*
* Licensed 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.
*/

package org.bridje.http;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
*/
public class Main
{
private static final Logger LOG = Logger.getLogger(Main.class.getName());

/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
new Thread(() ->
{
try
{
new HttpServer(8080).start();
}
catch (Exception e)
{
LOG.log(Level.SEVERE, e.getMessage(), e);
}
}).start();

new Thread(() ->
{
try
{
Thread.sleep(1000);
new HttpClient("localhost", 8080).start();
}
catch (Exception e)
{
LOG.log(Level.SEVERE, e.getMessage(), e);
}
}).start();
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<module>bridje-ioc</module>
<module>bridje-cfg</module>
<module>bridje-xsd-maven-plugin</module>
<module>bridje-http</module>
</modules>

<scm>
Expand Down

0 comments on commit 1be67a8

Please sign in to comment.