Commit fe8e2dd4 authored by Jonathan Michalon's avatar Jonathan Michalon
Browse files

Variant handling now true abstract class

Modified variantmessage to be real abstract method which should be
inherited by all messages transmitting one or more fields. Used it
in loginmessage, and kept old behaviour directly in signalmessage.
parent 13070150
......@@ -21,7 +21,7 @@
/**
* Login message with login, password and current status
*/
public class OVCC.Network.LoginMessage : StringMessage
public class OVCC.Network.LoginMessage : VariantMessage
{
public override MessageType message_type { get { return MessageType.LOGIN; } }
......@@ -44,28 +44,19 @@ public class OVCC.Network.LoginMessage : StringMessage
{
Object (login: login, password: password);
}
public override bool serialize (DataOutputStream stream,
Cancellable? cancel = null)
throws Error
protected override void build_variant ()
{
/* FIXME separator */
message = login + "|" + password + "|" + ((int)status).to_string();
return base.serialize(stream, cancel);
variant = new Variant ("(iss)", status, login, password);
}
public override bool deserialize (DataInputStream stream,
Cancellable? cancel = null)
throws Error
protected override void parse_variant ()
{
base.deserialize (stream, cancel);
string[] fields = message.split ("|");
if (fields == null || fields.length != 3) {
throw new MessageError.MALFORMED_MESSAGE ("Message should have two fields");
}
login = fields[0];
password = fields[1];
status = (State)int.parse(fields[2]);
return true;
State s;
string l;
string p;
parse ("(iss)", out s, out l, out p);
status = s;
login = l;
password = p;
}
}
......@@ -141,16 +141,21 @@ namespace OVCC.Network
}
}
public class SignalMessage: VariantMessage
public class SignalMessage: Message
{
public override MessageType message_type { get { return MessageType.SIGNAL; } }
public SignalType stype { get; set; default = 0; }
protected uint8[] data = null;
protected Variant variant = null;
public SignalMessage (SignalType s, Variant v)
{
base (v);
stype = s;
data = new uint8[v.get_size()];
v.store (data);
variant = v;
}
public override bool serialize (DataOutputStream stream,
......@@ -158,7 +163,7 @@ namespace OVCC.Network
throws Error
{
stream.put_uint16 ((uint16)stype, cancel);
return base.serialize (stream, cancel);
return write_buffer (stream, data, cancel);
}
public override bool deserialize (DataInputStream stream,
......@@ -166,8 +171,26 @@ namespace OVCC.Network
throws Error
{
stype = (SignalType)stream.read_uint16 (cancel);
data = read_buffer (stream, cancel);
return true;
}
public Variant get_variant (string format)
{
if (variant == null) {
variant = Variant.new_from_data<Object> (new VariantType(format), data, false, this);
}
return variant;
}
public void parse (string format,
...)
{
var ap = va_list ();
var v = get_variant (format);
return base.deserialize (stream, cancel);
v.get_va (format, null, &ap);
}
}
}
......@@ -23,20 +23,21 @@
*/
public abstract class OVCC.Network.VariantMessage : Message
{
protected uint8[] data = null;
protected Variant variant = null;
public VariantMessage (Variant v)
{
data = new uint8[v.get_size()];
v.store (data);
variant = v;
}
private uint8[] data = null;
protected abstract void build_variant ();
protected abstract void parse_variant ();
public override bool serialize (DataOutputStream stream,
Cancellable? cancel = null)
throws Error
{
build_variant ();
var data = new uint8[variant.get_size()];
variant.store (data);
return write_buffer (stream, data, cancel);
}
......@@ -46,24 +47,20 @@ public abstract class OVCC.Network.VariantMessage : Message
{
data = read_buffer (stream, cancel);
return true;
}
public Variant get_variant (string format)
{
if (variant == null) {
variant = Variant.new_from_data<Object> (new VariantType(format), data, false, this);
}
parse_variant ();
return variant;
return true;
}
public void parse (string format,
...)
protected void parse (string format,
...)
{
var ap = va_list ();
var v = get_variant (format);
variant = Variant.new_from_data<Object> (new VariantType(format), data, false, this);
variant.get_va (format, null, &ap);
v.get_va (format, null, &ap);
variant = null;
data = null;
}
}
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