/*
	PIP - Platform Independent Primitives
	PICloud TCP transport
	Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "picloudtcp.h"
#include "picrypt.h"
#include "pichunkstream.h"
#include "piethernet.h"
#include "pistreampacker.h"


const char hash_def_key[] = "_picrypt_";


PICloud::TCP::Header::Header() {
	version = Version_2;
}


PICloud::TCP::TCP(PIStreamPacker * s) : streampacker(s) {

}

void PICloud::TCP::setRole(PICloud::TCP::Role r) {
	header.role = r;
}


void PICloud::TCP::setServerName(const PIString & server_name) {
	suuid = PICrypt::hash(server_name);
}


void PICloud::TCP::sendStart() {
	//piCout << "sendStart";
	if (suuid.size() != PICrypt::sizeHash()) {
		piCout << "PICloud ERROR, server not set, invoke setServerName first";
		return;
	}
	header.type = PICloud::TCP::Connect;
	PIByteArray ba;
	ba << header;
	ba.append(suuid);
	streampacker->send(ba);
}


void PICloud::TCP::sendConnected(uint client_id) {
	header.type = PICloud::TCP::Connect;
	PIByteArray ba;
	ba << header << client_id;
	streampacker->send(ba);
}


void PICloud::TCP::sendDisconnected(uint client_id) {
	header.type = PICloud::TCP::Disconnect;
	PIByteArray ba;
	ba << header << client_id;
	streampacker->send(ba);
}


int PICloud::TCP::sendData(const PIByteArray & data) {
	header.type = PICloud::TCP::Data;
	PIByteArray ba;
	ba << header;
	ba.append(data);
//	piCout << "sendData" << ba.toHex();
	streampacker->send(ba);
	return data.size_s();
}


int PICloud::TCP::sendData(const PIByteArray & data, uint client_id) {
	header.type = PICloud::TCP::Data;
	PIByteArray ba;
	ba << header << client_id;
	ba.append(data);
	streampacker->send(ba);
	return data.size_s();
}


PIPair<PICloud::TCP::Type, PICloud::TCP::Role> PICloud::TCP::parseHeader(PIByteArray & ba) {
	PIPair<PICloud::TCP::Type, PICloud::TCP::Role> ret;
	ret.first = InvalidType;
	ret.second = InvalidRole;
	if (ba.size() < sizeof(Header)) return ret;
	PICloud::TCP::Header hdr;
	ba >> hdr;
	if (hdr.version != header.version) {
		piCout << "[PICloud]" << "invalid PICloud::TCP version!";
		return ret;
	}
	ret.first = (Type)hdr.type;
	ret.second = (Role)hdr.role;
	return ret;
}


PIByteArray PICloud::TCP::parseData(PIByteArray & ba) {
	if (header.role == Client) {
		return ba;
	}
	return PIByteArray();
}


PIPair<uint, PIByteArray> PICloud::TCP::parseDataServer(PIByteArray & ba) {
	PIPair<uint, PIByteArray> ret;
	ret.first = 0;
	if (header.role == Server) {
		ba >> ret.first;
		ret.second = ba;
	}
	return ret;
}


PIByteArray PICloud::TCP::parseConnect_d(PIByteArray & ba) {
	if (ba.size() != PICrypt::sizeHash()) {
		piCout << "PICloud ERROR, invalid server uuid";
		return PIByteArray();
	} else {
		return ba;
	}
}


uint PICloud::TCP::parseConnect(PIByteArray & ba) {
	uint ret;
	ba >> ret;
	return ret;
}


uint PICloud::TCP::parseDisconnect(PIByteArray & ba) {
	uint ret;
	ba >> ret;
	return ret;
}
