aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/java/spark/network/netty/FileServer.java
blob: dd3a557ae59522187f6e15f68d352f5e19c5c900 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package spark.network.netty;

import java.net.InetSocketAddress;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.oio.OioEventLoopGroup;
import io.netty.channel.socket.oio.OioServerSocketChannel;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * Server that accept the path of a file an echo back its content.
 */
class FileServer {

  private Logger LOG = LoggerFactory.getLogger(this.getClass().getName());

  private ServerBootstrap bootstrap = null;
  private ChannelFuture channelFuture = null;
  private int port = 0;
  private Thread blockingThread = null;

  public FileServer(PathResolver pResolver, int port) {
    InetSocketAddress addr = new InetSocketAddress(port);

    // Configure the server.
    bootstrap = new ServerBootstrap();
    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.
    channelFuture = bootstrap.bind(addr);
    try {
      // Get the address we bound to.
      InetSocketAddress boundAddress =
        ((InetSocketAddress) channelFuture.sync().channel().localAddress());
      this.port = boundAddress.getPort();
    } catch (InterruptedException ie) {
      this.port = 0;
    }
  }

  /**
   * Start the file server asynchronously in a new thread.
   */
  public void start() {
    blockingThread = new Thread() {
      public void run() {
        try {
          channelFuture.channel().closeFuture().sync();
          LOG.info("FileServer exiting");
        } catch (InterruptedException e) {
          LOG.error("File server start got interrupted", e);
        }
        // NOTE: bootstrap is shutdown in stop()
      }
    };
    blockingThread.setDaemon(true);
    blockingThread.start();
  }

  public int getPort() {
    return port;
  }

  public void stop() {
    // Close the bound channel.
    if (channelFuture != null) {
      channelFuture.channel().close();
      channelFuture = null;
    }
    // Shutdown bootstrap.
    if (bootstrap != null) {
      bootstrap.shutdown();
      bootstrap = null;
    }
    // TODO: Shutdown all accepted channels as well ?
  }
}