maindialog.cpp 24.9 KB
Newer Older
1 2 3 4 5
#include "maindialog.h"
#include "ui_maindialog.h"
#include "../ZoomPipeline_FuncSvr/smartlink/st_message.h"
#include "../ZoomPipeline_FuncSvr/smartlink/st_msg_applayer.h"
#include <QSettings>
6
#include <time.h>
7
#include <QMessageBox>
丁劲犇's avatar
丁劲犇 已提交
8
#include <string.h>
丁劲犇's avatar
丁劲犇 已提交
9
#include <QVector>
10 11
using namespace SmartLink;
MainDialog::MainDialog(QWidget *parent) :
丁劲犇's avatar
丁劲犇 已提交
12 13
	QDialog(parent),
	ui(new Ui::MainDialog)
14
{
丁劲犇's avatar
丁劲犇 已提交
15 16
	ui->setupUi(this);
	m_bLogedIn = false;
17
	m_bBox = false;
丁劲犇's avatar
丁劲犇 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
	m_bUUIDGot = false;
	nTimer = startTimer(100);
	ui->listView_msg->setModel(&model);
	client = new QGHTcpClient (this);
	connect(client, SIGNAL(readyRead()),this, SLOT(new_data_recieved()));
	connect(client, SIGNAL(connected()),this, SLOT(on_client_connected()));
	connect(client, SIGNAL(disconnected()),this,SLOT(on_client_disconnected()));
	connect(client, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError)));
	connect(client, SIGNAL(bytesWritten(qint64)), this, SLOT(on_client_trasferred(qint64)));

	m_currentReadOffset = 0;
	m_currentMessageSize = 0;


	QSettings settings("goldenhawking club","FunctionalClientTest",this);
	ui->lineEdit_ip->setText(settings.value("settings/ip","localhost").toString());
	ui->lineEdit_Port->setText(settings.value("settings/port","23456").toString());
	ui->plainTextEdit_boxSerialNum->setPlainText(settings.value("settings/box2svr_insid","Temporary Equip_id for test only, by goldenhawking@163.com.64Bts").toString());
	ui->lineEdit_username->setText(settings.value("settings/client2svr_username","debug").toString());
	ui->lineEdit_password->setText(settings.value("settings/client2svr_password","debug").toString());
	ui->plainTextEdit_box_userids->setPlainText(settings.value("settings/box2svr_uploadid","0,").toString());
39 40 41 42
}

MainDialog::~MainDialog()
{
丁劲犇's avatar
丁劲犇 已提交
43
	delete ui;
44 45 46
}
void MainDialog::on_client_trasferred(qint64 dtw)
{
丁劲犇's avatar
丁劲犇 已提交
47 48 49 50 51
	QGHTcpClient * pSock = qobject_cast<QGHTcpClient*>(sender());
	if (pSock)
	{
		displayMessage(QString("client %1 Transferrd %2 bytes.").arg((quintptr)pSock).arg(dtw));
	}
52 53 54 55

}
void MainDialog::on_client_connected()
{
丁劲犇's avatar
丁劲犇 已提交
56 57 58 59 60 61
	QGHTcpClient * pSock = qobject_cast<QGHTcpClient*>(sender());
	if (pSock)
	{
		displayMessage(QString("client %1 connected.").arg((quintptr)pSock));
		ui->pushButton_connect->setEnabled(false);
	}
62 63 64 65

}
void MainDialog::on_client_disconnected()
{
丁劲犇's avatar
丁劲犇 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
	m_bLogedIn = m_bUUIDGot = false;
	client = new QGHTcpClient (this);
	connect(client, SIGNAL(readyRead()),this, SLOT(new_data_recieved()));
	connect(client, SIGNAL(connected()),this, SLOT(on_client_connected()));
	connect(client, SIGNAL(disconnected()),this,SLOT(on_client_disconnected()));
	connect(client, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError)));
	connect(client, SIGNAL(bytesWritten(qint64)), this, SLOT(on_client_trasferred(qint64)));

	QGHTcpClient * pSock = qobject_cast<QGHTcpClient*>(sender());
	if (pSock)
	{
		displayMessage(QString("client %1 disconnected.").arg((quintptr)pSock));
		ui->pushButton_connect->setEnabled(true);
		pSock->abort();
		pSock->deleteLater();
	}
82 83 84
}
void MainDialog::displayError(QAbstractSocket::SocketError /*err*/)
{
丁劲犇's avatar
丁劲犇 已提交
85 86 87
	QGHTcpClient * sock = qobject_cast<QGHTcpClient *> (sender());
	if (sock)
		displayMessage(QString("client %1 error msg:").arg((quintptr)sock)+sock->errorString());
88 89 90
}
void MainDialog::new_data_recieved()
{
丁劲犇's avatar
丁劲犇 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
	QTcpSocket * pSock = qobject_cast<QTcpSocket*>(sender());
	if (pSock)
	{
		QByteArray block =pSock->readAll();
		if (block.isEmpty()==false && block.size()>0)
		{

			do
			{
				m_currentReadOffset = filter_message(block,m_currentReadOffset);
			}
			while (m_currentReadOffset < block.size());
			m_currentReadOffset = 0;
		}
	}
106 107 108
}
void MainDialog::displayMessage(const QString &str)
{
丁劲犇's avatar
丁劲犇 已提交
109 110 111
	model.insertRow(0,new QStandardItem(str));
	while (model.rowCount()>=256)
		model.removeRow(model.rowCount()-1);
112
}
丁劲犇's avatar
丁劲犇 已提交
113 114
void MainDialog::saveIni()
{
丁劲犇's avatar
丁劲犇 已提交
115 116 117 118 119 120 121
	QSettings settings("goldenhawking club","FunctionalClientTest",this);
	settings.setValue("settings/ip", ui->lineEdit_ip->text());
	settings.setValue("settings/port", ui->lineEdit_Port->text());
	settings.setValue("settings/box2svr_insid", ui->plainTextEdit_boxSerialNum->toPlainText());
	settings.setValue("settings/client2svr_username", ui->lineEdit_username->text());
	settings.setValue("settings/client2svr_password", ui->lineEdit_password->text());
	settings.setValue("settings/box2svr_uploadid", ui->plainTextEdit_box_userids->toPlainText());
丁劲犇's avatar
丁劲犇 已提交
122 123
}

124 125
void MainDialog::timerEvent(QTimerEvent * evt)
{
丁劲犇's avatar
丁劲犇 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
	static int nCount = 0;
	if (evt->timerId()==nTimer)
	{
		nCount++;
		if (nCount % 200 == 0 && client->isOpen()==true)
		{
			//send heart-beating
			QByteArray array(sizeof(SMARTLINK_HEARTBEATING),0);
			char * ptr = array.data();
			SMARTLINK_HEARTBEATING * pMsg = (SMARTLINK_HEARTBEATING *)ptr;
			pMsg->Mark = 0xBEBE;
			pMsg->tmStamp = time(0)&0x00ffff;
			//3/10 possibility to send a data block to server
			client->SendData(array);

		}
	}
143 144 145
}
void MainDialog::on_pushButton_connect_clicked()
{
丁劲犇's avatar
丁劲犇 已提交
146 147
	saveIni();
	client->connectToHost(ui->lineEdit_ip->text(),ui->lineEdit_Port->text().toUShort());
148
}
149 150
void MainDialog::on_pushButton_regisit_clicked()
{
丁劲犇's avatar
丁劲犇 已提交
151 152 153 154 155 156
	if (client->isOpen()==false)
	{
		QMessageBox::warning(this,"Please connect first!","Connect to server and test funcs");
		return;
	}
	saveIni();
157 158 159 160 161 162

	QString strSerial = ui->plainTextEdit_boxSerialNum->toPlainText();
	std::string strStdSerial = strSerial.toStdString();
	const char * pSrc = strStdSerial.c_str();
	int nMaxLen = strStdSerial.length();

丁劲犇's avatar
丁劲犇 已提交
163
	quint16 nMsgLen = sizeof(SMARTLINK_MSG_APP::tag_app_layer_header)
164
			+sizeof(stMsg_HostRegistReq) + nMaxLen /*Length Include \0*/;
丁劲犇's avatar
丁劲犇 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
	QByteArray array(sizeof(SMARTLINK_MSG) + nMsgLen - 1,0);
	char * ptr = array.data();
	SMARTLINK_MSG * pMsg = (SMARTLINK_MSG *)ptr;
	SMARTLINK_MSG_APP * pApp = (SMARTLINK_MSG_APP *)(((unsigned char *)
													  (ptr))+sizeof(SMARTLINK_MSG)-1
													 );
	pMsg->Mark = 0x55AA;
	pMsg->version = 1;
	pMsg->SerialNum = 0;
	pMsg->Priority = 1;
	pMsg->Reserved1 = 0;
	pMsg->source_id = (quint32)((quint64)(0xffffffff) & 0xffffffff );

	pMsg->destin_id = (quint32)((quint64)(0x00000001) & 0xffffffff );;

	pMsg->data_length = nMsgLen;
	pMsg->Reserved2 = 0;


	pApp->header.AskID = 0x01;
	pApp->header.MsgType = 0x1000;
	pApp->header.MsgFmtVersion = 0x01;
187 188

	for (int i=0;i<=nMaxLen;i++)
丁劲犇's avatar
丁劲犇 已提交
189 190 191 192
		pApp->MsgUnion.msg_HostRegistReq.HostSerialNum[i] =
				i<nMaxLen?pSrc[i]:0;
	//3/10 possibility to send a data block to server
	client->SendData(array);
193
}
194 195 196

void MainDialog::on_pushButton_Login_clicked()
{
丁劲犇's avatar
丁劲犇 已提交
197 198 199 200 201 202
	if (client->isOpen()==false)
	{
		QMessageBox::warning(this,"Please connect first!","Connect to server and test funcs");
		return;
	}
	saveIni();
203 204 205 206 207 208 209

	QString strSerial = ui->plainTextEdit_boxSerialNum->toPlainText();
	std::string strStdSerial = strSerial.toStdString();
	const char * pSrc = strStdSerial.c_str();
	int nMaxLen = strStdSerial.length();


丁劲犇's avatar
丁劲犇 已提交
210
	quint16 nMsgLen = sizeof(SMARTLINK_MSG_APP::tag_app_layer_header)
211
			+sizeof(stMsg_HostLogonReq) + nMaxLen /*\0 included*/;
丁劲犇's avatar
丁劲犇 已提交
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
	QByteArray array(sizeof(SMARTLINK_MSG) + nMsgLen - 1,0);
	char * ptr = array.data();
	SMARTLINK_MSG * pMsg = (SMARTLINK_MSG *)ptr;
	SMARTLINK_MSG_APP * pApp = (SMARTLINK_MSG_APP *)(((unsigned char *)
													  (ptr))+sizeof(SMARTLINK_MSG)-1
													 );
	pMsg->Mark = 0x55AA;
	pMsg->version = 1;
	pMsg->SerialNum = 0;
	pMsg->Priority = 1;
	pMsg->Reserved1 = 0;
	pMsg->source_id = (quint32)((quint64)(0xffffffff) & 0xffffffff );

	pMsg->destin_id = (quint32)((quint64)(0x00000001) & 0xffffffff );;

	pMsg->data_length = nMsgLen;
	pMsg->Reserved2 = 0;


	pApp->header.AskID = 0x01;
	pApp->header.MsgType = 0x1001;
	pApp->header.MsgFmtVersion = 0x01;
234 235

	for (int i=0;i<=nMaxLen;i++)
丁劲犇's avatar
丁劲犇 已提交
236 237 238 239 240 241
		pApp->MsgUnion.msg_HostLogonReq.HostSerialNum[i] =
				i<nMaxLen?pSrc[i]:0;
	pApp->MsgUnion.msg_HostLogonReq.ID = ui->lineEdit_boxid->text().toUInt();

	//3/10 possibility to send a data block to server
	client->SendData(array);
242
}
丁劲犇's avatar
丁劲犇 已提交
243 244
void MainDialog::on_pushButton_clientLogin_clicked()
{
丁劲犇's avatar
丁劲犇 已提交
245 246 247 248 249 250
	if (client->isOpen()==false)
	{
		QMessageBox::warning(this,"Please connect first!","Connect to server and test funcs");
		return;
	}
	saveIni();
251 252 253 254 255 256 257 258 259
	QString strUserName = ui->lineEdit_username->text();
	QString strPassWd = ui->lineEdit_password->text();
	std::string strStdUserName = strUserName.toStdString();
	const char * pSrcUsername = strStdUserName.c_str();
	int nMaxLenUserName = strStdUserName.length();
	std::string strStdPassword = strPassWd.toStdString();
	const char * pSrcPassword = strStdPassword.c_str();
	int nMaxLenPassword = strStdPassword.length();

丁劲犇's avatar
丁劲犇 已提交
260
	quint16 nMsgLen = sizeof(SMARTLINK_MSG_APP::tag_app_layer_header)
261
			+sizeof(stMsg_ClientLoginReq)+nMaxLenUserName+nMaxLenPassword+1;
丁劲犇's avatar
丁劲犇 已提交
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
	QByteArray array(sizeof(SMARTLINK_MSG) + nMsgLen - 1,0);
	char * ptr = array.data();
	SMARTLINK_MSG * pMsg = (SMARTLINK_MSG *)ptr;
	SMARTLINK_MSG_APP * pApp = (SMARTLINK_MSG_APP *)(((unsigned char *)
													  (ptr))+sizeof(SMARTLINK_MSG)-1
													 );
	pMsg->Mark = 0x55AA;
	pMsg->version = 1;
	pMsg->SerialNum = 0;
	pMsg->Priority = 1;
	pMsg->Reserved1 = 0;
	pMsg->source_id = (quint32)((quint64)(0xffffffff) & 0xffffffff );

	pMsg->destin_id = (quint32)((quint64)(0x00000001) & 0xffffffff );;

	pMsg->data_length = nMsgLen;
	pMsg->Reserved2 = 0;


	pApp->header.AskID = 0x01;
	pApp->header.MsgType = 0x3000;
	pApp->header.MsgFmtVersion = 0x01;


286 287 288 289 290 291 292 293 294

	for (int i=0;i<=nMaxLenUserName;i++)
		pApp->MsgUnion.msg_ClientLoginReq.UserNameAndPasswd[i] =
				i<nMaxLenUserName?pSrcUsername[i]:0;


	for (int i=0;i<=nMaxLenPassword;i++)
		pApp->MsgUnion.msg_ClientLoginReq.UserNameAndPasswd[i+nMaxLenUserName+1] =
				i<nMaxLenPassword?pSrcPassword[i]:0;
丁劲犇's avatar
丁劲犇 已提交
295 296 297 298 299

	pApp->MsgUnion.msg_ClientLoginReq.ClientVersion = 0;

	//3/10 possibility to send a data block to server
	client->SendData(array);
丁劲犇's avatar
丁劲犇 已提交
300
}
丁劲犇's avatar
丁劲犇 已提交
301 302
void MainDialog::on_pushButton_CrTime_clicked()
{
丁劲犇's avatar
丁劲犇 已提交
303 304 305 306 307 308 309 310 311 312 313 314
	if (client->isOpen()==false)
	{
		QMessageBox::warning(this,"Please connect first!","Connect to server and test funcs");
		return;
	}
	if (m_bLogedIn==false)
	{
		QMessageBox::warning(this,"Please log in the host first!","get uuid and login");
		return;
	}
	saveIni();
	quint16 nMsgLen = sizeof(SMARTLINK_MSG_APP::tag_app_layer_header)
315
			/*+sizeof(stMsg_HostTimeCorrectReq)*/;
丁劲犇's avatar
丁劲犇 已提交
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
	QByteArray array(sizeof(SMARTLINK_MSG) + nMsgLen - 1,0);
	char * ptr = array.data();
	SMARTLINK_MSG * pMsg = (SMARTLINK_MSG *)ptr;
	SMARTLINK_MSG_APP * pApp = (SMARTLINK_MSG_APP *)(((unsigned char *)
													  (ptr))+sizeof(SMARTLINK_MSG)-1
													 );
	pMsg->Mark = 0x55AA;
	pMsg->version = 1;
	pMsg->SerialNum = 0;
	pMsg->Priority = 1;
	pMsg->Reserved1 = 0;
	pMsg->source_id = (quint32)((quint64)(ui->lineEdit_boxid->text().toUInt()) & 0xffffffff );

	pMsg->destin_id = (quint32)((quint64)(0x00000001) & 0xffffffff );;

	pMsg->data_length = nMsgLen;
	pMsg->Reserved2 = 0;


	pApp->header.AskID = 0x01;
	pApp->header.MsgType = 0x1002;
	pApp->header.MsgFmtVersion = 0x01;

	//3/10 possibility to send a data block to server
	client->SendData(array);
丁劲犇's avatar
丁劲犇 已提交
341
}
丁劲犇's avatar
丁劲犇 已提交
342 343
void MainDialog::on_pushButton_box_upload_uid_clicked()
{
丁劲犇's avatar
丁劲犇 已提交
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
	if (client->isOpen()==false)
	{
		QMessageBox::warning(this,"Please connect first!","Connect to server and test funcs");
		return;
	}
	if (m_bLogedIn==false)
	{
		QMessageBox::warning(this,"Please log in the host first!","get uuid and login");
		return;
	}
	saveIni();
	QStringList lst = ui->plainTextEdit_box_userids->toPlainText().split(",");
	QVector<quint32> vecInt;
	foreach (QString item,lst)
	{
		vecInt.push_back(item.toUInt());
	}

	quint16 nMsgLen = sizeof(SMARTLINK_MSG_APP::tag_app_layer_header)
			+sizeof(stMsg_UploadUserListReq)+ sizeof(quint32)*vecInt.size() - sizeof(quint32);
	QByteArray array(sizeof(SMARTLINK_MSG) + nMsgLen - 1,0);
	char * ptr = array.data();
	SMARTLINK_MSG * pMsg = (SMARTLINK_MSG *)ptr;
	SMARTLINK_MSG_APP * pApp = (SMARTLINK_MSG_APP *)(((unsigned char *)
													  (ptr))+sizeof(SMARTLINK_MSG)-1
													 );
	pMsg->Mark = 0x55AA;
	pMsg->version = 1;
	pMsg->SerialNum = 0;
	pMsg->Priority = 1;
	pMsg->Reserved1 = 0;
	pMsg->source_id = (quint32)((quint64)(ui->lineEdit_boxid->text().toUInt()) & 0xffffffff );

	pMsg->destin_id = (quint32)((quint64)(0x00000001) & 0xffffffff );;

	pMsg->data_length = nMsgLen;
	pMsg->Reserved2 = 0;


	pApp->header.AskID = 0x01;
	pApp->header.MsgType = 0x1003;
	pApp->header.MsgFmtVersion = 0x01;

	pApp->MsgUnion.msg_UploadUserListReq.UserNum = (quint16)(vecInt.size() & 0x00ffff);
	for (int i=0;i< vecInt.size();i++)
		pApp->MsgUnion.msg_UploadUserListReq.pUserIDList[i] = vecInt[i];

	//3/10 possibility to send a data block to server
	client->SendData(array);
丁劲犇's avatar
丁劲犇 已提交
393 394 395 396
}

void MainDialog::on_pushButton_box_download_uid_clicked()
{
丁劲犇's avatar
丁劲犇 已提交
397 398 399 400 401 402 403 404 405 406 407 408
	if (client->isOpen()==false)
	{
		QMessageBox::warning(this,"Please connect first!","Connect to server and test funcs");
		return;
	}
	if (m_bLogedIn==false)
	{
		QMessageBox::warning(this,"Please log in the host first!","get uuid and login");
		return;
	}
	saveIni();
	quint16 nMsgLen = sizeof(SMARTLINK_MSG_APP::tag_app_layer_header)
409
			/*+sizeof(stMsg_DownloadUserListReq)*/;
丁劲犇's avatar
丁劲犇 已提交
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
	QByteArray array(sizeof(SMARTLINK_MSG) + nMsgLen - 1,0);
	char * ptr = array.data();
	SMARTLINK_MSG * pMsg = (SMARTLINK_MSG *)ptr;
	SMARTLINK_MSG_APP * pApp = (SMARTLINK_MSG_APP *)(((unsigned char *)
													  (ptr))+sizeof(SMARTLINK_MSG)-1
													 );
	pMsg->Mark = 0x55AA;
	pMsg->version = 1;
	pMsg->SerialNum = 0;
	pMsg->Priority = 1;
	pMsg->Reserved1 = 0;
	pMsg->source_id = (quint32)((quint64)(ui->lineEdit_boxid->text().toUInt()) & 0xffffffff );

	pMsg->destin_id = (quint32)((quint64)(0x00000001) & 0xffffffff );;

	pMsg->data_length = nMsgLen;
	pMsg->Reserved2 = 0;


	pApp->header.AskID = 0x01;
	pApp->header.MsgType = 0x1004;
	pApp->header.MsgFmtVersion = 0x01;

	//3/10 possibility to send a data block to server
	client->SendData(array);
丁劲犇's avatar
丁劲犇 已提交
435 436 437
}
void  MainDialog::on_pushButton_client_downHost_clicked()
{
丁劲犇's avatar
丁劲犇 已提交
438 439 440 441 442 443 444 445 446 447 448 449
	if (client->isOpen()==false)
	{
		QMessageBox::warning(this,"Please connect first!","Connect to server and test funcs");
		return;
	}
	if (m_bLogedIn==false)
	{
		QMessageBox::warning(this,"Please log in the host first!","get uuid and login");
		return;
	}
	saveIni();
	quint16 nMsgLen = sizeof(SMARTLINK_MSG_APP::tag_app_layer_header)
450
			/*+sizeof(stMsg_GetHostListReq)*/;
丁劲犇's avatar
丁劲犇 已提交
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
	QByteArray array(sizeof(SMARTLINK_MSG) + nMsgLen - 1,0);
	char * ptr = array.data();
	SMARTLINK_MSG * pMsg = (SMARTLINK_MSG *)ptr;
	SMARTLINK_MSG_APP * pApp = (SMARTLINK_MSG_APP *)(((unsigned char *)
													  (ptr))+sizeof(SMARTLINK_MSG)-1
													 );
	pMsg->Mark = 0x55AA;
	pMsg->version = 1;
	pMsg->SerialNum = 0;
	pMsg->Priority = 1;
	pMsg->Reserved1 = 0;
	pMsg->source_id = (quint32)((quint64)(ui->lineEdit_userid->text().toUInt()) & 0xffffffff );

	pMsg->destin_id = (quint32)((quint64)(0x00000001) & 0xffffffff );;

	pMsg->data_length = nMsgLen;
	pMsg->Reserved2 = 0;


	pApp->header.AskID = 0x01;
	pApp->header.MsgType = 0x3002;
	pApp->header.MsgFmtVersion = 0x01;
	//3/10 possibility to send a data block to server
	client->SendData(array);
丁劲犇's avatar
丁劲犇 已提交
475 476 477 478
}

void  MainDialog::on_pushButton_clientLogout_clicked()
{
丁劲犇's avatar
丁劲犇 已提交
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
	if (client->isOpen()==false)
	{
		QMessageBox::warning(this,"Please connect first!","Connect to server and test funcs");
		return;
	}
	if (m_bLogedIn==false)
	{
		QMessageBox::warning(this,"Please log in the host first!","get uuid and login");
		return;
	}
	saveIni();
	quint16 nMsgLen = sizeof(SMARTLINK_MSG_APP::tag_app_layer_header)
			+sizeof(stMsg_ClientLogoutReq);
	QByteArray array(sizeof(SMARTLINK_MSG) + nMsgLen - 1,0);
	char * ptr = array.data();
	SMARTLINK_MSG * pMsg = (SMARTLINK_MSG *)ptr;
	SMARTLINK_MSG_APP * pApp = (SMARTLINK_MSG_APP *)(((unsigned char *)
													  (ptr))+sizeof(SMARTLINK_MSG)-1
													 );
	pMsg->Mark = 0x55AA;
	pMsg->version = 1;
	pMsg->SerialNum = 0;
	pMsg->Priority = 1;
	pMsg->Reserved1 = 0;
	pMsg->source_id = (quint32)((quint64)(ui->lineEdit_userid->text().toUInt()) & 0xffffffff );

	pMsg->destin_id = (quint32)((quint64)(0x00000001) & 0xffffffff );;

	pMsg->data_length = nMsgLen;
	pMsg->Reserved2 = 0;


	pApp->header.AskID = 0x01;
	pApp->header.MsgType = 0x3001;
	pApp->header.MsgFmtVersion = 0x01;


	pApp->MsgUnion.msg_ClientLogoutReq.UserName[0] = 0;


	//3/10 possibility to send a data block to server
	client->SendData(array);
丁劲犇's avatar
丁劲犇 已提交
521
}
522

523 524 525 526
//!deal one message, affect m_currentRedOffset,m_currentMessageSize,m_currentHeader
//!return bytes Used.
int MainDialog::filter_message(const QByteArray & block, int offset)
{
丁劲犇's avatar
丁劲犇 已提交
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
	const int blocklen = block.length();
	while (blocklen>offset)
	{
		const char * dataptr = block.constData();

		//Recieve First 2 byte
		while (m_currentMessageSize<2 && blocklen>offset )
		{
			m_currentBlock.push_back(dataptr[offset++]);
			m_currentMessageSize++;
		}
		if (m_currentMessageSize < 2) //First 2 byte not complete
			continue;

		if (m_currentMessageSize==2)
		{
			const char * headerptr = m_currentBlock.constData();
			memcpy((void *)&m_currentHeader,headerptr,2);
		}

		const char * ptrCurrData = m_currentBlock.constData();
		//Heart Beating
		if (m_currentHeader.Mark == 0xBEBE)
		{
			while (m_currentMessageSize< sizeof(SMARTLINK_HEARTBEATING) && blocklen>offset )
			{
				m_currentBlock.push_back(dataptr[offset++]);
				m_currentMessageSize++;
			}
			if (m_currentMessageSize < sizeof(SMARTLINK_HEARTBEATING)) //Header not completed.
				continue;
			const SMARTLINK_HEARTBEATING * ptr = (const SMARTLINK_HEARTBEATING *)m_currentBlock.constData();
			displayMessage(tr("Recieved Heart-beating msg sended %1 sec(s) ago.").
						   arg((time(0)&0x00ffff)-(ptr->tmStamp)));
			//This Message is Over. Start a new one.
			m_currentMessageSize = 0;
			m_currentBlock = QByteArray();

			continue;
		}
		else if (m_currentHeader.Mark == 0x55AA)
			//Trans Message
		{
			while (m_currentMessageSize< sizeof(SMARTLINK_MSG)-1 && blocklen>offset)
			{
				m_currentBlock.push_back(dataptr[offset++]);
				m_currentMessageSize++;
			}
			if (m_currentMessageSize < sizeof(SMARTLINK_MSG)-1) //Header not completed.
				continue;
			else if (m_currentMessageSize == sizeof(SMARTLINK_MSG)-1)//Header just  completed.
			{
				const char * headerptr = m_currentBlock.constData();
				memcpy((void *)&m_currentHeader,headerptr,sizeof(SMARTLINK_MSG)-1);

				//continue reading if there is data left behind
				if (block.length()>offset)
				{
					qint32 bitLeft = m_currentHeader.data_length + sizeof(SMARTLINK_MSG) - 1
							-m_currentMessageSize ;
					while (bitLeft>0 && blocklen>offset)
					{
						m_currentBlock.push_back(dataptr[offset++]);
						m_currentMessageSize++;
						bitLeft--;
					}
					//deal block, may be send data as soon as possible;
					deal_current_message_block();
					if (bitLeft>0)
						continue;
					//This Message is Over. Start a new one.
					m_currentMessageSize = 0;
					m_currentBlock = QByteArray();
					continue;
				}
			}
			else
			{
				if (block.length()>offset)
				{
					qint32 bitLeft = m_currentHeader.data_length + sizeof(SMARTLINK_MSG) - 1
							-m_currentMessageSize ;
					while (bitLeft>0 && blocklen>offset)
					{
						m_currentBlock.push_back(dataptr[offset++]);
						m_currentMessageSize++;
						bitLeft--;
					}
					//deal block, may be processed as soon as possible;
					deal_current_message_block();
					if (bitLeft>0)
						continue;
					//This Message is Over. Start a new one.
					m_currentMessageSize = 0;
					m_currentBlock = QByteArray();
					continue;
				}
			} // end if there is more bytes to append
		} //end deal trans message
		else
		{
			displayMessage(tr("Client Send a unknown start Header %1 %2. Close client immediately.")
丁劲犇's avatar
丁劲犇 已提交
629
						   .arg((int)(ptrCurrData[0])).arg((int)(ptrCurrData[1])));
丁劲犇's avatar
丁劲犇 已提交
630 631 632 633 634 635 636 637 638
			m_currentMessageSize = 0;
			m_currentBlock = QByteArray();
			offset = blocklen;

			this->client->abort();
		}
	} // end while block len > offset

	return offset;
639 640 641 642
}
//!deal current message
int MainDialog::deal_current_message_block()
{
丁劲犇's avatar
丁劲犇 已提交
643 644 645 646 647 648
	//The bytes left to recieve.
	qint32 bytesLeft = m_currentHeader.data_length + sizeof(SMARTLINK_MSG) - 1
			-m_currentMessageSize ;
	if (bytesLeft)
		return 0;

丁劲犇's avatar
丁劲犇 已提交
649 650 651

	const char * ptr = m_currentBlock.constData();

丁劲犇's avatar
丁劲犇 已提交
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
	SMARTLINK_MSG_APP * pApp = (SMARTLINK_MSG_APP *)(((unsigned char *)
													  (ptr))+sizeof(SMARTLINK_MSG)-1
													 );

	if (pApp->header.MsgType==0x1800)
	{
		ui->lineEdit_boxid->setText(QString("%1").arg(pApp->MsgUnion.msg_HostRegistRsp.ID));
		displayMessage(tr("Res = %1, ID = %2")
					   .arg(pApp->MsgUnion.msg_HostRegistRsp.DoneCode)
					   .arg(pApp->MsgUnion.msg_HostRegistRsp.ID)
					   );
		if (pApp->MsgUnion.msg_HostRegistRsp.DoneCode!=2)
			m_bUUIDGot = true;
	}
	else if (pApp->header.MsgType==0x1801)
	{
		if (pApp->MsgUnion.msg_HostLogonRsp.DoneCode==0)
		{
			m_bLogedIn = true;
671
			m_bBox = true;
丁劲犇's avatar
丁劲犇 已提交
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
			QMessageBox::information(this,tr("Succeed!"),tr("Log in succeed!"));
		}
		else
			QMessageBox::information(this,tr("Failed!"),tr("Log in Failed!"));
		displayMessage(tr("Res = %1")
					   .arg(pApp->MsgUnion.msg_HostLogonRsp.DoneCode)
					   );
	}
	else if (pApp->header.MsgType==0x1802)
	{
		if (pApp->MsgUnion.msg_HostTimeCorrectRsp.DoneCode==0)
			QMessageBox::information(this,tr("Succeed!"),tr("Time Corrected!"));
		else
			QMessageBox::information(this,tr("Failed!"),tr("Time Correct Failed!"));
		displayMessage(tr("Res = %1")
					   .arg(pApp->MsgUnion.msg_HostTimeCorrectRsp.DoneCode)
					   );
		QString tm = QString("%1-%2-%3 %4:%5:%6")
				.arg(pApp->MsgUnion.msg_HostTimeCorrectRsp.DateTime.Year)
				.arg(pApp->MsgUnion.msg_HostTimeCorrectRsp.DateTime.Month)
				.arg(pApp->MsgUnion.msg_HostTimeCorrectRsp.DateTime.Day)
				.arg(pApp->MsgUnion.msg_HostTimeCorrectRsp.DateTime.Hour)
				.arg(pApp->MsgUnion.msg_HostTimeCorrectRsp.DateTime.Minute)
				.arg(pApp->MsgUnion.msg_HostTimeCorrectRsp.DateTime.Second)
				;
		ui->lineEdit_box_time->setText(tm);

	}
	else if (pApp->header.MsgType==0x3800)
	{
		if (pApp->MsgUnion.msg_ClientLoginRsp.DoneCode==0)
		{
			m_bLogedIn = true;
705
			m_bBox = false;
丁劲犇's avatar
丁劲犇 已提交
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
			QMessageBox::information(this,tr("Succeed!"),tr("Log in succeed!"));
		}
		else
			QMessageBox::information(this,tr("Failed!"),tr("Log in Failed!"));
		displayMessage(tr("Res = %1")
					   .arg(pApp->MsgUnion.msg_ClientLoginRsp.DoneCode)
					   );
		ui->lineEdit_userid->setText(QString ("%1").arg(pApp->MsgUnion.msg_ClientLoginRsp.UserID));
	}
	else if (pApp->header.MsgType==0x1803)
	{
		if (pApp->MsgUnion.msg_UploadUserListRsp.DoneCode==0)
			QMessageBox::information(this,tr("Succeed!"),tr("upload succeed!"));
		else
			QMessageBox::information(this,tr("Failed!"),tr("upload in Failed!"));
		displayMessage(tr("Res = %1")
					   .arg(pApp->MsgUnion.msg_UploadUserListRsp.DoneCode)
					   );

	}
	else if (pApp->header.MsgType==0x1804)
	{
		if (pApp->MsgUnion.msg_DownloadUserListRsp.DoneCode==0)
		{
			QMessageBox::information(this,tr("Succeed!"),tr("download succeed!"));
			QString strRes;
			for (quint16 i = 0;i<pApp->MsgUnion.msg_DownloadUserListRsp.UserNum;i++)
			{
				strRes += QString("%1,").arg(pApp->MsgUnion.msg_DownloadUserListRsp.pUserIDList[i]);
			}
			ui->plainTextEdit_box_userids->setPlainText(strRes);
		}
		else
			QMessageBox::information(this,tr("Failed!"),tr("download in Failed!"));
		displayMessage(tr("Res = %1")
					   .arg(pApp->MsgUnion.msg_DownloadUserListRsp.DoneCode)
丁劲犇's avatar
丁劲犇 已提交
742
					   );
丁劲犇's avatar
丁劲犇 已提交
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770

	}
	else if (pApp->header.MsgType==0x3801)
	{
		if (pApp->MsgUnion.msg_ClientLogoutRsp.DoneCode==0)
			QMessageBox::information(this,tr("Succeed!"),tr("log out succeed!"));
		else
			QMessageBox::information(this,tr("Failed!"),tr("download in Failed!"));
		displayMessage(tr("Res = %1")
					   .arg(pApp->MsgUnion.msg_ClientLogoutRsp.DoneCode)
					   );
		this->client->abort();
	}
	else if (pApp->header.MsgType==0x3802)
	{
		if (pApp->MsgUnion.msg_GetHostListRsp.HostNum>0)
		{
			QMessageBox::information(this,tr("Succeed!"),tr("download succeed!"));
			QString strRes;
			for (quint16 i = 0;i<pApp->MsgUnion.msg_GetHostListRsp.HostNum;i++)
			{
				strRes += QString("%1").arg(pApp->MsgUnion.msg_GetHostListRsp.pHostIDList[i]);
			}
			ui->plainTextEdit_hostsIds->setPlainText(strRes);
		}
		else
			QMessageBox::information(this,tr("Failed!"),tr("download in Failed!"));
	}
丁劲犇's avatar
丁劲犇 已提交
771 772 773 774 775 776 777 778 779 780 781
	else
	{
		QString str;
		int nLen =  m_currentHeader.data_length;
		for (int i=0;i<nLen;i++)
		{
			str += ptr[i+ sizeof(SMARTLINK_MSG) - 1];
		}
		ui->plainTextEdit_msg_recieved->setPlainText(str);

	}
丁劲犇's avatar
丁劲犇 已提交
782 783 784 785
	m_currentBlock = QByteArray();


	return 0;
786
}
丁劲犇's avatar
丁劲犇 已提交
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812

void MainDialog::on_pushButton_sendToClient_clicked()
{
	if (client->isOpen()==false)
	{
		QMessageBox::warning(this,"Please connect first!","Connect to server and test funcs");
		return;
	}
	if (m_bLogedIn==false)
	{
		QMessageBox::warning(this,"Please log in the host first!","get uuid and login");
		return;
	}
	saveIni();
	QString strMsg = ui->plainTextEdit_msg_to_client->toPlainText();
	QByteArray arrMsg(strMsg.toStdString().c_str());
	QByteArray array(sizeof(SMARTLINK_MSG) - 1,0);
	char * ptr = array.data();
	SMARTLINK_MSG * pMsg = (SMARTLINK_MSG *)ptr;

	pMsg->Mark = 0x55AA;
	pMsg->version = 1;
	pMsg->SerialNum = 0;
	pMsg->Priority = 1;
	pMsg->Reserved1 = 0;

813 814 815 816
	if (m_bBox==true)
		pMsg->source_id = (quint32)((quint64)(ui->lineEdit_boxid->text().toUInt()) & 0xffffffff );
	else
		pMsg->source_id = (quint32)((quint64)(ui->lineEdit_userid->text().toUInt()) & 0xffffffff );
丁劲犇's avatar
丁劲犇 已提交
817 818 819 820 821 822 823 824 825 826
	pMsg->destin_id = (quint32)((quint64)(ui->lineEdit_client_uuid->text().toUInt()) & 0xffffffff );;

	pMsg->data_length = arrMsg.size();
	pMsg->Reserved2 = 0;

	array.append(arrMsg);

	//3/10 possibility to send a data block to server
	client->SendData(array);
}