Read about our upcoming Code of Conduct on this issue

This instance will be upgraded to Heptapod 0.28.1 on 2022-01-26 at 16:00 UTC+1 (a few minutes of down time)

Commit a16a8a00 authored by aivan_2008's avatar aivan_2008
Browse files

1. Finished simple client modification to fulfill basic server control requirements.

2. Added basic save/load project network commands support.

--HG--
branch : ClientServerQt
parent 41fb1e540643
......@@ -6,7 +6,7 @@ RdkQTcpThread::RdkQTcpThread()
}
RdkQTcpThread::RdkQTcpThread(QTcpSocket *socket, RDK::UTransferReader &packetReader, bool CreateSUspended, QObject *parent):
RdkQTcpThread::RdkQTcpThread(QTcpSocket *socket, RDK::UTransferReader &packetReader, bool CreateSuspended, QObject *parent):
_socket(socket), QThread(parent), PacketReader(&packetReader)
{
CalcEnable = UCreateEvent(true);
......@@ -14,7 +14,12 @@ _socket(socket), QThread(parent), PacketReader(&packetReader)
CalculationNotInProgress = UCreateEvent(false);
CalculationInProgress = UCreateEvent(true);
SendInProgress = UCreateEvent(true);
SendInProgress->set();
ThreadError = 0;
if(!CreateSuspended)
{
start();
}
}
RdkQTcpThread::~RdkQTcpThread()
......@@ -62,6 +67,9 @@ void RdkQTcpThread::run()
// continue;
try
{
bool sv = _socket->isValid();
bool cst = _socket->state()==QAbstractSocket::SocketState::ConnectedState;
int bav = _socket->bytesAvailable();
if((_socket->isValid()) && (_socket->state()==QAbstractSocket::SocketState::ConnectedState) && (_socket->bytesAvailable()>0))
{
RDK::UParamT ClientBuffer;
......@@ -82,7 +90,7 @@ void RdkQTcpThread::run()
QDataStream in;
in.setDevice(_socket);
in.setVersion(QDataStream::Qt_5_0);
in.startTransaction();
//in.startTransaction();
//int len = _socket->bytesAvailable();
char *buffer;
uint length=0;
......@@ -114,6 +122,10 @@ void RdkQTcpThread::run()
}
}
PacketReaderUnlockEvent->exclusive_unlock();
//in.commitTransaction();
if(new_packet_size>prev_packet_size)
CalcEnable->reset();
//ResetEvent(CalcEnable);
......@@ -199,6 +211,7 @@ void RdkQTcpClient::AddIdCmdsRemovablePacket(int value)
//ReleaseMutex(Thread->PacketReaderUnlockEvent);
Thread->PacketReaderUnlockEvent->exclusive_unlock();
}
Thread->PacketReaderUnlockEvent->exclusive_unlock();
}
///
......@@ -212,6 +225,7 @@ void RdkQTcpClient::ClearIdCmdsRemovablePackets(void)
//ReleaseMutex(Thread->PacketReaderUnlockEvent);
Thread->PacketReaderUnlockEvent->exclusive_unlock();
}
Thread->PacketReaderUnlockEvent->exclusive_unlock();
}
///
......@@ -268,7 +282,10 @@ bool RdkQTcpClient::EraseUnresponcedPackets(void)
Thread->PacketReaderUnlockEvent->exclusive_unlock();
}
else
{
Thread->PacketReaderUnlockEvent->exclusive_unlock();
return false;
}
return true;
}
......@@ -326,7 +343,7 @@ int RdkQTcpClient::SendControlCommand(RDK::USerStorageXML &xml)
QDataStream out;
out.setDevice(TcpSocket);
out.setVersion(QDataStream::Qt_5_0);
out.startTransaction();
//out.startTransaction();
char* buf;
buf = new char[buffer.size()];
......@@ -385,7 +402,7 @@ int RdkQTcpClient::FindPacketById(int cmdId, RDK::USerStorageXML &xml, bool eras
if(erase_found)
packetList.erase(it);
//ReleaseMutex(Thread->PacketReaderUnlockEvent);
Thread->PacketReaderUnlockEvent->exclusive_lock();
Thread->PacketReaderUnlockEvent->exclusive_unlock();
return 1;
}
}
......@@ -430,7 +447,7 @@ int RdkQTcpClient::FindPacketById(int cmdId, RDK::USerStorageXML &xml, std::vect
if(erase_found)
packetList.erase(it);
//ReleaseMutex(Thread->PacketReaderUnlockEvent);
Thread->PacketReaderUnlockEvent->exclusive_lock();
Thread->PacketReaderUnlockEvent->exclusive_unlock();
return 1;
}
}
......@@ -458,6 +475,7 @@ void RdkQTcpClient::ErasePacket(std::list<RDK::UTransferPacket>::iterator &it)
//ReleaseMutex(Thread->PacketReaderUnlockEvent);
Thread->PacketReaderUnlockEvent->exclusive_unlock();
}
Thread->PacketReaderUnlockEvent->exclusive_unlock();
}
......@@ -475,12 +493,14 @@ int RdkQTcpClient::WaitServerResponse(int cmdId, RDK::USerStorageXML &response,
// {
// break;
// }
if(timeout<e_timer.elapsed())
if(e_timer.elapsed()>timeout)
break;
int diff=timeout-e_timer.elapsed();
QCoreApplication::processEvents();
//int diff=timeout-e_timer.elapsed();
//if(WaitForSingleObject(Thread->PacketReceivedEvent, diff) != WAIT_TIMEOUT)
if(Thread->PacketReceivedEvent->wait(diff)!=false)
if(Thread->PacketReceivedEvent->wait(10)!=false)
{
//ResetEvent(Thread->PacketReceivedEvent);
Thread->PacketReceivedEvent->reset();
......
......@@ -18,7 +18,7 @@ class RdkQTcpThread: public QThread
Q_OBJECT
public: //
RdkQTcpThread();
RdkQTcpThread(QTcpSocket *socket, RDK::UTransferReader &packetReader, bool CreateSUspended, QObject *parent);
RdkQTcpThread(QTcpSocket *socket, RDK::UTransferReader &packetReader, bool CreateSuspended, QObject *parent);
~RdkQTcpThread();
public: //
......
......@@ -506,3 +506,13 @@ int Rpc_SaveProject(int server_index, int timeout)
RDK::USerStorageXML request,response;
return ProcessSimpleCommand("SaveProject", server_index, -1, timeout, request, response);
}
int Rpc_LoadProject(int server_index, const char* project_path, int timeout)
{
RDK::USerStorageXML request,response;
request.Create("Request");
request.WriteString("FileName", project_path);
return ProcessSimpleCommand("LoadProject", server_index, -1, timeout, request, response);
}
......@@ -75,5 +75,24 @@ int Rpc_AddServer(void);
int Rpc_DelServer(int server_index);
///////////////
int Rpc_StartChannel(int server_index, int channel_index, int timeout);
int Rpc_StopChannel(int server_index, int channel_index, int timeout);
int Rpc_ResetChannel(int server_index, int channel_index, int timeout);
int Rpc_GetNumChannels(int server_index, int &results, int timeout);
int Rpc_SetNumChannels(int server_index, int num_channels, int timeout);
int Rpc_GetChannelName(int server_index, int channel_index, const char* &result, int timeout);
int Rpc_SetChannelName(int server_index, int channel_index, const char* channel_name, int timeout);
int Rpc_SaveProject(int server_index, int timeout);
int Rpc_LoadProject(int server_index, const char* project_path, int timeout);
#endif // RDKQTCPCLIENTLIB_H
......@@ -2,6 +2,7 @@
#include "ui_RdkQTcpClientWidget.h"
#include <iostream>
#include <QFileDialog>
#include <QMessageBox>
#include <QDateTime>
......@@ -31,6 +32,21 @@ RdkQTcpClientWidget::RdkQTcpClientWidget(QWidget *parent)
connect(ui->pushButtonConnect, SIGNAL(clicked()), this, SLOT(onPushButtonConnectClick()));
connect(ui->pushButtonDisconnect, SIGNAL(clicked()), this, SLOT(onPushButtonDisconnectClick()));
connect(ui->pushButtonStartChannel, SIGNAL(clicked()), this, SLOT(onPushButtonStartChannelClick()));
connect(ui->pushButtonStopChannel, SIGNAL(clicked()), this, SLOT(onPushButtonStopChannelClick()));
connect(ui->pushButtonResetChannel, SIGNAL(clicked()), this, SLOT(onPushButtonResetChannelClick()));
connect(ui->pushButtonGetNumChannels, SIGNAL(clicked()), this, SLOT(onPushButtonGetNumChannelsClick()));
connect(ui->pushButtonSaveServer, SIGNAL(clicked()), this, SLOT(onPushButtonSaveServerClick()));
connect(ui->pushButtonGetChannelName, SIGNAL(clicked()), this, SLOT(onPushButtonGetChannelNameClick()));
connect(ui->pushButtonSetChannelName, SIGNAL(clicked()), this, SLOT(onPushButtonSetChannelNameClick()));
connect(ui->pushButtonLoadServerConfig, SIGNAL(clicked()), this, SLOT(onPushButtonLoadConfigClick()));
ui->lineEditChannelIndex->setText("0");
}
RdkQTcpClientWidget::~RdkQTcpClientWidget()
......@@ -89,7 +105,7 @@ void RdkQTcpClientWidget::onPushButtonConnectClick()
{
const char* server_addr = address.c_str();
int res = Rpc_Connect(0, server_addr, port);
res = Rpc_Connect(1, server_addr, port);
//res = Rpc_Connect(1, server_addr, port);
if(Rpc_IsClientConnected(0))
{
WriteLog("Client connected");
......@@ -102,9 +118,94 @@ void RdkQTcpClientWidget::onPushButtonConnectClick()
void RdkQTcpClientWidget::onPushButtonDisconnectClick()
{
int res = Rpc_Disconnect(0);
if(!Rpc_IsClientConnected(0))
{
WriteLog("Client disconnected");
}
WriteLogError(res);
//WriteLog("")
}
void RdkQTcpClientWidget::onPushButtonStartChannelClick()
{
const char* serverAnswer=NULL;
int res=Rpc_StartChannel(0, ui->lineEditChannelIndex->text().toInt(), 60000);
if(WriteLogError(res))
{
serverAnswer=Rpc_GetServerAnswerDebug(0);
if(serverAnswer != NULL && ui->checkBoxWriteServerResponse->isChecked())
ui->plainTextEditLog->appendPlainText(QString(serverAnswer));
}
}
void RdkQTcpClientWidget::onPushButtonStopChannelClick()
{
const char* serverAnswer=NULL;
int res=Rpc_StopChannel(0, ui->lineEditChannelIndex->text().toInt(), 5000);
if(WriteLogError(res))
{
serverAnswer=Rpc_GetServerAnswerDebug(0);
if(serverAnswer != NULL && ui->checkBoxWriteServerResponse->isChecked())
ui->plainTextEditLog->appendPlainText(QString(serverAnswer));
}
}
void RdkQTcpClientWidget::onPushButtonResetChannelClick()
{
const char* serverAnswer=NULL;
int res=Rpc_ResetChannel(0, ui->lineEditChannelIndex->text().toInt(), 5000);
if(WriteLogError(res))
{
serverAnswer=Rpc_GetServerAnswerDebug(0);
if(serverAnswer != NULL && ui->checkBoxWriteServerResponse->isChecked())
ui->plainTextEditLog->appendPlainText(QString(serverAnswer));
}
}
void RdkQTcpClientWidget::onPushButtonGetNumChannelsClick()
{
int res=0;
Rpc_GetNumChannels(0, res, 10000);
ui->lineEditNumChannels->setText(QString::number(res));
}
void RdkQTcpClientWidget::onPushButtonSaveServerClick()
{
int res=Rpc_SaveProject(0, 10000);
}
void RdkQTcpClientWidget::onPushButtonGetChannelNameClick()
{
const char *res=0;
Rpc_GetChannelName(0, ui->lineEditChannelIndex->text().toInt(), res, 1000);
if(res)
ui->lineEditChannelName->setText(QString(res));
}
void RdkQTcpClientWidget::onPushButtonSetChannelNameClick()
{
std::string name = ui->lineEditChannelName->text().toUtf8().constData();
Rpc_SetChannelName(0, ui->lineEditChannelIndex->text().toInt(), name.c_str(), 1000);
}
void RdkQTcpClientWidget::onPushButtonLoadConfigClick()
{
QFileDialog fd(this);
fd.setWindowTitle("Open config file");
fd.setDirectory("/home/ivan");
fd.selectNameFilter("*.ini");
QString fileName = QFileDialog::getOpenFileName(this, tr("Open config file"), QCoreApplication::applicationDirPath(), tr("*.ini"), nullptr, QFileDialog::DontUseNativeDialog);
if (fileName.isEmpty())
return;
std::string name = fileName.toUtf8().constData();
Rpc_LoadProject(0, name.c_str(), 1000);
}
/*
void RdkQTcpClientWidget::onPushButtonPingClick()
{
......
......@@ -29,6 +29,17 @@ private slots:
void onPushButtonConnectClick();
void onPushButtonDisconnectClick();
void onPushButtonStartChannelClick();
void onPushButtonStopChannelClick();
void onPushButtonResetChannelClick();
void onPushButtonGetNumChannelsClick();
void onPushButtonSaveServerClick();
void onPushButtonGetChannelNameClick();
void onPushButtonSetChannelNameClick();
void onPushButtonLoadConfigClick();
private:
Ui::RdkQTcpClientWidget *ui;
std::map<int, QString> Errors;
......
......@@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>676</width>
<height>437</height>
<height>637</height>
</rect>
</property>
<property name="windowTitle">
......@@ -52,19 +52,76 @@
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_5">
<item>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QLabel" name="labelChannelIndex">
<property name="text">
<string>Channel index</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEditChannelIndex"/>
</item>
</layout>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QLabel" name="labelChannelName">
<property name="text">
<string>ChannelName</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEditChannelName"/>
</item>
</layout>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_6">
<item>
<widget class="QPushButton" name="pushButtonGetChannelName">
<property name="text">
<string>Get Channel Name</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButtonSetChannelName">
<property name="text">
<string>Set Channel Name</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QPushButton" name="pushButtonStartServer">
<widget class="QPushButton" name="pushButtonStartChannel">
<property name="text">
<string>Start</string>
<string>Start Channel</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButtonStopServer">
<widget class="QPushButton" name="pushButtonResetChannel">
<property name="text">
<string>Stop</string>
<string>Reset Channel</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButtonStopChannel">
<property name="text">
<string>Stop Channel</string>
</property>
</widget>
</item>
......@@ -80,7 +137,7 @@
<item>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QLineEdit" name="lineEdit"/>
<widget class="QLineEdit" name="lineEditNumChannels"/>
</item>
<item>
<widget class="QPushButton" name="pushButtonGetNumChannels">
......@@ -91,6 +148,9 @@
</item>
<item>
<widget class="QPushButton" name="pushButtonSetNumChannels">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Set</string>
</property>
......@@ -110,12 +170,29 @@
<item>
<widget class="QPushButton" name="pushButtonSaveServer">
<property name="text">
<string>Save Server</string>
<string>Save Server Config</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButtonLoadServerConfig">
<property name="text">
<string>Load Config...</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QCheckBox" name="checkBoxWriteServerResponse">
<property name="cursor">
<cursorShape>WaitCursor</cursorShape>
</property>
<property name="text">
<string>Write server response</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="labelLog">
<property name="text">
......
......@@ -324,13 +324,16 @@ const char* URpcDecoderCommon::RemoteCall(const char *request, int &return_value
std::string file_name=xml.ReadString("FileName","");
if(!file_name.empty())
{
return_value=1;//UServerControlForm->LoadProject(channel_index,file_name);
GetApplication()->OpenProject(file_name);
RDK::UIVisualControllerStorage::UpdateInterface(true);
return_value=0;//UServerControlForm->LoadProject(channel_index,file_name);
}
}
else
if(cmd == "SaveProject")
{
return_value=1;//UServerControlForm->SaveProject();
GetApplication()->SaveProject();
return_value=0;//UServerControlForm->SaveProject();
}
}
// }
......
......@@ -134,6 +134,16 @@ void UServerTransport::SetServerBinding(std::string &interface_address, int port
}
int UServerTransport::GetSocketState(std::string bind)
{
return -1;
}
bool UServerTransport::ServerIsActive()
{
return false;
}
//
std::string UServerTransport::GetServerBindingInterfaceAddress(void)
{
......
......@@ -48,6 +48,10 @@ virtual std::string GetServerBindingInterfaceAddress(void);
//
virtual int GetServerBindingPort(void) const;
virtual int GetSocketState(std::string bind);
virtual bool ServerIsActive();
/// ,
/// 01.04.2020 , *Tcp, *Http, *Udp
void ProcessIncomingData(std::string &bind, std::vector<URpcCommandInternal> &command_list);
......
......@@ -13,6 +13,68 @@ extern RDK::UApplication RdkApplication;
///
//extern UEngineControlVcl RdkEngineControl;
UServerSocketQt::UServerSocketQt()
{
}
UServerSocketQt::UServerSocketQt(QTcpSocket* s)
{
this->socket = s;
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
}
UServerSocketQt::~UServerSocketQt()
{
}
void UServerSocketQt::SetSocket(QTcpSocket *s)
{
/*if(socket!=NULL)
{
if(socket->isOpen())
{
socket->close();
}
delete socket;
socket=NULL;
}*/
disconnect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
disconnect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
this->socket = s;
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
}
QTcpSocket* UServerSocketQt::GetSocket()
{
return socket;
}
void UServerSocketQt::readyRead()
{
// bind
QString peer_name = socket->peerAddress().toString();
QString peer_port = QString::number(socket->peerPort());
QString b = peer_name+":"+peer_port;
std::string bnd = b.toUtf8().constData();
emit(onReadyRead(bnd));
}
void UServerSocketQt::disconnected()
{
// bind
QString peer_name = socket->peerAddress().toString();
QString peer_port = QString::number(socket->peerPort());
QString b = peer_name+":"+peer_port;
std::string bnd = b.toUtf8().constData();
emit(onDisconnected(bnd));
}
UServerTransportTcpQt::UServerTransportTcpQt()
{
......@@ -94,7 +156,7 @@ void UServerTransportTcpQt::ServerNewConnection()
bool b1 = Application->GetServerControl()!=NULL;
bool b2 = Application->GetServerControl()->GetServerTransport()!=NULL;
std::map<std::string, QTcpSocket*>::iterator I = serverSockets.find(bnd);
std::map<std::string, UServerSocketQt*>::iterator I = serverSockets.find(bnd);
if(I!=serverSockets.end())
{
......@@ -102,17 +164,68 @@ void UServerTransportTcpQt::ServerNewConnection()
serverSockets.erase(I);
}
serverSockets[bnd] = socket;
UServerSocketQt* sqt = new UServerSocketQt(socket);
connect(sqt, SIGNAL(onReadyRead(std::string)), this, SLOT(SocketReadyRead(std::string)));
connect(sqt, SIGNAL(onDisconnected(std::string)), this, SLOT(SocketDisconnected(std::string)));
serverSockets[bnd] = sqt;
Application->GetServerControl()->GetServerTransport()->ConnectClient(bnd);
Application->GetServerControl()->ProcessIncomingData(bnd, Application->GetServerControl()->GetServerTransport());
//Application->GetServerControl()->ProcessIncomingData(bnd, Application->GetServerControl()->GetServerTransport());
}
}