diff --git a/client/main.vala b/client/main.vala new file mode 100644 index 0000000..f09088b --- /dev/null +++ b/client/main.vala @@ -0,0 +1,82 @@ +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); + } +} diff --git a/client/meson.build b/client/meson.build new file mode 100644 index 0000000..8aa5328 --- /dev/null +++ b/client/meson.build @@ -0,0 +1,18 @@ +project( + 'study-system-client', + 'vala', + version : '0.1.0', +) + +# Vala often generates C code that produces warnings, so ignore these +# to avoid a noisy build output. +add_project_arguments('-w', language: 'c') + +gtk_dep = dependency('gtk4') + +exe = executable( + 'study-system-client', + 'main.vala', + dependencies: [gtk_dep], + c_args: ['-w'] +)