-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Netty: add Netty 4 as a new Netty backend and make it default
The old Netty 3 backend was moved to play.server.netty3 The new Netty 4 backend is placed to play.server.netty4 The play.server.Server class was retained to a shortcut for Netty 4.
- Loading branch information
Showing
23 changed files
with
1,713 additions
and
96 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package play.server; | ||
|
||
import play.Invocation; | ||
|
||
public abstract class NettyInvocation extends Invocation { | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,13 @@ | ||
package play.server; | ||
|
||
import org.jboss.netty.bootstrap.ServerBootstrap; | ||
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import play.Play; | ||
import play.Play.Mode; | ||
|
||
import java.net.InetAddress; | ||
import java.net.InetSocketAddress; | ||
import java.net.UnknownHostException; | ||
import java.util.concurrent.Executors; | ||
|
||
import static java.lang.Integer.parseInt; | ||
|
||
public class Server { | ||
private static final Logger logger = LoggerFactory.getLogger(Server.class); | ||
|
||
public static int httpPort; | ||
private final Play play; | ||
|
||
public class Server extends play.server.netty4.Server { | ||
public Server(Play play) { | ||
this(play, parseInt(Play.configuration.getProperty("http.port", "9000"))); | ||
super(play); | ||
} | ||
|
||
public Server(Play play, int port) { | ||
this.play = play; | ||
httpPort = port; | ||
} | ||
|
||
public void start() { | ||
System.setProperty("file.encoding", "utf-8"); | ||
|
||
ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory( | ||
Executors.newCachedThreadPool(), Executors.newCachedThreadPool()) | ||
); | ||
InetAddress address = address(); | ||
bootstrap.setPipelineFactory(new HttpServerPipelineFactory(Play.invoker, play.getActionInvoker())); | ||
bootstrap.bind(new InetSocketAddress(address, httpPort)); | ||
bootstrap.setOption("child.tcpNoDelay", true); | ||
|
||
if (Play.mode == Mode.DEV) { | ||
if (address == null) { | ||
logger.info("Listening for HTTP on port {} (Waiting a first request to start) ...", httpPort); | ||
} else { | ||
logger.info("Listening for HTTP at {}:{} (Waiting a first request to start) ...", address, httpPort); | ||
} | ||
} else { | ||
if (address == null) { | ||
logger.info("Listening for HTTP on port {} ...", httpPort); | ||
} else { | ||
logger.info("Listening for HTTP at {}:{} ...", address, httpPort); | ||
} | ||
} | ||
} | ||
|
||
private InetAddress address() { | ||
if (Play.configuration.getProperty("http.address") != null) { | ||
return address(Play.configuration.getProperty("http.address")); | ||
} | ||
|
||
if (System.getProperties().containsKey("http.address")) { | ||
return address(System.getProperty("http.address")); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private InetAddress address(String host) { | ||
try { | ||
return InetAddress.getByName(host); | ||
} | ||
catch (UnknownHostException e) { | ||
throw new RuntimeException("Cannot resolve address " + host, e); | ||
} | ||
super(play, port); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
framework/src/play/server/ByteRange.java → ...ork/src/play/server/netty3/ByteRange.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package play.server; | ||
package play.server.netty3; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
2 changes: 1 addition & 1 deletion
2
...ework/src/play/server/ByteRangeInput.java → ...rc/play/server/netty3/ByteRangeInput.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
3 changes: 2 additions & 1 deletion
3
...rk/src/play/server/FileChannelBuffer.java → ...play/server/netty3/FileChannelBuffer.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
2 changes: 1 addition & 1 deletion
2
framework/src/play/server/FileService.java → ...k/src/play/server/netty3/FileService.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
2 changes: 1 addition & 1 deletion
2
...k/src/play/server/FlashPolicyHandler.java → ...lay/server/netty3/FlashPolicyHandler.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
2 changes: 1 addition & 1 deletion
2
...lay/server/HttpServerPipelineFactory.java → ...ver/netty3/HttpServerPipelineFactory.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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package play.server.netty3; | ||
|
||
import org.jboss.netty.bootstrap.ServerBootstrap; | ||
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import play.Play; | ||
import play.Play.Mode; | ||
|
||
import java.net.InetAddress; | ||
import java.net.InetSocketAddress; | ||
import java.net.UnknownHostException; | ||
import java.util.concurrent.Executors; | ||
|
||
import static java.lang.Integer.parseInt; | ||
|
||
public class Server { | ||
private static final Logger logger = LoggerFactory.getLogger(Server.class); | ||
|
||
public static int httpPort; | ||
private final Play play; | ||
|
||
public Server(Play play) { | ||
this(play, parseInt(Play.configuration.getProperty("http.port", "9000"))); | ||
} | ||
|
||
public Server(Play play, int port) { | ||
this.play = play; | ||
httpPort = port; | ||
} | ||
|
||
public void start() { | ||
System.setProperty("file.encoding", "utf-8"); | ||
|
||
ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory( | ||
Executors.newCachedThreadPool(), Executors.newCachedThreadPool()) | ||
); | ||
InetAddress address = address(); | ||
bootstrap.setPipelineFactory(new HttpServerPipelineFactory(Play.invoker, play.getActionInvoker())); | ||
bootstrap.bind(new InetSocketAddress(address, httpPort)); | ||
bootstrap.setOption("child.tcpNoDelay", true); | ||
|
||
if (Play.mode == Mode.DEV) { | ||
if (address == null) { | ||
logger.info("Listening for HTTP on port {} (Waiting a first request to start) ...", httpPort); | ||
} else { | ||
logger.info("Listening for HTTP at {}:{} (Waiting a first request to start) ...", address, httpPort); | ||
} | ||
} else { | ||
if (address == null) { | ||
logger.info("Listening for HTTP on port {} ...", httpPort); | ||
} else { | ||
logger.info("Listening for HTTP at {}:{} ...", address, httpPort); | ||
} | ||
} | ||
} | ||
|
||
private InetAddress address() { | ||
if (Play.configuration.getProperty("http.address") != null) { | ||
return address(Play.configuration.getProperty("http.address")); | ||
} | ||
|
||
if (System.getProperties().containsKey("http.address")) { | ||
return address(System.getProperty("http.address")); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private InetAddress address(String host) { | ||
try { | ||
return InetAddress.getByName(host); | ||
} | ||
catch (UnknownHostException e) { | ||
throw new RuntimeException("Cannot resolve address " + host, e); | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...rc/play/server/StreamChunkAggregator.java → .../server/netty3/StreamChunkAggregator.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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package play.server.netty4; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import play.exceptions.UnexpectedException; | ||
|
||
import java.io.IOException; | ||
import java.io.RandomAccessFile; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
||
class ByteRange { | ||
private static final Logger logger = LoggerFactory.getLogger(ByteRange.class); | ||
|
||
private final String file; | ||
private final RandomAccessFile raf; | ||
final long start; | ||
final long end; | ||
private final byte[] header; | ||
private int servedHeader; | ||
private int servedRange; | ||
|
||
ByteRange(String file, RandomAccessFile raf, long start, long end, long fileLength, String contentType, boolean includeHeader) { | ||
this.file = file; | ||
this.raf = raf; | ||
this.start = start; | ||
this.end = end; | ||
if(includeHeader) { | ||
header = ByteRangeInput.makeRangeBodyHeader(ByteRangeInput.DEFAULT_SEPARATOR, contentType, start, end, fileLength).getBytes(UTF_8); | ||
} else { | ||
header = new byte[0]; | ||
} | ||
} | ||
|
||
private long length() { | ||
return end - start + 1; | ||
} | ||
|
||
long remaining() { | ||
return end - start + 1 - servedRange; | ||
} | ||
|
||
long computeTotalLength() { | ||
return length() + header.length; | ||
} | ||
|
||
int fill(byte[] into, int offset) { | ||
logger.trace("fill {} at {}", file, offset); | ||
int count = 0; | ||
for(; offset < into.length && servedHeader < header.length; offset++, servedHeader++, count++) { | ||
into[offset] = header[servedHeader]; | ||
} | ||
if(offset < into.length) { | ||
try { | ||
raf.seek(start + servedRange); | ||
long maxToRead = remaining() > (into.length - offset) ? (into.length - offset) : remaining(); | ||
if(maxToRead > Integer.MAX_VALUE) { | ||
logger.debug("FileService: maxToRead >= 2^32 ! ({})", file); | ||
maxToRead = Integer.MAX_VALUE; | ||
} | ||
int read = raf.read(into, offset, (int) maxToRead); | ||
if(read < 0) { | ||
throw new UnexpectedException("error while reading file : no more to read ! length=" + raf.length() + ", seek=" + (start + servedRange)); | ||
} | ||
count += read; | ||
servedRange += read; | ||
} catch(IOException e) { | ||
throw new UnexpectedException(e); | ||
} | ||
} | ||
return count; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ByteRange(" + start + "," + end + "@" + file + ")"; | ||
} | ||
} |
Oops, something went wrong.