Skip to content
Snippets Groups Projects
Commit 7335234d authored by ik1g19's avatar ik1g19
Browse files

add further classes

added classes
 - ClientConnection
 - DstoreConnection
 - Connection
 - Dstore
 - Index
 - Server
parent 870d441e
No related branches found
No related tags found
No related merge requests found
......@@ -22,7 +22,7 @@ public class Client {
/**
* @desc constructs a client
* @param cport controller port to use
* @param cport controller port to talk to
* @param timeout timeout (ms)
*/
public Client(int cport, int timeout) {
......@@ -46,7 +46,6 @@ public class Client {
buffer.clear();
client.read(buffer);
response = new String(buffer.array()).trim();
System.out.println("response=" + response);
buffer.clear();
} catch (IOException e) {
e.printStackTrace();
......@@ -67,14 +66,14 @@ public class Client {
Scanner scanner = new Scanner(System.in);
String command = "";
while (!command.equals("quit")) {
while (!command.equals("QUIT")) {
System.out.println("Enter Command:");
command = scanner.nextLine();
String response = instance.sendMessage(command);
System.out.println(response);
System.out.println("Response: " + response);
}
}
......
package ftp;
public class ClientConnection extends Connection {
}
package ftp;
public abstract class Connection {
}
......@@ -18,8 +18,6 @@ public class Controller {
int timeout;
int rbPeriod;
private static final String POISON_PILL = "POISON_PILL";
/**
* @desc constructs a controller
......@@ -40,7 +38,7 @@ public class Controller {
serverSocket.bind(new InetSocketAddress(cport));
serverSocket.configureBlocking(false);
serverSocket.register(selector, SelectionKey.OP_ACCEPT);
ByteBuffer buffer = ByteBuffer.allocate(256);
ByteBuffer buffer = ByteBuffer.allocate(256); //buffer to read/write
while (true) {
selector.select();
......@@ -55,34 +53,34 @@ public class Controller {
}
if (key.isReadable()) {
answerWithEcho(buffer, key);
takeRequest(buffer, key);
}
iter.remove();
}
}
}
private static void answerWithEcho(ByteBuffer buffer, SelectionKey key)
throws IOException {
private static void takeRequest(ByteBuffer buffer, SelectionKey key) throws IOException {
SocketChannel client = (SocketChannel) key.channel();
client.read(buffer);
if (new String(buffer.array()).trim().equals(POISON_PILL)) {
client.close();
System.out.println("Not accepting client messages anymore");
String command = new String(buffer.array()).trim();
System.out.println("Received: " + command);
switch(command) {
case "DSTORE":
key.attach(new DstoreConnection(new Index()));
break;
}
else {
buffer.flip();
buffer.flip(); //reset pos to 0, sets limit to what was wrote, ready for reading
client.write(buffer);
buffer.clear();
}
buffer.clear(); //empties buffer and clears, ready for writing
}
private static void register(Selector selector, ServerSocketChannel serverSocket)
throws IOException {
private static void register(Selector selector, ServerSocketChannel serverSocket) throws IOException {
SocketChannel client = serverSocket.accept();
client.configureBlocking(false);
client.register(selector, SelectionKey.OP_READ);
......
package ftp;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Dstore {
int port;
int cport;
int timeout;
String file_folder;
private static SocketChannel dstore;
private static ByteBuffer buffer;
/**
* @desc constructs a client
* @param port port to listen on
* @param cport controller port to talk to
* @param timeout timeout (ms)
* @param file_folder where to store data locally
*/
public Dstore(int port, int cport, int timeout, String file_folder) throws IOException{
this.port = port;
this.cport = cport;
this.timeout = timeout;
this.file_folder = file_folder;
// opening connection to controller
// open(addr) acts as convenience method for open() and connect()
// in blocking mode, so will wait for response before progressing
//
try {
dstore = SocketChannel.open(new InetSocketAddress(cport));
buffer = ByteBuffer.allocate(256);
} catch (IOException e) {
e.printStackTrace();
}
sendMessage("DSTORE");
// creating server socket to receive requests and files
//
ServerSocketChannel serverSocket = ServerSocketChannel.open();
serverSocket.bind(new InetSocketAddress(port));
while (true) {
SocketChannel client = serverSocket.accept();
new Thread( () -> {
} ).start();
}
}
public String sendMessage(String msg) {
buffer = ByteBuffer.wrap(msg.getBytes());
String response = null;
try {
dstore.write(buffer);
buffer.clear();
dstore.read(buffer);
response = new String(buffer.array()).trim();
buffer.clear();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
private static void takeRequest(ByteBuffer buffer, SelectionKey key)
throws IOException {
SocketChannel client = (SocketChannel) key.channel();
client.read(buffer);
String command = new String(buffer.array()).trim();
System.out.println("Received: " + command);
buffer.flip(); //resets pos to 0 to read from buffer and sets limit to what was put there
client.write(buffer);
buffer.clear(); //empties buffer and clears, ready for writing
}
private static void register(Selector selector, ServerSocketChannel serverSocket)
throws IOException {
SocketChannel client = serverSocket.accept();
client.configureBlocking(false);
client.register(selector, SelectionKey.OP_READ);
}
public static void main(String args[]) {
String numberArgs[] = Arrays.copyOf(args, args.length-1);
Stream<String> str = Arrays.stream(numberArgs);
List<Integer> intArgs = str.map(x -> {return Integer.parseInt(x);})
.collect(Collectors.toList());
try {
Dstore dstore = new Dstore(intArgs.get(0), intArgs.get(1), intArgs.get(2), args[3]);
} catch (IOException e) {
System.out.println("IOException " + e.getMessage());
}
}
}
package ftp;
public class DstoreConnection extends Connection {
private Index file_index;
public DstoreConnection(Index file_index) {
this.file_index = file_index;
}
}
package ftp;
public class DstoreFile {
private String filename;
private int filesize;
public DstoreFile(String filename, int filesize) {
this.filename = filename;
this.filesize = filesize;
}
}
package ftp;
import java.util.ArrayList;
import java.util.List;
public class Index {
private ArrayList<DstoreFile> files = new ArrayList<DstoreFile>();
public Index() {}
public Index(List<DstoreFile> files) {
this.files = new ArrayList<DstoreFile>(files);
}
public void addFile(String filename, int filesize) {
files.add(new DstoreFile(filename,filesize));
}
public ArrayList<DstoreFile> getIndex() {
return files;
}
}
package ftp;
public abstract class Server {
public abstract void handleRequests();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment