mirror of
https://github.com/sirjonasxx/G-Earth.git
synced 2025-01-19 00:26:27 +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.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.List;
|
|
||||||
import java.util.PriorityQueue;
|
|
||||||
import java.util.Queue;
|
|
||||||
|
|
||||||
public class HConnection {
|
public class HConnection {
|
||||||
|
|
||||||
|
|
||||||
private volatile Queue<HPacket> sendToClientAsyncQueue = new PriorityQueue<>();
|
private final Queue<HPacket> sendToClientAsyncQueue = new LinkedList<>();
|
||||||
private volatile Queue<HPacket> sendToServerAsyncQueue = new PriorityQueue<>();
|
private final Queue<HPacket> sendToServerAsyncQueue = new LinkedList<>();
|
||||||
public HConnection() {
|
public HConnection() {
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
while (true) {
|
while (true) {
|
||||||
HPacket packet;
|
HPacket packet;
|
||||||
|
synchronized (sendToClientAsyncQueue) {
|
||||||
while ((packet = sendToClientAsyncQueue.poll()) != null) {
|
while ((packet = sendToClientAsyncQueue.poll()) != null) {
|
||||||
sendToClient(packet);
|
sendToClient(packet);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Thread.sleep(1);
|
Thread.sleep(1);
|
||||||
} catch (InterruptedException e) { //java........................................
|
} catch (InterruptedException e) { //java........................................
|
||||||
@ -40,9 +40,12 @@ public class HConnection {
|
|||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
while (true) {
|
while (true) {
|
||||||
HPacket packet;
|
HPacket packet;
|
||||||
|
synchronized (sendToServerAsyncQueue) {
|
||||||
while ((packet = sendToServerAsyncQueue.poll()) != null) {
|
while ((packet = sendToServerAsyncQueue.poll()) != null) {
|
||||||
sendToServer(packet);
|
sendToServer(packet);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Thread.sleep(1);
|
Thread.sleep(1);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
@ -365,6 +368,10 @@ public class HConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void setState(State state) {
|
private void setState(State state) {
|
||||||
|
if (state == State.CONNECTED) {
|
||||||
|
sendToClientAsyncQueue.clear();
|
||||||
|
sendToServerAsyncQueue.clear();
|
||||||
|
}
|
||||||
if (state != this.state) {
|
if (state != this.state) {
|
||||||
State buffer = this.state;
|
State buffer = this.state;
|
||||||
this.state = state;
|
this.state = state;
|
||||||
@ -425,10 +432,15 @@ public class HConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void sendToClientAsync(HPacket message) {
|
public void sendToClientAsync(HPacket message) {
|
||||||
|
synchronized (sendToClientAsyncQueue) {
|
||||||
sendToClientAsyncQueue.add(message);
|
sendToClientAsyncQueue.add(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
public void sendToServerAsync(HPacket message) {
|
public void sendToServerAsync(HPacket message) {
|
||||||
|
synchronized (sendToServerAsyncQueue) {
|
||||||
sendToServerAsyncQueue.add(message);
|
sendToServerAsyncQueue.add(message);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ import java.awt.*;
|
|||||||
|
|
||||||
public class SubForm {
|
public class SubForm {
|
||||||
|
|
||||||
GEarthController parentController;
|
protected GEarthController parentController;
|
||||||
|
|
||||||
public void setParentController(GEarthController controller) {
|
public void setParentController(GEarthController controller) {
|
||||||
parentController = controller;
|
parentController = controller;
|
||||||
|
@ -7,10 +7,12 @@ import javafx.event.ActionEvent;
|
|||||||
import javafx.scene.control.*;
|
import javafx.scene.control.*;
|
||||||
import javafx.scene.layout.GridPane;
|
import javafx.scene.layout.GridPane;
|
||||||
import javafx.scene.layout.VBox;
|
import javafx.scene.layout.VBox;
|
||||||
|
import javafx.stage.FileChooser;
|
||||||
import main.protocol.HMessage;
|
import main.protocol.HMessage;
|
||||||
import main.protocol.HPacket;
|
import main.protocol.HPacket;
|
||||||
import main.ui.SubForm;
|
import main.ui.SubForm;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -211,8 +213,68 @@ public class Scheduler extends SubForm {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void saveBtnClicked(ActionEvent actionEvent) {
|
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) {
|
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