Skip to content

Commit e33b542

Browse files
author
Philipp Gersch
authored
Merge pull request #2 from DevKevYT/2.1.1
Add files via upload
2 parents 7e0ed18 + dd1858a commit e33b542

File tree

5 files changed

+53
-27
lines changed

5 files changed

+53
-27
lines changed

server-hook/src/Application.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class Application extends Hook {
1515
Server server;
1616

1717
public Application() {
18-
super(Version.of("2.1.0"));
18+
super(Version.of("2.1.1"));
1919
}
2020

2121
@Override

server-hook/src/Codes.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ public interface Codes {
77

88
//Codes 0-99 keine Errors
99
public static final int CODE_SUCCESS = 0;
10+
//Einkommende Datei
1011
public static final int CODE_READY = 1;
12+
//Ausgehende Datei
13+
public static final int CODE_SEND_READY = 2;
1114

1215
//Codes 100-... Error codes
1316
public static final int CODE_UNKNOWN_ERROR = 100;

server-hook/src/Commands.java

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public static final String generateErrorJSON(int code, String errorMessage) {
6969
public Command[] createLib() {
7070
return new Command[] {
7171

72-
new Command("download-file", "string", "download-file <klasseid> Stellt die Date als Stream bereit") {
72+
new Command("download-file", "string", "download-file <klasseid> Stellt die Date als Stream bereit. Wenn eine Nachricht mit code 2 vom Server kommt muss der Befehl 'ready-to-recieve' zurückgesendet werden. Dann kann die Datei empfangen werden") {
7373
@Override
7474
public Object execute(Object[] arg0, Process arg1, Block arg2) throws Exception {
7575
Connection c = (Connection) arg1.getVariable("connection", arg1.getMain());
@@ -81,18 +81,33 @@ public Object execute(Object[] arg0, Process arg1, Block arg2) throws Exception
8181
if(filepath != Variable.UNKNOWN) {
8282
File sheet = new File(filepath.getAsString());
8383
if(sheet.exists()) {
84-
try (FileInputStream fileReadStream = new FileInputStream(sheet)) {
85-
int read;
86-
byte[] bytes = new byte[8192];
87-
while ((read = fileReadStream.read(bytes)) != -1) {
88-
c.client.getOutputStream().write(bytes, 0, read);
89-
}
90-
91-
c.status = Codes.CODE_SUCCESS;
92-
c.terminateConnection();
93-
} catch (FileNotFoundException e1) {
94-
e1.printStackTrace();
95-
} catch (IOException e1) {
84+
85+
try {
86+
c.sendMessage(generateJSON(Codes.CODE_SEND_READY, "\"ready-to-send\""));
87+
} catch (IOException e) {
88+
return;
89+
}
90+
//Warte auf Client Bestätigung
91+
try {
92+
String approve = c.read();
93+
94+
if(approve.equals("ready-to-recieve")) {
95+
try (FileInputStream fileReadStream = new FileInputStream(sheet)) {
96+
int read;
97+
byte[] bytes = new byte[8192];
98+
while ((read = fileReadStream.read(bytes)) != -1) {
99+
c.client.getOutputStream().write(bytes, 0, read);
100+
}
101+
102+
c.status = Codes.CODE_SUCCESS;
103+
c.terminateConnection();
104+
} catch (FileNotFoundException e1) {
105+
e1.printStackTrace();
106+
} catch (IOException e1) {
107+
}
108+
}
109+
} catch (IOException e) {
110+
return;
96111
}
97112
} else {
98113
c.status = Codes.CODE_ENTRY_MISSING;

server-hook/src/Connection.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11

22

3+
import java.io.BufferedReader;
34
import java.io.BufferedWriter;
45
import java.io.IOException;
6+
import java.io.InputStreamReader;
57
import java.io.OutputStreamWriter;
68
import java.net.Socket;
79

@@ -15,6 +17,7 @@ public class Connection {
1517

1618
public final Socket client;
1719
public final BufferedWriter writer;
20+
public final BufferedReader reader;
1821

1922
public Thread thread;
2023

@@ -27,12 +30,25 @@ public Connection(Socket client) throws IOException {
2730
this.sessionId = SESSION_COUNTER;
2831
this.client = client;
2932
this.writer = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
33+
this.reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
3034
SESSION_COUNTER++;
3135
}
3236

37+
public String read() throws IOException {
38+
int max = 500; //Maximal 500 Zeichen
39+
StringBuilder command = new StringBuilder();
40+
while(command.length() < max) {
41+
int character = reader.read();
42+
if(character == '\n') break;
43+
command.append((char) character);
44+
}
45+
return command.toString();
46+
}
47+
3348
public void terminateConnection() throws IOException {
3449
client.close();
3550
writer.close();
51+
reader.close();
3652
}
3753

3854
public void addThread(Thread thread) {

server-hook/src/Server.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11

22

3-
import java.io.BufferedReader;
43
import java.io.IOException;
5-
import java.io.InputStreamReader;
64
import java.net.ServerSocket;
75
import java.net.Socket;
86
import java.util.ArrayList;
@@ -37,6 +35,7 @@ public Server(int port, int maxConnections, int maxConnectionTime) {
3735
}
3836

3937
private void addClientConnection(Socket client) throws IOException {
38+
4039
final Connection c = new Connection(client);
4140

4241
//System.out.println("Starting session: " + c.sessionId);
@@ -47,15 +46,9 @@ public void run() {
4746
//Maximal einen Command pro connection!
4847
try {
4948
Process commandHandler = new Process(true);
50-
BufferedReader reader = new BufferedReader(new InputStreamReader(c.client.getInputStream()));
5149

52-
int max = 6000; //Maximal 6000 Zeichen
53-
StringBuilder command = new StringBuilder();
54-
while(command.length() < max) {
55-
int character = reader.read();
56-
if(character == '\n') break;
57-
command.append((char) character);
58-
}
50+
String command = c.read();
51+
5952
//Einfach eine Zeile!
6053
if(command != null) {
6154
commandHandler.clearLibraries();
@@ -71,16 +64,15 @@ public void log(String arg0, boolean arg1) {
7164

7265
@Override
7366
public void error(String arg0) {
74-
Main.logger.logError("Error while executing command: " + command + ": " + arg0 + " (Exit Code: " + c.status + ")");
67+
Main.logger.logError("Error while executing command: " + (arg0.length() > 100 ? arg0.substring(0, 100) : arg0) + " (Exit Code: " + c.status + ")");
7568
//Send error message
7669
try {
7770
c.writer.write("{\"error\": \"" + arg0 + "\",\"code\": " + c.status + "}");
7871
c.writer.flush();
7972
} catch (IOException e) {}
8073
}
8174
});
82-
83-
commandHandler.execute(command.toString(), false);
75+
commandHandler.execute(command, false);
8476
}
8577
} catch (IOException e) {
8678
c.status = Codes.CODE_UNKNOWN_ERROR;

0 commit comments

Comments
 (0)