-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
185 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 45 additions & 21 deletions
66
src/main/java/fr/litarvan/sakado/server/data/DataServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,74 @@ | ||
package fr.litarvan.sakado.server.data; | ||
|
||
import fr.litarvan.sakado.server.data.network.NetworkClient; | ||
import com.google.gson.Gson; | ||
import fr.litarvan.sakado.server.data.network.FetchRequest; | ||
import fr.litarvan.sakado.server.data.network.FetchResponse; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.DataOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
public class DataServer | ||
{ | ||
private String name; | ||
private String host; | ||
private int port; | ||
private static final Gson gson = new Gson(); | ||
|
||
private transient NetworkClient client; | ||
private String name; | ||
private String url; | ||
|
||
public DataServer() | ||
{ | ||
} | ||
|
||
public DataServer(String name, String host, int port) | ||
public DataServer(String name, String url) | ||
{ | ||
this.name = name; | ||
this.host = host; | ||
this.port = port; | ||
this.url = url; | ||
} | ||
|
||
public void init() throws IOException | ||
public FetchResponse fetch(FetchRequest request) throws IOException | ||
{ | ||
this.client = new NetworkClient(host, port); | ||
} | ||
URL url = new URL(this.url); | ||
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | ||
|
||
public NetworkClient getClient() | ||
{ | ||
return this.client; | ||
connection.setDoInput(true); | ||
connection.setDoOutput(true); | ||
|
||
connection.setRequestMethod("POST"); | ||
connection.setRequestProperty("Content-Type", "application/json"); | ||
|
||
try (DataOutputStream out = new DataOutputStream(connection.getOutputStream())) | ||
{ | ||
out.write(gson.toJson(request).getBytes(StandardCharsets.UTF_8)); | ||
} | ||
|
||
StringBuilder content = new StringBuilder(); | ||
|
||
try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) | ||
{ | ||
String line; | ||
|
||
while ((line = in.readLine()) != null) | ||
{ | ||
content.append(line).append(System.lineSeparator()); | ||
} | ||
} | ||
|
||
connection.disconnect(); | ||
|
||
return gson.fromJson(content.toString(), FetchResponse.class); | ||
} | ||
|
||
public String getName() | ||
{ | ||
return name; | ||
} | ||
|
||
public String getHost() | ||
{ | ||
return host; | ||
} | ||
|
||
public int getPort() | ||
public String getUrl() | ||
{ | ||
return port; | ||
return url; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.