#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QJsonObject>
#include <QJsonDocument>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
m_tcpserver = new QTcpServer(this);
m_tcpsocket = new QTcpSocket(this);
socket_list = new QList<QTcpSocket*>;
//clientdata = new QList<client_data*>;
m_tcpserver->listen(QHostAddress::Any,6666);
// connect(m_tcpserver,SIGNAL(newConnection()),
// this,SLOT(newConnect()));
connect(m_tcpserver,SIGNAL(newConnection()),
this,SLOT(newConnect()));
}
Widget::~Widget()
{
delete ui;
}
void Widget::newConnect()
{
m_tcpsocket = m_tcpserver->nextPendingConnection();
socket_list->append(m_tcpsocket);
connect(m_tcpsocket,SIGNAL(readyRead()),
this,SLOT(readMessage()));
// connect(m_tcpsocket,SIGNAL(disconnected()),
// m_tcpsocket,SLOT(deleteLater()));
}
void Widget::readMessage()
{
struct client_data *cdata;
for(int i = 0;i < socket_list->length();i ++)
{
//qDebug() << "QDataStream " ;
QDataStream in(socket_list->at(i));
in.setVersion (QDataStream::Qt_5_4);
in >> message;
if(!(message.isEmpty()))
{
qDebug() << "break ";
break;
}
a = i;
QByteArray byte_array;
byte_array = message.toLatin1();
qDebug() << message ;
//qDebug()<< byte_array;
//现在解析json数据
QJsonParseError json_error;
QJsonDocument parse_doucment = QJsonDocument::fromJson(byte_array,&json_error);
//qDebug() <<json_error.error; //打印错误信息,看返回的是哪种类型的
if(json_error.error == QJsonParseError::NoError)
{
//qDebug()<< "error";
if(parse_doucment.isObject())
{
QJsonObject obj = parse_doucment.object();
if(obj.contains("client_id"))
{
QJsonValue client_id_value = obj.take("client_id");
if(client_id_value.isDouble())
{
int client_id = client_id_value.toVariant().toInt();
cdata->client_id = client_id;
//qDebug()<< client_id;
}
}
if(obj.contains("requestcode1"))
{
QJsonValue requestcode1_value = obj.take("requestcode1");
if(requestcode1_value.isBool())
{
bool requestcode1 = requestcode1_value.toBool();
cdata->requestcode1 = requestcode1;
//qDebug()<< requestcode1;
}
}
if(obj.contains("requestcode2"))
{
QJsonValue requestcode2_value = obj.take("requestcode2");
if(requestcode2_value.isBool())
{
bool requestcode2 = requestcode2_value.toBool();
cdata->requestcode2 = requestcode2;
//qDebug()<< requestcode2;
}
}
}
}
//解析完json数据,把每一个socket的数据放进cliendata里面
if(cdata->requestcode1 == true)
{
//查询数据库的信息,并给客户端发送排队人数,和取号号码
}
else
{
//不处理
}
if(cdata->requestcode2 == true)
{
//数据库更新
}
else
{
//不处理,数据库不更新
}
//clientdata->append(cdata);
}
// //对每一个clientdata遍历,进行处理 code 立即处理,不需要保存
// for(int i = 0;i < clientdata->length();i ++)
// {
// struct client_data *data = clientdata->at(i);
// if(data->requestcode1 == true)
// {
// //查询数据库的信息,并给客户端发送排队人数,和取号号码
// }
// else
// {
// //不处理
// }
// if(data->requestcode2 == true)
// {
// //数据库更新
// }
// else
// {
// //不处理,数据库不更新
// }
// }
}
void Widget::sendMessage(QString takeNumber,int queue,bool stateCode)
{
QJsonObject json;
json.insert("takeNumber",takeNumber);//取号号码
json.insert("queue", queue);//排队人数
json.insert("stateCode",stateCode);//状态码
QJsonDocument document;
document.setObject(json);
QByteArray byte_array = document.toJson(QJsonDocument::Compact);
QString json_str(byte_array);
//QString str = infomation;
qDebug()<<json_str;
QByteArray information;
QDataStream out(&information,QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_5_4);
out<<json_str;
//m_tcpsocket->write(message);
for (int i = 0;i < socket_list->length(); i++)
{
if(a==i)
{
socket_list->at(i)->write(information);
}
//qDebug() << information;
}
}
void Widget::on_pushButton_clicked()
{
QString str = QString("A0005");
sendMessage(str,5,true);
//ui->lineEdit->clear();
}