Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 106 additions & 18 deletions commander/src/main/java/hse/java/commander/MainController.java
Original file line number Diff line number Diff line change
@@ -1,45 +1,133 @@
package hse.java.commander;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;

import java.nio.file.Path;
import javafx.scene.input.MouseEvent;

public class MainController {

@FXML
public ListView<String> left;
private enum FileOperation {
COPY, MOVE, DELETE
}

@FXML
public ListView<String> right;
@FXML public ListView<String> left;
@FXML public ListView<String> right;

@FXML
public Button move;
@FXML public Button move;
@FXML public Button copy;
@FXML public Button delete;

private Path leftDir;
private Path rightDir;

private boolean isLeftActive = true;

// for testing
public void setInitialDirs(Path leftStart, Path rightStart) {
this.leftDir = leftStart;
this.rightDir = rightStart;
refreshPanels();
}

private void updateList(Path dir, ListView<String> dir_list) {
dir_list.getItems().clear();
dir_list.getItems().add("...");

if (dir != null) {
try (java.nio.file.DirectoryStream<Path> dirStream = java.nio.file.Files.newDirectoryStream(dir)) {
for (Path path : dirStream) {
dir_list.getItems().add(path.getFileName().toString());
}
} catch (java.io.IOException e) {
System.err.println(e.getMessage());
}
}
}

public void initialize() {
move.setOnMouseClicked(event -> {
if (leftDir == null) leftDir = Path.of(System.getProperty("user.home"));
if (rightDir == null) rightDir = Path.of(System.getProperty("user.home"));

refreshPanels();

left.setOnMouseClicked(event -> handlePanelClick(event, left, true));
right.setOnMouseClicked(event -> handlePanelClick(event, right, false));

if (copy != null) copy.setOnAction(e -> executeOperation(FileOperation.COPY));
if (move != null) move.setOnAction(e -> executeOperation(FileOperation.MOVE));
if (delete != null) delete.setOnAction(e -> executeOperation(FileOperation.DELETE));
}

private void handlePanelClick(MouseEvent event, ListView<String> list, boolean isLeft) {
isLeftActive = isLeft;

if (isLeft) {
right.getSelectionModel().clearSelection();
} else {
left.getSelectionModel().clearSelection();
}

});
System.out.println(System.getProperty("user.home"));
left.getItems().add("Kek");
if (event.getClickCount() == 2) {
String selected = list.getSelectionModel().getSelectedItem();
if (selected == null) return;

left.setOnMouseClicked(event -> {
if (event.getClickCount() == 2) {
int index = left.getSelectionModel().getSelectedIndex();
if (index >= 0) {
left.getItems().set(index, "clicked");
Path currentDir = isLeft ? leftDir : rightDir;
Path newDir;

if (selected.equals("...")) {
newDir = currentDir.getParent();
} else {
newDir = currentDir.resolve(selected);
}

if (newDir != null && Files.isDirectory(newDir)) {
if (isLeft) leftDir = newDir; else rightDir = newDir;
refreshPanels();
}
}
}

private void executeOperation(FileOperation op) {
ListView<String> activeList = isLeftActive ? left : right;
Path sourceDir = isLeftActive ? leftDir : rightDir;
Path targetDir = isLeftActive ? rightDir : leftDir;

String selected = activeList.getSelectionModel().getSelectedItem();

if (selected == null || selected.equals("...")) return;

Path sourcePath = sourceDir.resolve(selected);

try {
switch (op) {
case COPY -> {
Path targetPath = targetDir.resolve(selected);
Files.copy(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING);
}
case MOVE -> {
Path targetPath = targetDir.resolve(selected);
Files.move(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING);
}
case DELETE -> {
Files.delete(sourcePath);
}
}
});
} catch (IOException e) {
System.err.println(e.getMessage());
}

refreshPanels();
}

private void refreshPanels() {
updateList(leftDir, left);
updateList(rightDir, right);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,77 @@

public class BoundedBlockingQueue<T> {

private class Node<T> {
T value;
Node<T> next;

Node(T value_) {
value = value_;
next = null;
}
}

private final int capacity_;
private int size_;
private Node<T> first = null;
private Node<T> last = null;

public BoundedBlockingQueue(int capacity) {
if (capacity <= 0) {
throw new IllegalArgumentException();
}
capacity_ = capacity;
}

public synchronized void put(T item) throws InterruptedException {
if (item == null) {
throw new NullPointerException("");
}
Node<T> node = new Node<>(item);
while (isFull()) {
this.wait();
}
if (first == null) {
first = node;
last = node;
} else {
last.next = node;
last = node;
}
size_++;
this.notifyAll();
}

public void put(T item) throws InterruptedException {
public synchronized boolean isFull() {
return size_ == capacity_;
}

public synchronized boolean isEmpty() {
return size_ == 0;
}

public T take() throws InterruptedException {
return null;
public synchronized T take() throws InterruptedException {
while (isEmpty()) {
this.wait();
}

T ret = first.value;

first = first.next;
if (first == null) {
last = null;
}
size_--;
this.notifyAll();

return ret;
}

public int size() {
return 0;
public synchronized int size() {
return size_;
}

public int capacity() {
return 0;
public synchronized int capacity() {
return capacity_;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package hse.java.lectures.lecture6.tasks.synchronizer;

import lombok.Getter;

import java.io.PrintStream;

import lombok.Getter;

public class StreamWriter implements Runnable {

private final String message;
Expand All @@ -26,11 +26,20 @@ public void attachMonitor(StreamingMonitor monitor) {

@Override
public void run() {
// Writer threads are intentionally infinite for the task contract.
while (true) {
output.print(message);
onTick.run();
try {
if (!monitor.waitMyTurn(id)) {
break;
}

output.print(message);
onTick.run();

monitor.finishMyTurn();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
}
}
}

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
package hse.java.lectures.lecture6.tasks.synchronizer;

public class StreamingMonitor {
// impl your sync here
}
private final int[] orderedIds;
private final int maxTotalTicks;
private int currentTurnIndex = 0;
private int currentTotalTicks = 0;

public StreamingMonitor(int[] orderedIds, int ticksPerWriter) {
this.orderedIds = orderedIds;
this.maxTotalTicks = orderedIds.length * ticksPerWriter;
}

public synchronized boolean waitMyTurn(int id) throws InterruptedException {
while (currentTotalTicks < maxTotalTicks && orderedIds[currentTurnIndex] != id) {
wait();
}
return currentTotalTicks < maxTotalTicks;
}

public synchronized void finishMyTurn() {
currentTotalTicks++;
currentTurnIndex = (currentTurnIndex + 1) % orderedIds.length;
notifyAll();
}

public synchronized void awaitCompletion() throws InterruptedException {
while (currentTotalTicks < maxTotalTicks) {
wait();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,25 @@ public Synchronizer(List<StreamWriter> tasks, int ticksPerWriter) {
this.ticksPerWriter = ticksPerWriter;
}

/**
* Starts infinite writer threads and waits until each writer prints exactly ticksPerWriter ticks
* in strict ascending id order.
*/
public void execute() {
// add monitor and sync
int[] orderedIds = tasks.stream()
.mapToInt(StreamWriter::getId)
.sorted()
.toArray();

StreamingMonitor monitor = new StreamingMonitor(orderedIds, ticksPerWriter);

for (StreamWriter writer : tasks) {
writer.attachMonitor(monitor);
Thread worker = new Thread(writer, "stream-writer-" + writer.getId());
worker.setDaemon(true);
worker.start();
}
}

}
try {
monitor.awaitCompletion();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
Loading