Commit 71bb1fa0 authored by Jonathan Michalon's avatar Jonathan Michalon
Browse files

Enable server connection from bot, handled by libovccclient

parent ef91a390
......@@ -21,6 +21,7 @@ public class Bot
player = new OVCC.Player ("""Clever bot %3u""".printf (player_suffix));
/* connect to the server, choosing an available nickname */
debug ("Trying connection...");
while (true) {
try {
client = new OVCCClient.Client (player);
......@@ -30,13 +31,17 @@ public class Bot
} catch (OVCCClient.ServerError.MISSING_AUTHENTICATION e) {
}
}
debug ("Connected");
/* join any open table */
game = server.join_table (-1);
// if (server.open_tables.length > 0) {
// player.join (server.open_tables[0]); /* may throw a rejection */
// } else {
// throw new OVCCClient.ServerError.NO_TABLE_OPEN ("failed to join a game");
// }
debug ("Trying to join...");
try {
game = server.join_table (-1);
} catch (OVCCClient.ServerError.NO_OPEN_TABLE e) {
warning ("Cannot join an open table: %s", e.message);
server.disconnect_from ();
return false;
}
debug ("Joined");
/* react on game state changes */
game.notify["state"].connect ((s, p) => {
......@@ -103,11 +108,13 @@ public int main (string[] args)
{
var loop = new MainLoop ();
var bot = new Bot (loop);
var server = new OVCCClient.Server ("localhost", 9007);
var server = new OVCCClient.Server ("localhost", 0xdead);
try {
bot.join (server);
loop.run ();
if (bot.join (server))
{
loop.run ();
}
} catch (Error e) {
warning ("Failed to join on the server: %s", e.message);
}
......
using OVCC;
using OVCC.Network;
namespace OVCCClient
{
......@@ -23,10 +24,12 @@ namespace OVCCClient
public class Server : Object
{
private string host;
private uint16 port;
private SocketClient socket;
private SocketConnection connection;
private string host;
private uint16 port;
private SocketClient socket;
private SocketConnection connection = null;
private DataInputStream input;
private DataOutputStream output;
public Server (string host,
uint16 port)
......@@ -43,17 +46,53 @@ namespace OVCCClient
public /*async*/ bool connect_to (Player player)
throws ServerError
{
bool ok = true;
try {
SocketConnectable addr = NetworkAddress.parse (host, port);
connection = socket.connect (addr);
} catch {
ok = false;
} /* ... */
return ok;
input = new DataInputStream (connection.input_stream);
output = new DataOutputStream (connection.output_stream);
} catch (Error e) {
warning ("Trying to connect: %s", e.message);
throw new ServerError.FAILED ("Cannot connect to server");
}
/* ... */
return true;
}
public void disconnect_from ()
throws ServerError
requires (connection != null)
{
try {
send_message (new DisconnectMessage ());
connection.socket.close();
} catch {
/* FIXME ? */
}
connection = null;
input = null;
output = null;
}
public bool is_connected ()
{
return connection != null;
}
public void send_message (Message msg)
throws ServerError
requires (connection != null)
{
try {
msg.send (output);
} catch (Error e) {
warning ("Trying to send message %s: %s", msg.get_type().name(), e.message);
throw new ServerError.COMMUNICATION_FAILED ("Cannot send message");
}
}
public List<OVCC.Game>? enumerate_tables (TablesFilter filter)
requires (connection != null)
{
return null;
}
......@@ -62,6 +101,7 @@ namespace OVCCClient
* The index is typically within the list returned by enumerate_tables() */
public Game join_table (int index)
throws ServerError
requires (connection != null)
{
throw new ServerError.NO_OPEN_TABLE ("Not implemented");
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment