Skip to content
Snippets Groups Projects
Commit 90bb8a1f authored by ik1g19's avatar ik1g19
Browse files

add most functionality to load operation

parent b6aa6055
No related branches found
No related tags found
No related merge requests found
No preview for this file type
......@@ -12,6 +12,10 @@ public class ClientMain {
if (!downloadFolder.exists())
if (!downloadFolder.mkdir()) throw new RuntimeException("Cannot create download folder (folder absolute path: " + downloadFolder.getAbsolutePath() + ")");
File uploadFolder = new File("to_store");
if (!uploadFolder.exists())
throw new RuntimeException("to_store folder does not exist");
testClient(cport, timeout, downloadFolder);
......@@ -31,6 +35,8 @@ public class ClientMain {
try { client.store(new File("test.txt")); } catch(IOException e) { e.printStackTrace(); }
try {Thread.sleep(10000);} catch (InterruptedException e) {System.out.println(e);}
// try { client.store(new File("Clipboard01.pdf")); } catch(IOException e) { e.printStackTrace(); }
//
// try { client.store(new File("Clipboard01.jpg")); } catch(IOException e) { e.printStackTrace(); }
......@@ -38,9 +44,7 @@ public class ClientMain {
// String list[] = null;
// try { list = list(client); } catch(IOException e) { e.printStackTrace(); }
//
// if (list != null)
// for (String filename : list)
// try { client.load(filename, downloadFolder); } catch(IOException e) { e.printStackTrace(); }
try { client.load("test.txt", downloadFolder); } catch(IOException e) { e.printStackTrace(); }
/*if (list != null)
for (String filename : list)
......
package ftp;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
......@@ -15,7 +12,7 @@ public class Controller extends Server {
private int rbPeriod;
private int nextID = 0;
private DstoreIndex dstoreIndex;
private DStoreIndex dStoreIndex;
private FileIndex fileIndex;
......@@ -32,7 +29,7 @@ public class Controller extends Server {
this.timeout = timeout;
this.rbPeriod = rbPeriod;
dstoreIndex = new DstoreIndex();
dStoreIndex = new DStoreIndex();
fileIndex = new FileIndex();
start();
......@@ -69,15 +66,26 @@ public class Controller extends Server {
String files = readSocket(client);
// todo use stream instead
//
List<String[]> filesInfo = new ArrayList<String[]>() {{
String[] filesAndSizes = files.split("\\|");
for (String file : filesAndSizes) add(file.split(" "));
}};
DStoreConnection dStore;
if (!files.equals("empty")) {
List<DStoreFile> dStoreFiles = Arrays.stream(files.split("\\|")).
map(x -> x.split(" ")).
map(x -> new DStoreFile(x[0], Long.parseLong(x[1]))).
collect(Collectors.toList());
dStore = new DStoreConnection(dStoreFiles, port, nextID);
dStoreFiles.stream().forEach(x -> x.addDstore(dStore));
}
else dStore = new DStoreConnection(port, nextID);
dStoreIndex.addDStore(dStore);
dstoreIndex.addDstore(filesInfo, port, nextID);
threadIDOutput("New Dstore (ID: " + nextID + ") successfully joined");
nextID++;
......@@ -88,16 +96,20 @@ public class Controller extends Server {
String filename = args[1];
Long filesize = Long.parseLong(args[2]);
DstoreFile file = fileIndex.addFile(filename, filesize);
DStoreFile file = fileIndex.addFile(filename, filesize);
file.setStoreInProgress(true);
List<DstoreConnection> dstores = dstoreIndex.getFirstN(r);
List<DStoreConnection> dStores = dStoreIndex.getFirstN(r);
dstores.stream().forEach(x -> x.addFile(file));
dStores.stream().
forEach(x -> {
x.addFile(file);
file.addDstore(x);
});
String ports = dstores.stream().
String ports = dStores.stream().
map(x -> Integer.toString(x.getPort())).
collect(Collectors.joining(" "));
......@@ -109,7 +121,7 @@ public class Controller extends Server {
else if (command.equals("STORE_ACK")) {
String filename = args[1];
DstoreFile file = fileIndex.get(filename);
DStoreFile file = fileIndex.get(filename);
file.storeAck();
......@@ -120,6 +132,15 @@ public class Controller extends Server {
threadIDOutput("Store of file " + filename + " complete");
}
}
else if (command.equals("LOAD")) {
String filename = args[1];
DStoreFile file = fileIndex.get(filename);
int dStorePort = file.getDstore().getPort();
send("LOAD_FROM " + dStorePort + " " + file.getFilesize(), client);
}
}
}
\ No newline at end of file
package ftp;
import ftp.DstoreConnection;
import ftp.Server;
import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Dstore extends Server {
public class DStore extends Server {
int cport;
String file_folder;
......@@ -29,7 +21,7 @@ public class Dstore extends Server {
* @param timeout timeout (ms)
* @param file_folder where to store data locally
*/
public Dstore(int port, int cport, int timeout, String file_folder) {
public DStore(int port, int cport, int timeout, String file_folder) {
this.port = port;
this.cport = cport;
......@@ -79,7 +71,7 @@ public class Dstore extends Server {
.collect(Collectors.toList());
Dstore dstore = new Dstore(intArgs.get(0), intArgs.get(1), intArgs.get(2), args[3]);
DStore dStore = new DStore(intArgs.get(0), intArgs.get(1), intArgs.get(2), args[3]);
}
......@@ -94,20 +86,17 @@ public class Dstore extends Server {
if (command.equals("LIST")) {
File folder = new File(file_folder);
String fileMessage = "empty";
//todo use stream instead
//
List<String> files = new ArrayList<String>() {{
for (File file : folder.listFiles()) add(file.getName() + " " + file.length());
}};
if (folder.listFiles().length > 0) {
List<String> files = Arrays.stream(folder.listFiles()).
map(x -> x.getName() + " " + x.length()).
collect(Collectors.toList());
// todo use joining instead of reduce
//
String ident = files.get(0);
files.remove(0);
String fileMessage = files.stream()
.reduce(ident, (file1, file2) -> (file1 + "|" + file2));
fileMessage = files.stream().collect(Collectors.joining("|"));
}
send(fileMessage, client);
......@@ -137,6 +126,17 @@ public class Dstore extends Server {
send("STORE_ACK " + filename, controller);
} else threadIDErr("Unable to connect to Controller");
}
else if (command.equals("LOAD_DATA")) {
String filename = args[1];
try {
sendFile(client, file_folder + "\\" + filename);
} catch (IOException e) {
threadIDErr(e.getMessage());
}
}
}
......@@ -163,4 +163,23 @@ public class Dstore extends Server {
}
public void sendFile(Socket client, String filepath) throws IOException {
File inputFile = new File(filepath);
FileInputStream inf = new FileInputStream(inputFile);
OutputStream out = client.getOutputStream();
byte[] buf = new byte[1000]; int buflen;
while ((buflen = inf.read(buf)) != -1) {
out.write(buf, 0, buflen);
}
inf.close(); client.close(); out.close();
}
}
......@@ -3,7 +3,7 @@ package ftp;
import java.util.List;
import java.util.stream.Collectors;
public class DstoreConnection {
public class DStoreConnection {
private FileIndex file_index;
private int port;
......@@ -11,22 +11,36 @@ public class DstoreConnection {
public DstoreConnection(List<String[]> files, int port, int id) {
// public DStoreConnection(List<String[]> files, int port, int id) {
// this.port = port;
// this.id = id;
//
// List<DStoreFile> dStoreFiles = files.stream()
// .map(x -> new DStoreFile(x[0],Long.parseLong(x[1])))
// .collect(Collectors.toList());
//
// file_index = new FileIndex(dStoreFiles);
// }
public DStoreConnection(int port, int id) {
this.port = port;
this.id = id;
List<DstoreFile> dstoreFiles = files.stream()
.map(x -> new DstoreFile(x[0],Long.parseLong(x[1])))
.collect(Collectors.toList());
file_index = new FileIndex();
}
public DStoreConnection(List<DStoreFile> dStoreFiles, int port, int id) {
this.port = port;
this.id = id;
file_index = new FileIndex(dstoreFiles);
file_index = new FileIndex(dStoreFiles);
}
public void addFile(String filename, Long filesize) { DstoreFile file = file_index.addFile(filename,filesize); }
public void addFile(String filename, Long filesize) { DStoreFile file = file_index.addFile(filename,filesize); }
public void addFile(DstoreFile file) { file_index.put(file.getFilename(),file); }
public void addFile(DStoreFile file) { file_index.put(file.getFilename(),file); }
......
package ftp;
public class DstoreFile {
public class DStoreFile {
private String filename;
private Long filesize;
......@@ -13,15 +13,15 @@ public class DstoreFile {
private int storeAcksQuota;
private int storeAcks = 0;
private DstoreIndex dstoreIndex;
private DStoreIndex dStoreIndex;
public DstoreFile(String filename, Long filesize) {
public DStoreFile(String filename, Long filesize) {
this.filename = filename;
this.filesize = filesize;
dstoreIndex = new DstoreIndex();
dStoreIndex = new DStoreIndex();
}
......@@ -31,6 +31,8 @@ public class DstoreFile {
public boolean isRemoveInProgress() {return removeInProgress;}
public boolean isRemoveComplete() {return removeComplete;}
public void setStoreInProgress(Boolean store) { storeInProgress = store; }
public void setStoreComplete(Boolean complete) { storeComplete = complete; }
public void setRemoveInProgress(Boolean remove) { removeInProgress = remove; }
......@@ -45,13 +47,24 @@ public class DstoreFile {
public void setStoreAcksQuota(int quota) { storeAcksQuota = quota; }
public int getStoreAcks() { return storeAcks; }
public int storeAck() { return storeAcks++; }
public Boolean storeAckCheck() { return storeAcks++ == storeAcksQuota; }
public Boolean ackCheck() { return storeAcks == storeAcksQuota; }
public void addDstore(DstoreConnection dstore) { dstoreIndex.put(dstore.getID(),dstore); }
public void addDstore(DStoreConnection dStore) { dStoreIndex.put(dStore.getID(),dStore); }
public DStoreConnection getDstore() {
return dStoreIndex.getFirstAvailable();
}
}
package ftp;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
public class DStoreIndex extends HashMap<Integer, DStoreConnection> {
public DStoreIndex() {}
public DStoreConnection addDStore(List<DStoreFile> files, int port, Integer id) {
DStoreConnection dStore = new DStoreConnection(files,port,id);
put(id, dStore);
return dStore;
}
public DStoreConnection addDStore(DStoreConnection dStore) {
put(dStore.getID(), dStore);
return dStore;
}
public List<DStoreConnection> getFirstN(int n) {
return entrySet().stream()
.map(x -> x.getValue())
.limit(n)
.collect(Collectors.toList());
}
public DStoreConnection getFirstAvailable() {
return entrySet().stream()
.map(x -> x.getValue())
.collect(Collectors.toList())
.get(0);
}
}
package ftp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
public class DstoreIndex extends HashMap<Integer,DstoreConnection> {
public DstoreIndex() {}
public DstoreConnection addDstore(List<String[]> files, int port, Integer id) {
DstoreConnection dstore = new DstoreConnection(files,port,id);
put(id, dstore);
return dstore;
}
public List<DstoreConnection> getFirstN(int n) {
return entrySet().stream()
.map(x -> x.getValue())
.limit(n)
.collect(Collectors.toList());
}
}
......@@ -3,18 +3,18 @@ package ftp;
import java.util.HashMap;
import java.util.List;
public class FileIndex extends HashMap<String,DstoreFile> {
public class FileIndex extends HashMap<String, DStoreFile> {
public FileIndex() {}
public FileIndex(List<DstoreFile> list) {
public FileIndex(List<DStoreFile> list) {
list.stream().forEach(x -> put(x.getFilename(),x));
}
public DstoreFile addFile(String filename, Long filesize) {
DstoreFile file = new DstoreFile(filename,filesize);
public DStoreFile addFile(String filename, Long filesize) {
DStoreFile file = new DStoreFile(filename,filesize);
put(filename,file);
return file;
}
......
......@@ -9,6 +9,7 @@ public abstract class Server {
protected int timeout;
protected void start() {
try {
ServerSocket ss = new ServerSocket(port);
......@@ -40,9 +41,11 @@ public abstract class Server {
}
protected abstract void handleRequest(String request, Socket client);
protected void send(String msg, Socket socket) {
try {
PrintWriter out = new PrintWriter(socket.getOutputStream());
......@@ -55,7 +58,6 @@ public abstract class Server {
}
}
protected void send(String msg, String hostname, int port) {
try {
Socket socket = new Socket(hostname, port);
......@@ -70,6 +72,7 @@ public abstract class Server {
}
protected String readSocket(Socket socket) {
String request = null;
......@@ -86,6 +89,7 @@ public abstract class Server {
}
protected void threadIDOutput(String output) {
System.out.println("Thread ID: " + Thread.currentThread().getId() + " " + output);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment