/*
    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 "pichunkstream.h"
#include "picrypt.h"
#include "piethernet.h"
#include "pistreampacker.h"
#include "pitranslator.h"


const char hash_cloud_key[] = "_picloud_";


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


PICloud::TCP::TCP(PIStreamPacker * s): streampacker(s) {
	streampacker->setMaxPacketSize(63 * 1024);
}

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


void PICloud::TCP::setServerName(const PIString & server_name_) {
	server_name = server_name_;
	suuid =
		PICrypt::hash(PIByteArray(server_name_.data(), server_name_.size()), (const unsigned char *)hash_cloud_key, sizeof(hash_cloud_key));
}


PIString PICloud::TCP::serverName() const {
	return 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);
	// mutex_send.lock();
	streampacker->send(ba);
	// mutex_send.unlock();
}


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


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


int PICloud::TCP::sendData(const PIByteArray & data) {
	header.type = PICloud::TCP::Data;
	PIByteArray ba;
	ba << header;
	ba.append(data);
	// piCout << "[PICloud::TCP] sendData" << ba.toHex();
	mutex_send.lock();
	streampacker->send(ba);
	mutex_send.unlock();
	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);
	mutex_send.lock();
	streampacker->send(ba);
	mutex_send.unlock();
	return data.size_s();
}


void PICloud::TCP::sendPing() {
	header.type = PICloud::TCP::Ping;
	PIByteArray ba;
	ba << header;
	ba.append(suuid);
	mutex_send.lock();
	streampacker->send(ba);
	mutex_send.unlock();
}


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!"_tr("PICloud");
		return ret;
	}
	ret.first  = (Type)hdr.type;
	ret.second = (Role)hdr.role;
	return ret;
}


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


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


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