RuneHive-Game
Loading...
Searching...
No Matches
OnDemandRequestWorker.java
Go to the documentation of this file.
1package dev.advo.fs.dispatch;
2
3import java.io.IOException;
4import java.nio.ByteBuffer;
5
6import org.jboss.netty.buffer.ChannelBuffer;
7import org.jboss.netty.buffer.ChannelBuffers;
8import org.jboss.netty.channel.Channel;
9
10import dev.advo.fs.fs.FileDescriptor;
11import dev.advo.fs.fs.IndexedFileSystem;
12import dev.advo.fs.net.ondemand.OnDemandRequest;
13import dev.advo.fs.net.ondemand.OnDemandResponse;
14
15/**
16 * A worker which services 'on-demand' requests.
17 * @author Graham
18 */
19public final class OnDemandRequestWorker implements Runnable {
20
21 /**
22 * The resource provider.
23 */
25 /**
26 * An object used for locking checks to see if the worker is running.
27 */
28 private final Object lock = new Object();
29 /**
30 * A flag indicating if the worker should be running.
31 */
32 private boolean running = true;
33
34 /**
35 * Creates the 'on-demand' request worker.
36 * @param fs The file system.
37 */
39 this.provider = fs;
40 }
41
42 protected ChannelRequest<OnDemandRequest> nextRequest() throws InterruptedException {
44 }
45
46 /*
47 * In this request, the server will receive a byte of 1 followed by a big endian medium int
48 * that contains the index/file being requested. The server then responds with a big endian medium
49 * for index/file, a big endian int for the size of the data and then a byte[] with the file data.
50
51 */
52 protected void service(IndexedFileSystem fs, Channel channel, OnDemandRequest request) throws IOException {
53 FileDescriptor desc = request.getFileDescriptor();
54 System.err.println("Serving file: " + desc.toString());
55
56 ByteBuffer buf = fs.getFile(desc);
57 int bufLen = buf.remaining();
58 int length = bufLen;
59
60 byte[] tmp = new byte[bufLen];
61 buf.get(tmp, 0, tmp.length);
62 ChannelBuffer chunkData = ChannelBuffers.wrappedBuffer(tmp, 0, bufLen);
63
64 OnDemandResponse response = new OnDemandResponse(desc, length, chunkData);
65 channel.write(response);
66 }
67
68 /**
69 * Stops this worker. The worker's thread may need to be interrupted.
70 */
71 public final void stop() {
72 synchronized (lock) {
73 running = false;
74 }
75 }
76
77 @Override
78 public final void run() {
79 while (true) {
80 synchronized (lock) {
81 if (!running) {
82 break;
83 }
84 }
85
87 try {
88 request = nextRequest();
89 } catch (InterruptedException e) {
90 continue;
91 }
92
93 Channel channel = request.getChannel();
94
95 try {
96 service(provider, channel, request.getRequest());
97 } catch (IOException e) {
98 // System.err.println("Error serving file: " + request.getRequest().getFileDescriptor().toString());
99 // e.printStackTrace();
100 // channel.close();
101 }
102 }
103 }
104}
A specialised request which contains a channel as well as the request object itself.
Channel getChannel()
Gets the channel.
void service(IndexedFileSystem fs, Channel channel, OnDemandRequest request)
OnDemandRequestWorker(IndexedFileSystem fs)
Creates the 'on-demand' request worker.
final IndexedFileSystem provider
The resource provider.
boolean running
A flag indicating if the worker should be running.
final Object lock
An object used for locking checks to see if the worker is running.
ChannelRequest< OnDemandRequest > nextRequest()
A class which dispatches requests to worker threads.
static ChannelRequest< OnDemandRequest > nextOnDemandRequest()
Gets the next 'on-demand' request from the queue, blocking if none are available.
A class which points to a file in the cache.
A file system based on top of the operating system's file system.