Bläddra i källkod

change fwupdate. not complete.

yuchuli 1 år sedan
förälder
incheckning
2099cb3040

+ 60 - 5
src/tool/fwupdate_admin/grpcfwclient.cpp

@@ -1,7 +1,10 @@
 #include "grpcfwclient.h"
 
-grpcfwclient::grpcfwclient()
+
+grpcfwclient::grpcfwclient(std::string strserverip ,std::string strserverport )
 {
+    mstrserverip = strserverip;
+    mstrserverport = strserverport;
     mbRun = true;
     mpthread = new std::thread(&grpcfwclient::threadrpc,this);
 }
@@ -49,15 +52,19 @@ void grpcfwclient::threadrpc()
 
         iv::queryrpcReq xreq;
         iv::queryrpcReply xreply;
-        if(mbReplyUpdate)
+        if(mbReqUpdate)
         {
             mmutexReq.lock();
             xreq.CopyFrom(mReq);
-            mbReplyUpdate = false;
+            mbReqUpdate = false;
             mmutexReq.unlock();
 
 
         }
+        else
+        {
+            continue;
+        }
 
 
         ClientContext context ;
@@ -95,24 +102,72 @@ void grpcfwclient::threadrpc()
           std::this_thread::sleep_for(std::chrono::milliseconds(900));
 
         }
+        mmutexReq.lock();
+        mbProc = false;
+        mmutexReq.unlock();
+
+        mcvreply.notify_all();
 
     }
 
 }
 
-void grpcfwclient::SetReq(iv::queryrpcReq & xreq)
+int grpcfwclient::SetReq(iv::queryrpcReq & xreq)
 {
     mmutexReq.lock();
+    if(mbProc)
+    {
+        mmutexReq.unlock();
+        std::cout<<" Have a reqest execting..."<<std::endl;
+        return 0;
+    }
+
     mReq.CopyFrom(xreq);
     mbReplyUpdate = false;
     mbReqUpdate = true;
     mnConnectFail = 0;
+    mbProc = true;
     mmutexReq.unlock();
     mcv.notify_all();
+
+    return 1;
 }
 
-int grpcfwclient::GetReply(iv::queryrpcReply & xReply)
+int grpcfwclient::GetReply(iv::queryrpcReply & xReply,int nwaitms)
 {
 
+    std::unique_lock<std::mutex> lk(mmutexcvreply);
+    if(mcvreply.wait_for(lk, std::chrono::milliseconds(nwaitms)) == std::cv_status::timeout)
+    {
+        lk.unlock();
+    }
+    else
+    {
+        lk.unlock();
+    }
+
+    if(mbProc)return 0;
+    else
+    {
+        if(mbReplyUpdate)
+        {
+            mmutexReply.lock();
+            xReply.CopyFrom(mReply);
+            mbReplyUpdate = false;
+            mmutexReply.unlock();
+            return 1;
+        }
+        else
+        {
+            if(mnConnectFail>0)
+            {
+                return mnConnectFail * (-1);
+            }
+            else
+            {
+                return -1000;
+            }
+        }
+    }
     return 0;
 }

+ 18 - 3
src/tool/fwupdate_admin/grpcfwclient.h

@@ -18,7 +18,7 @@ using grpc::Status;
 class grpcfwclient
 {
 public:
-    grpcfwclient();
+    grpcfwclient(std::string strserverip = "127.0.0.1",std::string strserverport = "19111");
     ~grpcfwclient();
 
 private:
@@ -34,6 +34,9 @@ private:
     std::condition_variable mcv;
     std::mutex mmutexcv;
 
+    std::condition_variable mcvreply;
+    std::mutex mmutexcvreply;
+
     std::mutex mmutexReq;
     std::mutex mmutexReply;
 
@@ -45,9 +48,21 @@ private:
     iv::queryrpcReply mReply;
     bool mbReplyUpdate = false;
 
+    bool mbProc = false;
+
 public:
-    void SetReq(iv::queryrpcReq & xreq);
-    int GetReply(iv::queryrpcReply & xReply);
+    /**
+     * @brief SetReq
+     * @param xreq
+     * @return 0 because sending, set request fail.   1 set request to sending.
+     */
+    int SetReq(iv::queryrpcReq & xreq);
+    /**
+     * @brief GetReply  Call this Function, need SetReq return 1. if SetReq return 0, not call this fuction
+     * @param xReply
+     * @return 0 sending, wait reply   <0 error, send faile.  1 get reply
+     */
+    int GetReply(iv::queryrpcReply & xReply,int nwaitms = 10);
 
 };
 

+ 93 - 10
src/tool/fwupdate_admin/mainwindowlogin.cpp

@@ -3,11 +3,15 @@
 
 #include <QMessageBox>
 
+#include "grpcfwclient.h"
 #include "mainwindow.h"
 #include "mainwindowlogin.h"
 
+#include "fwlogin.pb.h"
+
 extern MainWindow * gwmain;
 extern MainWindowLogin * gwlogin;
+extern grpcfwclient * gpgrpcfwclient;
 
 MainWindowLogin::MainWindowLogin(QWidget *parent) :
     QMainWindow(parent),
@@ -27,11 +31,9 @@ MainWindowLogin::MainWindowLogin(QWidget *parent) :
     "QCheckBox::indicator {width: 20px;height: 20px;border: none;image: url(:/no_display.png);}"
     "QCheckBox::indicator:checked {image: url(:/display.png);}");
 
-//    mpcheckdisplay->setGeometry(ui->lineEdit_password->pos().x() + 100,ui->lineEdit_password->pos().y() + 4,20,20);
-
     mpcheckdisplay->setGeometry(ui->lineEdit_password->size().width() - 30, (ui->lineEdit_password->size().height() -20)/2,20,20);
 
-    connect(this,SIGNAL(loginstate(int,char *)),this,SLOT(onloginstate(int,char *)));
+    connect(this,SIGNAL(loginstate(int,const char *)),this,SLOT(onloginstate(int,const char *)));
 
     setWindowTitle("Login");
 }
@@ -43,10 +45,44 @@ MainWindowLogin::~MainWindowLogin()
 
 void MainWindowLogin::on_pushButton_Login_clicked()
 {
-    ui->pushButton_Login->setText("Loging....");
-    ui->pushButton_Login->setEnabled(false);
 
-    mpthread = new std::thread(&MainWindowLogin::threadlogin,this);
+    if(ui->lineEdit_username->text().length()<1)
+    {
+        QMessageBox::warning(this,tr("Warning"),tr("username empty."),QMessageBox::YesAll);
+        return;
+    }
+
+    if(ui->lineEdit_password->text().length()<1)
+    {
+        QMessageBox::warning(this,tr("Waring"),tr("password empty."),QMessageBox::YesAll);
+        return;
+    }
+
+    iv::fw::loginreq xlogin;
+    xlogin.set_username(ui->lineEdit_username->text().toStdString());
+    xlogin.set_password(ui->lineEdit_password->text().toStdString());
+
+    iv::queryrpcReq xreq;
+    xreq.set_ntype(iv::CommonRPCType::LOGIN);
+    int ndatasize = xlogin.ByteSize();
+    xreq.set_ndatasize(ndatasize);
+    std::shared_ptr<char> pstr_ptr = std::shared_ptr<char>(new char[ndatasize]);
+    if(xlogin.SerializeToArray(pstr_ptr.get(),ndatasize))
+    {
+        xreq.set_data(pstr_ptr.get(),ndatasize);
+        ui->pushButton_Login->setText("Loging....");
+        ui->pushButton_Login->setEnabled(false);
+
+        mpthread = new std::thread(&MainWindowLogin::threadlogin,this,xreq);
+    }
+    else
+    {
+        QMessageBox::warning(this,tr("Warning"),tr("logingreq serialize fail."),QMessageBox::YesAll);
+        return;
+    }
+
+
+
 
 //    this->hide();
 //    gwmain->show();
@@ -62,7 +98,7 @@ void MainWindowLogin::slot_checkbox_state_changed(int state)
 
 }
 
-void MainWindowLogin::onloginstate(int nstate, char * strerr)
+void MainWindowLogin::onloginstate(int nstate,  const char * strerr)
 {
     if(nstate == 1)
     {
@@ -80,8 +116,55 @@ void MainWindowLogin::onloginstate(int nstate, char * strerr)
     }
 }
 
-void MainWindowLogin::threadlogin()
+void MainWindowLogin::threadlogin(iv::queryrpcReq  xreq)
 {
-    std::this_thread::sleep_for(std::chrono::milliseconds(3000));
-    emit loginstate(0,"password error");
+    int i;
+    for(i=0;i<3;i++)
+    {
+        int nreq = gpgrpcfwclient->SetReq(xreq);
+        if(nreq == 0)
+        {
+            std::cout<<"MainWindowLogin::threadlogin setreq fail."<<std::endl;
+            std::this_thread::sleep_for(std::chrono::milliseconds(10));
+            continue;
+        }
+        iv::queryrpcReply xReply;
+        int nreply = gpgrpcfwclient->GetReply(xReply);
+        while(nreply == 0)
+        {
+            nreply = gpgrpcfwclient->GetReply(xReply);
+        }
+        if(nreply<0)
+        {
+            emit loginstate(0,"can't connect to server");
+        }
+        else
+        {
+            if(xReply.ntype() == iv::CommonRPCType::LOGIN)
+            {
+                iv::fw::loginreply xLoginReply;
+                if(xLoginReply.ParseFromArray(xReply.data().data(),xReply.ndatasize()))
+                {
+                    if(xLoginReply.nauth() == 0)
+                    {
+                        emit loginstate(0,xLoginReply.errstr().data());
+                    }
+                    else
+                    {
+                        emit loginstate(1,"Login Success.");
+                    }
+                }
+                else
+                {
+                    emit loginstate(0,"Parse Reply Fail.");
+                }
+            }
+            else
+            {
+                emit loginstate(0,"login fail.");
+            }
+        }
+        break;
+    }
+
 }

+ 6 - 3
src/tool/fwupdate_admin/mainwindowlogin.h

@@ -5,6 +5,9 @@
 #include <QCheckBox>
 #include <thread>
 
+
+#include "commonrpc.pb.h"
+
 namespace Ui {
 class MainWindowLogin;
 }
@@ -18,17 +21,17 @@ public:
     ~MainWindowLogin();
 
 signals:
-    void loginstate(int nstate,char * strerr);
+    void loginstate(int nstate,const char * strerr);
 
 private slots:
     void on_pushButton_Login_clicked();
 
     void slot_checkbox_state_changed(int state);
 
-    void onloginstate(int nstate, char * strerr);
+    void onloginstate(int nstate, const char * strerr);
 
 private:
-    void threadlogin();
+    void threadlogin(iv::queryrpcReq xreq);
 
 private:
     Ui::MainWindowLogin *ui;

+ 59 - 0
src/tool/server_fwupdate/fwadminproc.cpp

@@ -0,0 +1,59 @@
+#include "fwadminproc.h"
+
+#include <iostream>
+
+fwadminproc::fwadminproc()
+{
+    mpfwdb = new fwdb();
+}
+
+fwadminproc::~fwadminproc()
+{
+    delete mpfwdb;
+}
+
+iv::queryrpcReply fwadminproc::ProcReq(const iv::queryrpcReq * req)
+{
+    iv::queryrpcReply xreply;
+    switch (req->ntype()) {
+    case iv::CommonRPCType::LOGIN:
+        xreply = ProcLogin(req);
+        break;
+    default:
+        break;
+    }
+    return xreply;
+}
+
+iv::queryrpcReply fwadminproc::ProcLogin(const iv::queryrpcReq *req)
+{
+    iv::queryrpcReply xreply;
+    iv::fw::loginreq xloginreq;
+    iv::fw::loginreply xloginreply;
+    if(xloginreq.ParseFromArray(req->data().data(),req->ndatasize()))
+    {
+        int nauth = mpfwdb->CheckAuth(xloginreq.username(),xloginreq.password());
+        xloginreply.set_nauth(nauth);
+        xloginreply.set_errstr("username or password error.");
+    }
+    else
+    {
+        xloginreply.set_nauth(0);
+        xloginreply.set_errstr("Parse Request Fail.");
+
+    }
+    xreply.set_ntype(iv::CommonRPCType::LOGIN);
+    int ndatasize = xloginreply.ByteSize();
+    std::shared_ptr<char> pstr_ptr = std::shared_ptr<char>(new char[ndatasize]);
+    if(xloginreply.SerializeToArray(pstr_ptr.get(),ndatasize))
+    {
+        xreply.set_ndatasize(ndatasize);
+        xreply.set_data(pstr_ptr.get(),ndatasize);
+    }
+    else
+    {
+        std::cout<<"fwadminproc::ProcLogin "<<std::endl;
+    }
+
+    return xreply;
+}

+ 26 - 0
src/tool/server_fwupdate/fwadminproc.h

@@ -0,0 +1,26 @@
+#ifndef FWADMINPROC_H
+#define FWADMINPROC_H
+
+#include "fwdb.h"
+
+#include "fwlogin.pb.h"
+#include "commonrpc.pb.h"
+
+class fwadminproc
+{
+public:
+    fwadminproc();
+    ~fwadminproc();
+
+public:
+
+    iv::queryrpcReply ProcReq(const iv::queryrpcReq * req);
+
+private:
+    iv::queryrpcReply ProcLogin(const iv::queryrpcReq * req);
+
+private:
+    fwdb * mpfwdb;
+};
+
+#endif // FWADMINPROC_H

+ 35 - 4
src/tool/server_fwupdate/fwdb.cpp

@@ -1,5 +1,7 @@
 #include "fwdb.h"
 
+#include <iostream>
+
 fwdb::fwdb()
 {
     mpthread = new std::thread(&fwdb::threaddb,this);
@@ -72,7 +74,7 @@ void fwdb::OpenDataBase()
     {
         if(!sql_query.exec("create table accountdata(id INTEGER primary key AUTOINCREMENT,username TEXT, password TEXT,"
                            "phone TEXT, passneedchange BOOL, passerrorcount INTEGER,"
-                           "usercreatetime DATETIME)"))
+                           "uservalidtime DATETIME, usercreatetime DATETIME)"))
         {
             qDebug() << "Error: Fail to create table."<< sql_query.lastError();
             return;
@@ -96,9 +98,9 @@ void fwdb::OpenDataBase()
         QDateTime now = QDateTime::currentDateTime();
 
         QString strnow = now.toString("yyyy-MM-dd hh:mm:ss");
-        snprintf(strsen,1000,"INSERT INTO accountdata(username,password,passerrorcount,usercreatetime)"
-                 " VALUES(\"%s\",\"%s\",%d,\"%s\")",
-                                     "admin","adc",0,strnow.toLatin1().data());
+        snprintf(strsen,1000,"INSERT INTO accountdata(username,password,passerrorcount,uservalidtime,usercreatetime)"
+                 " VALUES(\"%s\",\"%s\",%d,\"%s\",\"%s\")",
+                                     "admin","adc",0,"2099-12-31 23:59:59",strnow.toLatin1().data());
         sql_query.exec(strsen);
         qDebug("Create Table groupdata successfully.");
     }
@@ -108,3 +110,32 @@ void fwdb::threaddb()
 {
     OpenDataBase();
 }
+
+int fwdb::CheckAuth(std::string strusername, std::string strpassword)
+{
+    int nrtn = 0;
+    mmutexdb.lock();
+    QSqlQuery query(mdatabase);
+    char strsen[1000];
+    snprintf(strsen,1000,"select * from accountdata where((accountdata.username = \"%s\") &(accountdata.password = \"%s\"))",
+             strusername.data(),strpassword.data());
+    query.exec(strsen);
+    if(!query.exec(strsen))
+    {
+        std::cout<<query.lastError().text().data()<<std::endl;
+    }
+    else
+    {
+        while(query.next())
+        {
+            nrtn = 1;
+//            int id = query.value(0).toInt();
+//            qint64 recordid = query.value(1).toLongLong();
+//            mql_xvectorrecordtime.push_back(recordid);
+//            std::string strvehid = query.value(3).toString().toStdString();
+
+        }
+    }
+    mmutexdb.unlock();
+    return 1;
+}

+ 5 - 0
src/tool/server_fwupdate/fwdb.h

@@ -20,12 +20,17 @@ public:
     fwdb();
     ~fwdb();
 
+public:
+    int CheckAuth(std::string strusername,std::string strpassword);
+
 private:
     std::thread * mpthread;
     bool mbthreadrun = true;
     std::string mstrdbpath;
     QSqlDatabase mdatabase;
 
+    std::mutex mmutexdb;
+
 private:
     void threaddb();
     void OpenDataBase();

+ 10 - 1
src/tool/server_fwupdate/main.cpp

@@ -15,6 +15,8 @@
 
 #include "commonrpc.grpc.pb.h"
 
+#include "fwadminproc.h"
+
 using grpc::Server;
 using grpc::ServerBuilder;
 using grpc::ServerContext;
@@ -23,6 +25,9 @@ using grpc::Status;
 static std::unique_ptr<Server> gserver_grpc;
 static std::string gstrserver;
 
+
+fwadminproc * gpfwadminproc;
+
 #include "xmlparam.h"
 
 // Logic and data behind the server's behavior.
@@ -30,7 +35,9 @@ class CommonRPCServiceImpl final : public iv::CommonRPC::Service{
   Status rpcquery(ServerContext* context, const iv::queryrpcReq * request,
                   iv::queryrpcReply * reply) override {
 
-
+    iv::queryrpcReply xreply = gpfwadminproc->ProcReq(request);
+    reply = new iv::queryrpcReply;
+    reply->CopyFrom(xreply);
     return Status::OK;
   }
 
@@ -74,6 +81,8 @@ int main(int argc, char *argv[])
 {
     QApplication a(argc, argv);
 
+    fwadminproc xfwadmin;
+    gpfwadminproc = &xfwadmin;
 
     iv::xmlparam::Xmlparam xp("./server_fwupdate.xml");
 

+ 1 - 2
src/tool/server_fwupdate/mainwindow.cpp

@@ -12,14 +12,13 @@ MainWindow::MainWindow(QWidget *parent) :
     m_tcpserverFW->setMaxPendingConnections(2000);
     connect(m_tcpserverFW,SIGNAL(newConnection()), this,SLOT(newfwupdateConnect()));
 
-    mpfwdb = new fwdb();
+
 
     setWindowTitle("Shenlan Firmware update auth server.");
 }
 
 MainWindow::~MainWindow()
 {
-    delete mpfwdb;
     delete ui;
 }
 

+ 1 - 1
src/tool/server_fwupdate/mainwindow.h

@@ -47,7 +47,7 @@ private:
 
     QTcpServer * m_tcpserverFW;
 
-    fwdb * mpfwdb;
+
 
 };
 

+ 6 - 2
src/tool/server_fwupdate/server_fwupdate.pro

@@ -30,13 +30,17 @@ SOURCES += \
         mainwindow.cpp \
     fwdb.cpp \
     commonrpc.grpc.pb.cc \
-    ../../include/msgtype/commonrpc.pb.cc
+    ../../include/msgtype/commonrpc.pb.cc \
+    fwadminproc.cpp \
+    ../../include/msgtype/fwlogin.pb.cc
 
 HEADERS += \
         mainwindow.h \
     fwdb.h \
     commonrpc.grpc.pb.h \
-    ../../include/msgtype/commonrpc.pb.h
+    ../../include/msgtype/commonrpc.pb.h \
+    fwadminproc.h \
+    ../../include/msgtype/fwlogin.pb.h
 
 FORMS += \
         mainwindow.ui