study-system/client/main.vala
Camden Dixie O'Brien 83ab6f7a20 Create initial version of client
This just connects to the server and does very little else.
2025-02-22 20:09:27 +00:00

83 lines
2.0 KiB
Vala

using Gtk;
public class Connection {
private InetSocketAddress host;
public signal void response_received(uint8[] response);
public Connection() {
var loopback = new InetAddress.loopback(SocketFamily.IPV6);
host = new InetSocketAddress(loopback, 12888);
}
public async void send(uint8[] message) throws Error {
var client = new SocketClient();
var conn = yield client.connect_async(host);
yield conn.output_stream.write_async(message);
var response = new uint8[1024];
var len = yield conn.input_stream.read_async(response);
response_received(response[0:len]);
}
}
public class MainWindow : Gtk.ApplicationWindow {
private Connection connection;
private Gtk.Label response_label;
private Gtk.Button connect_button;
public MainWindow(Gtk.Application app, Connection connection) {
Object(application: app);
default_width = 600;
default_height = 400;
title = "Study System Client";
this.connection = connection;
connection.response_received.connect((response) => {
response_label.label = "Response: " + (string)response;
});
var box = new Gtk.Box(Gtk.Orientation.VERTICAL, 10);
box.margin_start = 10;
box.margin_end = 10;
box.margin_top = 10;
box.margin_bottom = 10;
connect_button = new Gtk.Button.with_label("Foo");
connect_button.clicked.connect(on_connect_clicked);
box.append(connect_button);
response_label = new Gtk.Label("");
response_label.wrap = true;
box.append(response_label);
set_child(box);
present();
}
private async void on_connect_clicked() {
try {
yield connection.send("Bar".data);
} catch (Error e) {
response_label.label = "Error: " + e.message;
}
}
}
public class StudySystemClient : Gtk.Application {
public StudySystemClient() {
Object(application_id: "sh.wip.study-system-client");
}
protected override void activate() {
var connection = new Connection();
new MainWindow(this, connection);
}
public static int main(string[] args) {
var app = new StudySystemClient();
return app.run(args);
}
}