Skip to content

Commit

Permalink
Added java torchbringer client
Browse files Browse the repository at this point in the history
  • Loading branch information
moraguma committed Jun 5, 2024
1 parent 710ae99 commit 1072d5a
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions servers/TorchbringerClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import org.example.rl.util.RLPercept;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;

public class TorchBringerClient {
private Socket clientSocket;
private PrintWriter out;
private BufferedReader in;

public TorchBringerClient(int port) {
try {
clientSocket = new Socket(InetAddress.getLocalHost(), port);
out = new PrintWriter(clientSocket.getOutputStream());
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
} catch (Exception e) {
e.printStackTrace();
}
}

public void initialize(String configPath) {
try {
String jsonData = new String(Files.readAllBytes(Paths.get(configPath)));
JSONObject request = new JSONObject();
request.put("method", "initialize");
request.put("config", new JSONObject(jsonData));

out.println(request.toString());
System.out.println(in.readLine());
} catch (Exception e) {
e.printStackTrace();
}

}

public ArrayList<Double> step(RLPercept percept) {
JSONObject request = new JSONObject();
request.put("method", "step");
request.put("state", percept.getState());
request.put("reward", percept.getReward());
request.put("terminal", percept.isTerminal());

out.println(request.toString());
try {
return ((ArrayList<ArrayList<Double>>) new JSONObject(in.readLine()).get("action")).get(0);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

0 comments on commit 1072d5a

Please sign in to comment.