
var coder = require("./coder");

var tool = require("./tool");
var util = require("util");


coder.RegistMessage(coder.TAG.ADCLOUD_MESSAGE_TAG_PROXY_GET_MASTER,MProxyGetMaster);
coder.RegistMessage(coder.TAG.ADCLOUD_MESSAGE_TAG_PROXY_GET_MASTER_BACK,MProxyGetMasterBack);
coder.RegistMessage(coder.TAG.ADCLOUD_MESSAGE_TAG_PROXY_GET_GATE,MProxyGetGate);
coder.RegistMessage(coder.TAG.ADCLOUD_MESSAGE_TAG_PROXY_GET_GATE_BACK,MProxyGetGateBack);

exports.MProxyGetMaster = MProxyGetMaster;
exports.MProxyGetMasterBack = MProxyGetMasterBack;
exports.MProxyGetGate = MProxyGetGate;
exports.MProxyGetGateBack = MProxyGetGateBack;


function MProxyGetMaster(id,version,num) {
	coder.Message.call(this,coder.TAG.ADCLOUD_MESSAGE_TAG_PROXY_GET_MASTER);
	this.id = id;
	this.num = num;
	this.version = version;
}
util.inherits(MProxyGetMaster, coder.Message);

MProxyGetMaster.prototype.BodyLength = function(){
	var size = Buffer.byteLength(this.id)  + 1 + 4 + Buffer.byteLength(this.version)  + 1;
	return size;
}

MProxyGetMaster.prototype.EncodeBody= function(buf_writer){
	buf_writer.WriteStringAscii(this.id);
	buf_writer.WriteUInt32(this.num);
	buf_writer.WriteStringAscii(this.version);
}

MProxyGetMaster.prototype.DecodeBody = function(buf_reader){
	this.id = buf_reader.ReadStringAscii();
	this.num = buf_reader.ReadUInt32();
	this.version = buf_reader.ReadStringAscii();
}



function MProxyGetMasterBack() {
	coder.Message.call(this,coder.TAG.ADCLOUD_MESSAGE_TAG_PROXY_GET_MASTER_BACK);
	this.code = 0;
	this.addr = "";
	this.inaddr = "";
	this.err = "";
}
util.inherits(MProxyGetMasterBack, coder.Message);

MProxyGetMasterBack.prototype.BodyLength = function(){
	var size = Buffer.byteLength(this.addr)  + 1 + 1 + Buffer.byteLength(this.inaddr)+1 + Buffer.byteLength(this.err)+1 ;
	return size;
}

MProxyGetMasterBack.prototype.EncodeBody= function(buf_writer){
	buf_writer.WriteUInt8(this.code);
	buf_writer.WriteStringAscii(this.addr);
	buf_writer.WriteStringAscii(this.inaddr);
	buf_writer.WriteStringAscii(this.err);
}

MProxyGetMasterBack.prototype.DecodeBody = function(buf_reader){
	this.code = buf_reader.ReadUInt8();
	this.addr = buf_reader.ReadStringAscii();
	this.inaddr = buf_reader.ReadStringAscii();
	this.err = buf_reader.ReadStringAscii();
}


function MProxyGetGate(id) {
	coder.Message.call(this,coder.TAG.ADCLOUD_MESSAGE_TAG_PROXY_GET_GATE);
	this.id = id;
}
util.inherits(MProxyGetGate, coder.Message);

MProxyGetGate.prototype.BodyLength = function(){
	var size = Buffer.byteLength(this.id)  + 1;
	return size;
}

MProxyGetGate.prototype.EncodeBody= function(buf_writer){
	buf_writer.WriteStringAscii(this.id);
}

MProxyGetGate.prototype.DecodeBody = function(buf_reader){
	this.id = buf_reader.ReadStringAscii();
}


function MProxyGetGateBack() {
	coder.Message.call(this,coder.TAG.ADCLOUD_MESSAGE_TAG_PROXY_GET_GATE_BACK);
	this.code = 0;
	this.addrs = [];
	this.err = "";
}
util.inherits(MProxyGetGateBack, coder.Message);

MProxyGetGateBack.prototype.BodyLength = function(){
	var size = 1;
	for(var i in this.addrs){
		size += Buffer.byteLength(this.id)  + 1;
	}
	size += Buffer.byteLength(this.err)  + 1;
	return size;
}

MProxyGetGateBack.prototype.EncodeBody= function(buf_writer){
	buf_writer.WriteUInt8(this.code);
	for(var i in this.addrs){
		buf_writer.WriteStringAscii(this.addrs[i]);
	}
	buf_writer.WriteStringAscii(this.err);
}

MProxyGetGateBack.prototype.DecodeBody = function(buf_reader){
	this.code = buf_reader.ReadUInt8();
	while(buf_reader.DataSize() > 0){
		this.addrs.push( buf_reader.ReadStringAscii());
	}
	this.err = this.addrs[this.addrs.length-1];
	this.addrs.splice(this.addrs.length-1);
}