blob: 38af3050967ebd51329d7bb821a9256142d4b8df (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
package spark.network.netty;
import java.io.File;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.Channel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.oio.OioEventLoopGroup;
import io.netty.channel.socket.oio.OioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
/**
* Server that accept the path of a file an echo back its content.
*/
public class FileServer {
private ServerBootstrap bootstrap = null;
private Channel channel = null;
private PathResolver pResolver;
public FileServer(PathResolver pResolver){
this.pResolver = pResolver;
}
public void run(int port) {
// Configure the server.
bootstrap = new ServerBootstrap();
try {
bootstrap.group(new OioEventLoopGroup(), new OioEventLoopGroup())
.channel(OioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 100)
.option(ChannelOption.SO_RCVBUF, 1500)
.childHandler(new FileServerChannelInitializer(pResolver));
// Start the server.
channel = bootstrap.bind(port).sync().channel();
channel.closeFuture().sync();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
bootstrap.shutdown();
}
}
public void stop(){
if (channel!=null){
channel.close();
}
if (bootstrap != null){
bootstrap.shutdown();
}
}
}
|