Skip to content

Commit

Permalink
file transfers and node departures
Browse files Browse the repository at this point in the history
  • Loading branch information
TortCode committed Sep 24, 2024
1 parent 416e0f6 commit 9eccf5f
Show file tree
Hide file tree
Showing 16 changed files with 479 additions and 194 deletions.
89 changes: 60 additions & 29 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ FROM openjdk:11
WORKDIR /user/app/

COPY ./src/ ./src/
COPY ./initdata/ ./data/
RUN find ./src/ -type f -name "*.java" > sources.txt
RUN javac -d ./out/ @sources.txt
CMD java -cp ./out/ pfs.Main ./data/$DIRECTORY $TRACKER
CMD java -cp ./out/ pfs.Main /data/$DIRECTORY $TRACKER
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ javac -d ./out/ @sources.txt
#### Tracker
Start a tracker server on any host (note the hostname down) with:
```
java -cp ./out/ pfs.Main
./tracker.sh
```

#### Peer
For each integer `i` from 1 to 15, pick a unique host and start peer `i` with:
```
java -cp ./out/ pfs.Main ./data/d{i} {tracker hostname}
./node.sh {trackerhostname} {i}
```

*curly braces {} indicates substitution with the appropriate variable
Expand All @@ -48,4 +48,6 @@ java -cp ./out/ pfs.Main ./data/d{i} {tracker hostname}
Each peer will log the following items:
- CONNECT TO <hostname>: Peer is sending a connection request to specified hostname
- CONNECT FROM <hostname>: Peer is receiving a connection request from specified hostname
- NEIGHBORS <hostnames>: Peer currently connected to the following hostnames (logged on connection request sent/received)
- NEIGHBORS <hostnames>: Peer currently connected to the following hostnames (logged on connection request sent/received)
- SEND <details...>: Sending a message
- RECV <details...>: Receiving a message
32 changes: 31 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ services:
depends_on:
- tracker
stdin_open: true
volumes:
- ./data:/data
node2:
image: p2p
environment:
Expand All @@ -19,6 +21,8 @@ services:
depends_on:
- tracker
stdin_open: true
volumes:
- ./data:/data
node3:
image: p2p
environment:
Expand All @@ -27,6 +31,8 @@ services:
depends_on:
- tracker
stdin_open: true
volumes:
- ./data:/data
node4:
image: p2p
environment:
Expand All @@ -35,6 +41,8 @@ services:
depends_on:
- tracker
stdin_open: true
volumes:
- ./data:/data
node5:
image: p2p
environment:
Expand All @@ -43,6 +51,8 @@ services:
depends_on:
- tracker
stdin_open: true
volumes:
- ./data:/data
node6:
image: p2p
environment:
Expand All @@ -51,6 +61,8 @@ services:
depends_on:
- tracker
stdin_open: true
volumes:
- ./data:/data
node7:
image: p2p
environment:
Expand All @@ -59,6 +71,8 @@ services:
depends_on:
- tracker
stdin_open: true
volumes:
- ./data:/data
node8:
image: p2p
environment:
Expand All @@ -67,6 +81,8 @@ services:
depends_on:
- tracker
stdin_open: true
volumes:
- ./data:/data
node9:
image: p2p
environment:
Expand All @@ -75,6 +91,8 @@ services:
depends_on:
- tracker
stdin_open: true
volumes:
- ./data:/data
node10:
image: p2p
environment:
Expand All @@ -83,6 +101,8 @@ services:
depends_on:
- tracker
stdin_open: true
volumes:
- ./data:/data
node11:
image: p2p
environment:
Expand All @@ -91,6 +111,8 @@ services:
depends_on:
- tracker
stdin_open: true
volumes:
- ./data:/data
node12:
image: p2p
environment:
Expand All @@ -99,6 +121,8 @@ services:
depends_on:
- tracker
stdin_open: true
volumes:
- ./data:/data
node13:
image: p2p
environment:
Expand All @@ -107,6 +131,8 @@ services:
depends_on:
- tracker
stdin_open: true
volumes:
- ./data:/data
node14:
image: p2p
environment:
Expand All @@ -115,11 +141,15 @@ services:
depends_on:
- tracker
stdin_open: true
volumes:
- ./data:/data
node15:
image: p2p
environment:
DIRECTORY: d15
TRACKER: tracker
depends_on:
- tracker
stdin_open: true
stdin_open: true
volumes:
- ./data:/data
1 change: 0 additions & 1 deletion initdata/d3/delta.txt

This file was deleted.

File renamed without changes.
19 changes: 12 additions & 7 deletions src/pfs/FileDirectory.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ public class FileDirectory {
public static class FileEntry {
public final String fileName;
public final String keyword;
public final long contentLength;

public FileEntry(String fileName, String keyword) {
public FileEntry(String fileName, String keyword, long contentLength) {
this.fileName = fileName;
this.keyword = keyword;
this.contentLength = contentLength;
}
}

Expand All @@ -37,7 +39,9 @@ public FileDirectory(String directory) {
try (BufferedReader br = Files.newBufferedReader(path)) {
keyword = br.readLine();
}
FileEntry entry = new FileEntry(fileName, keyword);
long totalLength = Files.size(path);
long contentLength = totalLength - keyword.getBytes().length - 1;
FileEntry entry = new FileEntry(fileName, keyword, contentLength);
this.fileNameMap.put(fileName, entry);
this.keywordMap.put(keyword, entry);
}
Expand All @@ -54,15 +58,14 @@ public FileEntry searchByFileName(String fileName) {
return fileNameMap.get(fileName);
}

public void createFile(String fileName, String keyword) throws IOException {
FileEntry entry = new FileEntry(fileName, keyword);
public void createFile(String fileName, String keyword, long contentLength) throws IOException {
FileEntry entry = new FileEntry(fileName, keyword, contentLength);
this.fileNameMap.put(fileName, entry);
this.keywordMap.put(keyword, entry);
Path path = root.resolve(fileName);
Files.createFile(path);
try (BufferedWriter bw = Files.newBufferedWriter(path)) {
bw.write(keyword);
bw.newLine();
bw.write('\n');
}
}

Expand All @@ -73,7 +76,9 @@ public OutputStream newFileOutput(String fileName) throws IOException {

public InputStream newFileInput(String fileName) throws IOException {
InputStream in = new BufferedInputStream(Files.newInputStream(this.root.resolve(fileName), StandardOpenOption.READ));
while (in.read() != '\n') ;
while (true) {
if (in.read() == '\n') break;
}
return in;
}
}
Loading

0 comments on commit 9eccf5f

Please sign in to comment.