Skip to content
Snippets Groups Projects
Commit 17ffbd83 authored by ik1g19's avatar ik1g19
Browse files

giving up with nio

parent 55372169
No related branches found
No related tags found
No related merge requests found
...@@ -27,6 +27,8 @@ public class Controller extends Server { ...@@ -27,6 +27,8 @@ public class Controller extends Server {
this.timeout = timeout; this.timeout = timeout;
this.rbPeriod = rbPeriod; this.rbPeriod = rbPeriod;
allocateBufferBytes(256);
openSelector(); openSelector();
} }
...@@ -46,13 +48,13 @@ public class Controller extends Server { ...@@ -46,13 +48,13 @@ public class Controller extends Server {
@Override @Override
protected void handleRequest(String request, SelectionKey key, ByteBuffer buffer) { protected void handleRequest(String request, SelectionKey key) {
String args[] = request.split(" "); String args[] = request.split(" ");
switch(args[0]) { switch(args[0]) {
case "JOIN": case "JOIN":
key.attach(new DstoreConnection(new Index(), Integer.parseInt(args[1]))); key.attach(new DstoreConnection(new Index(), Integer.parseInt(args[1])));
sendMessage("LIST",key,buffer); sendMessage("LIST",key);
break; break;
} }
} }
......
package ftp.dstore1; package ftp;
import ftp.DstoreConnection; import ftp.DstoreConnection;
import ftp.Index; import ftp.Index;
...@@ -38,9 +38,6 @@ public class Dstore extends Server { ...@@ -38,9 +38,6 @@ public class Dstore extends Server {
this.file_folder = file_folder; this.file_folder = file_folder;
ByteBuffer buffer = ByteBuffer.allocate(256);
// opening connection to controller // opening connection to controller
// open(addr) acts as convenience method for open() and connect() // open(addr) acts as convenience method for open() and connect()
// in blocking mode, so will wait for response before progressing // in blocking mode, so will wait for response before progressing
...@@ -53,7 +50,9 @@ public class Dstore extends Server { ...@@ -53,7 +50,9 @@ public class Dstore extends Server {
} }
sendMessage("JOIN " + port, toController, buffer); allocateBufferBytes(256);
sendMessage("JOIN " + port, toController);
openSelector(new SocketChannel[] {toController}); openSelector(new SocketChannel[] {toController});
} }
...@@ -92,21 +91,17 @@ public class Dstore extends Server { ...@@ -92,21 +91,17 @@ public class Dstore extends Server {
@Override @Override
protected void handleRequest(String request, SelectionKey key, ByteBuffer buffer) { protected void handleRequest(String request, SelectionKey key) {
String args[] = request.split(" "); String args[] = request.split(" ");
switch(args[0]) { switch(args[0]) {
case "LIST": case "LIST":
File folder = new File("./storage"); File folder = new File("storage");
String[] files = folder.list(); String[] files = folder.list();
// String fileNames = Arrays.stream(list())
// .map(File::getName)
// .reduce("", (file1, file2) -> file1 + " " + file2);
String fileNames = Arrays.stream(files) String fileNames = Arrays.stream(files)
.reduce("", (file1, file2) -> file1 + " " + file2); .reduce("", (file1, file2) -> file1 + " " + file2);
System.out.println(fileNames);
sendMessage(fileNames, key, buffer); sendMessage(fileNames, key);
break; break;
} }
} }
......
...@@ -15,15 +15,20 @@ public abstract class Server { ...@@ -15,15 +15,20 @@ public abstract class Server {
protected int timeout; protected int timeout;
protected Selector selector; protected Selector selector;
private ByteBuffer buffer;
protected void allocateBufferBytes(int bytes) {
buffer = ByteBuffer.allocate(bytes); //buffer to read/write
}
/** /**
* @desc abstract method which handles server requests based on implmentation * @desc abstract method which handles server requests based on implmentation
* @param request request to be handled * @param request request to be handled
* @param key corresponding key * @param key corresponding key
* @param buffer buffer to be used for r/w
*/ */
protected abstract void handleRequest(String request, SelectionKey key, ByteBuffer buffer); protected abstract void handleRequest(String request, SelectionKey key);
/** /**
...@@ -36,7 +41,6 @@ public abstract class Server { ...@@ -36,7 +41,6 @@ public abstract class Server {
serverSocket.bind(new InetSocketAddress(port)); serverSocket.bind(new InetSocketAddress(port));
serverSocket.configureBlocking(false); serverSocket.configureBlocking(false);
serverSocket.register(selector, SelectionKey.OP_ACCEPT); serverSocket.register(selector, SelectionKey.OP_ACCEPT);
ByteBuffer buffer = ByteBuffer.allocate(256); //buffer to read/write
System.out.println("Selector Started"); System.out.println("Selector Started");
...@@ -59,8 +63,8 @@ public abstract class Server { ...@@ -59,8 +63,8 @@ public abstract class Server {
if (key.isReadable()) { if (key.isReadable()) {
System.out.println("Channel has data to be read"); System.out.println("Channel has data to be read");
String request = takeRequest(buffer, key); String request = takeRequest(key);
handleRequest(request, key, buffer); handleRequest(request, key);
} }
iter.remove(); iter.remove();
...@@ -87,7 +91,7 @@ public abstract class Server { ...@@ -87,7 +91,7 @@ public abstract class Server {
channel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE); channel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);
} }
ByteBuffer buffer = ByteBuffer.allocate(256); //buffer to read/write buffer = ByteBuffer.allocate(256); //buffer to read/write
System.out.println("Selector Started"); System.out.println("Selector Started");
...@@ -110,8 +114,8 @@ public abstract class Server { ...@@ -110,8 +114,8 @@ public abstract class Server {
if (key.isReadable()) { if (key.isReadable()) {
System.out.println("Channel has data to be read"); System.out.println("Channel has data to be read");
String request = takeRequest(buffer, key); String request = takeRequest(key);
handleRequest(request, key, buffer); handleRequest(request, key);
} }
iter.remove(); iter.remove();
...@@ -136,12 +140,11 @@ public abstract class Server { ...@@ -136,12 +140,11 @@ public abstract class Server {
/** /**
* @desc reads request from buffer * @desc reads request from buffer
* @param buffer buffer to read request from
* @param key key linking to the channel * @param key key linking to the channel
* @return returns the request * @return returns the request
* @throws IOException * @throws IOException
*/ */
protected String takeRequest(ByteBuffer buffer, SelectionKey key) throws IOException { protected String takeRequest(SelectionKey key) throws IOException {
SocketChannel client = (SocketChannel) key.channel(); SocketChannel client = (SocketChannel) key.channel();
client.read(buffer); client.read(buffer);
String request = new String(buffer.array()).trim(); String request = new String(buffer.array()).trim();
...@@ -208,10 +211,9 @@ public abstract class Server { ...@@ -208,10 +211,9 @@ public abstract class Server {
* @desc sends a message using a given key, does not wait for a response * @desc sends a message using a given key, does not wait for a response
* @param msg message to be sent * @param msg message to be sent
* @param key key of corresponding channel to send message through * @param key key of corresponding channel to send message through
* @param buffer buffer to r/w messages
* @return * @return
*/ */
protected void sendMessage(String msg, SelectionKey key, ByteBuffer buffer) { protected void sendMessage(String msg, SelectionKey key) {
buffer = ByteBuffer.wrap(msg.getBytes()); buffer = ByteBuffer.wrap(msg.getBytes());
SocketChannel channel = (SocketChannel) key.channel(); SocketChannel channel = (SocketChannel) key.channel();
...@@ -230,10 +232,9 @@ public abstract class Server { ...@@ -230,10 +232,9 @@ public abstract class Server {
* @desc sends a message using a given channel, does not wait for a response * @desc sends a message using a given channel, does not wait for a response
* @param msg message to be sent * @param msg message to be sent
* @param channel channel to send message through * @param channel channel to send message through
* @param buffer buffer to r/w messages
* @return * @return
*/ */
protected void sendMessage(String msg, SocketChannel channel, ByteBuffer buffer) { protected void sendMessage(String msg, SocketChannel channel) {
buffer = ByteBuffer.wrap(msg.getBytes()); buffer = ByteBuffer.wrap(msg.getBytes());
try { try {
......
File moved
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment