100 lines
2.5 KiB
Vala
100 lines
2.5 KiB
Vala
using Gtk;
|
|
|
|
public class Connection {
|
|
public signal void response_received(uint8[] response);
|
|
|
|
private TlsClientConnection tls_client;
|
|
|
|
public Connection() throws Error {
|
|
var loopback = new InetAddress.loopback(SocketFamily.IPV6);
|
|
var host = new InetSocketAddress(loopback, 12888);
|
|
|
|
var db_type = TlsBackend.get_default().get_file_database_type();
|
|
const string ca_path = Config.CERT_DIR + "/ca.pem";
|
|
var db = Object.new(db_type, "anchors", ca_path) as TlsDatabase;
|
|
var cert =
|
|
new TlsCertificate.from_file(Config.CERT_DIR + "/client.pem");
|
|
|
|
var plain_client = new SocketClient();
|
|
var plain_connection = plain_client.connect(host);
|
|
tls_client = TlsClientConnection.new(plain_connection, host);
|
|
|
|
tls_client.set_database(db);
|
|
tls_client.set_certificate(cert);
|
|
tls_client.handshake();
|
|
}
|
|
|
|
public async void send(uint8[] message) throws Error {
|
|
yield tls_client.output_stream.write_async(message);
|
|
var response = new uint8[1024];
|
|
var len = yield tls_client.input_stream.read_async(response);
|
|
response_received(response[0:len]);
|
|
}
|
|
}
|
|
|
|
public class MainWindow : Gtk.ApplicationWindow {
|
|
private Connection connection;
|
|
private Gtk.Button send_button;
|
|
private Gtk.Label response_label;
|
|
|
|
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;
|
|
|
|
send_button = new Gtk.Button.with_label("Send");
|
|
send_button.clicked.connect(on_send_clicked);
|
|
box.append(send_button);
|
|
|
|
response_label = new Gtk.Label("");
|
|
response_label.wrap = true;
|
|
box.append(response_label);
|
|
|
|
set_child(box);
|
|
|
|
present();
|
|
}
|
|
|
|
private async void on_send_clicked() {
|
|
try {
|
|
yield connection.send("Foo".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() {
|
|
try {
|
|
var connection = new Connection();
|
|
new MainWindow(this, connection);
|
|
} catch (Error e) {
|
|
stderr.printf("Failed to initialize connection: %s\n",
|
|
e.message);
|
|
}
|
|
}
|
|
|
|
public static int main(string[] args) {
|
|
var app = new StudySystemClient();
|
|
return app.run(args);
|
|
}
|
|
}
|