mirror of
https://github.com/sirjonasxx/G-Earth.git
synced 2025-01-18 16:26:26 +01:00
finished scheduler + queue fix
This commit is contained in:
parent
19c7643519
commit
6e0ac613c5
@ -12,23 +12,23 @@ import java.net.InetAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.PriorityQueue;
|
||||
import java.util.Queue;
|
||||
import java.util.*;
|
||||
|
||||
public class HConnection {
|
||||
|
||||
|
||||
private volatile Queue<HPacket> sendToClientAsyncQueue = new PriorityQueue<>();
|
||||
private volatile Queue<HPacket> sendToServerAsyncQueue = new PriorityQueue<>();
|
||||
private final Queue<HPacket> sendToClientAsyncQueue = new LinkedList<>();
|
||||
private final Queue<HPacket> sendToServerAsyncQueue = new LinkedList<>();
|
||||
public HConnection() {
|
||||
new Thread(() -> {
|
||||
while (true) {
|
||||
HPacket packet;
|
||||
while ((packet = sendToClientAsyncQueue.poll()) != null) {
|
||||
sendToClient(packet);
|
||||
synchronized (sendToClientAsyncQueue) {
|
||||
while ((packet = sendToClientAsyncQueue.poll()) != null) {
|
||||
sendToClient(packet);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
Thread.sleep(1);
|
||||
} catch (InterruptedException e) { //java........................................
|
||||
@ -40,9 +40,12 @@ public class HConnection {
|
||||
new Thread(() -> {
|
||||
while (true) {
|
||||
HPacket packet;
|
||||
while ((packet = sendToServerAsyncQueue.poll()) != null) {
|
||||
sendToServer(packet);
|
||||
synchronized (sendToServerAsyncQueue) {
|
||||
while ((packet = sendToServerAsyncQueue.poll()) != null) {
|
||||
sendToServer(packet);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
Thread.sleep(1);
|
||||
} catch (InterruptedException e) {
|
||||
@ -365,6 +368,10 @@ public class HConnection {
|
||||
}
|
||||
|
||||
private void setState(State state) {
|
||||
if (state == State.CONNECTED) {
|
||||
sendToClientAsyncQueue.clear();
|
||||
sendToServerAsyncQueue.clear();
|
||||
}
|
||||
if (state != this.state) {
|
||||
State buffer = this.state;
|
||||
this.state = state;
|
||||
@ -425,10 +432,15 @@ public class HConnection {
|
||||
}
|
||||
|
||||
public void sendToClientAsync(HPacket message) {
|
||||
sendToClientAsyncQueue.add(message);
|
||||
synchronized (sendToClientAsyncQueue) {
|
||||
sendToClientAsyncQueue.add(message);
|
||||
}
|
||||
|
||||
}
|
||||
public void sendToServerAsync(HPacket message) {
|
||||
sendToServerAsyncQueue.add(message);
|
||||
synchronized (sendToServerAsyncQueue) {
|
||||
sendToServerAsyncQueue.add(message);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ import java.awt.*;
|
||||
|
||||
public class SubForm {
|
||||
|
||||
GEarthController parentController;
|
||||
protected GEarthController parentController;
|
||||
|
||||
public void setParentController(GEarthController controller) {
|
||||
parentController = controller;
|
||||
|
@ -7,10 +7,12 @@ import javafx.event.ActionEvent;
|
||||
import javafx.scene.control.*;
|
||||
import javafx.scene.layout.GridPane;
|
||||
import javafx.scene.layout.VBox;
|
||||
import javafx.stage.FileChooser;
|
||||
import main.protocol.HMessage;
|
||||
import main.protocol.HPacket;
|
||||
import main.ui.SubForm;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
@ -211,8 +213,68 @@ public class Scheduler extends SubForm {
|
||||
}
|
||||
|
||||
public void saveBtnClicked(ActionEvent actionEvent) {
|
||||
FileChooser fileChooser = new FileChooser();
|
||||
|
||||
//Set extension filter
|
||||
FileChooser.ExtensionFilter extFilter =
|
||||
new FileChooser.ExtensionFilter("SCHED files (*.sched)", "*.sched");
|
||||
fileChooser.getExtensionFilters().add(extFilter);
|
||||
fileChooser.setTitle("Save Schedule File");
|
||||
|
||||
//Show save file dialog
|
||||
File file = fileChooser.showSaveDialog(parentController.getStage());
|
||||
|
||||
if(file != null){
|
||||
|
||||
try {
|
||||
FileWriter fileWriter = new FileWriter(file);
|
||||
BufferedWriter out = new BufferedWriter(fileWriter);
|
||||
|
||||
for (int i = 0; i < scheduleItemList.size(); i++) {
|
||||
out.write(scheduleItemList.get(i).stringify());
|
||||
if (i != scheduleItemList.size() - 1) out.write("\n");
|
||||
}
|
||||
|
||||
out.flush();
|
||||
out.close();
|
||||
fileWriter.close();
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void loadBtnClicked(ActionEvent actionEvent) {
|
||||
List<ScheduleItem> list = new ArrayList<>();
|
||||
|
||||
FileChooser fileChooser = new FileChooser();
|
||||
fileChooser.setTitle("Load Schedule File");
|
||||
fileChooser.getExtensionFilters().addAll(
|
||||
new FileChooser.ExtensionFilter("Schedule Files", "*.sched"));
|
||||
File selectedFile = fileChooser.showOpenDialog(parentController.getStage());
|
||||
if (selectedFile != null) {
|
||||
|
||||
FileReader fr = null;
|
||||
try {
|
||||
fr = new FileReader(selectedFile);
|
||||
BufferedReader br = new BufferedReader(fr);
|
||||
String line = null;
|
||||
|
||||
while ((line = br.readLine()) != null)
|
||||
{
|
||||
list.add(new ScheduleItem(line));
|
||||
}
|
||||
|
||||
fr.close();
|
||||
br.close();
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
load(list);
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user