OSDN Git Service

Implemented server and clients.
authorHiroaki Yamamoto <admin@hysoftware.net>
Wed, 6 Oct 2010 12:04:41 +0000 (21:04 +0900)
committerHiroaki Yamamoto <admin@hysoftware.net>
Wed, 6 Oct 2010 12:04:41 +0000 (21:04 +0900)
TcpServer And TcpClients has been implemented, but debugging must be needed (Because current implementation has bugs).

CMakeLists.txt
ui/jasmine_mainwindow.cxx
ui/jasmine_mainwindow.h
ui/memberlist.cxx
ui/memberlist.h

index 39ce020..874a6a5 100644 (file)
@@ -1,6 +1,7 @@
 cmake_minimum_required(VERSION 2.6)
 project(jasmine)
 find_package(Qt4 4.5 REQUIRED QtCore QtGui QtNetwork)
+find_package(OpenMP)
 add_definitions(${QT_DEFINITIONS})
 include_directories(${CMAKE_CURRENT_BINARY_DIR} ${QT_INCLUDE_DIR})
 include(${QT_USE_FILE})
@@ -67,11 +68,20 @@ qt4_wrap_cpp(ui_moc ${ui_h})
 qt4_wrap_ui(ui_moc ${ui})
 qt4_add_resources(res_moc ${res})
 if(WIN32)
-       add_executable(jasmine WIN32
+       if(OPENMP_FOUND)
+               add_executable(jasmine WIN32
                        ${main} ${ui_moc} ${ui_src} ${serverclient_moc}
                        ${serverclient_src} ${structures} ${res_moc}
                        ${validator} ${ported_rmd6})
-       target_link_libraries(jasmine ${QT_LIBRARIES})
+               set_property(TARGET jasmine PROPERTY COMPLILE_FLAGS ${OpenMP_CXX_FLAGS} APPEND)
+               target_link_libraries(jasmine gomp ${QT_LIBRARIES})
+       else(OPENMP_FOUND)
+               add_executable(jasmine WIN32
+                       ${main} ${ui_moc} ${ui_src} ${serverclient_moc}
+                       ${serverclient_src} ${structures} ${res_moc}
+                       ${validator} ${ported_rmd6})
+               target_link_libraries(jasmine ${QT_LIBRARIES})
+       endif(OPENMP_FOUND)
 else(WIN32)
        add_library(qripemd160 SHARED ${ported_rmd6})
        add_library(validator SHARED ${validator})
@@ -93,4 +103,8 @@ else(WIN32)
        target_link_libraries(ui resource serverclient ${QT_LIBRARIES})
 
        target_link_libraries(jasmine ui ${QT_LIBRARIES})
+       if(OPENMP_FOUND)
+               set_property(TARGET ui PROPERTY COMPILE_FLAGS ${OpenMP_CXX_FLAGS} APPEND)
+               target_link_libraries(ui gomp)
+       endif(OPENMP_FOUND)
 endif(WIN32)
index 0e790fe..f10bb49 100644 (file)
 #include <QDataStream>
 #include <QPair>
 
+#ifdef _OPENMP
+#include <omp.h>
+#endif
+
 using namespace structures;
 using namespace network;
 mainWindow::mainWindow(){
@@ -48,6 +52,8 @@ mainWindow::mainWindow(){
        connect(this->receiveText,SIGNAL(highlighted(const QUrl &)),SLOT(selectedLink(const QUrl &)));
        connect(this->sendTextEditor,SIGNAL(invalidLink(const QString &)),SLOT(invalidLink(const QString &)));
        connect(this->sendTextEditor,SIGNAL(sendTriggered()),this->sendButton,SLOT(click()));
+
+       connect(this->mainServer,SIGNAL(pending(const serverSocket &)),SLOT(tcpserver_pending(const serverSocket &)));
 }
 mainWindow::~mainWindow(){}
 void mainWindow::closeEvent(QCloseEvent *event){
@@ -60,16 +66,24 @@ void mainWindow::showEvent(QShowEvent *event){
                this->status->showMessage("<font color=\"#ff0000\">Don't forget almost all features are not implemented!</font>",default_status_interval);
        event->accept();
 }
-int mainWindow::isInMember(const AddressAndPort &peer){
-       for(int row=0;row<this->memberList->rowCount();row++){
-               QStringList address_port=this->memberList->item(row,1)->text().split(":");
-               if(peer==AddressAndPort(QHostAddress(address_port[0]),(address_port.count()==2)?address_port[1].toUShort():default_port)) return row;
+bool mainWindow::isInMember(const AddressAndPort &peer,bool matchIPOnly){
+       QList<AddressAndPort> iplist=this->memberList->addressPortList();
+       foreach(AddressAndPort addressPort,iplist){
+               if(matchIPOnly&&peer.first==addressPort.first) return true;
+               else if(peer==addressPort) return true;
        }
-       return -1;
+       return false;
 }
+
 //Client behavior
 void mainWindow::on_sendButton_clicked(){
-       //TODO:Make clients and make sending process.
+       QList<AddressAndPort> addressList=this->memberList->addressPortList();
+       QVector<tcpClient *> clients(addressList.size());
+       foreach(tcpClient *client,clients) client=new tcpClient(default_buffer_size,this->setting.name(),this);
+#ifdef _OMP
+#pragma omp parallel for
+#endif
+       for(int index=0;index<addressList.size();index++) (*clients[index])<<this->sendTextEditor->html();
 }
 void mainWindow::on_sendFileAction_triggered(){
        //TODO:Send files
@@ -260,3 +274,17 @@ void mainWindow::selectedLink(const QUrl &link){
        this->status->showMessage(link.toString(QUrl::None));
 }
 settings mainWindow::app_setting() const{return this->setting;}
+
+//Implementations for main server.
+bool mainWindow::tcpserver_pending(const serverSocket &socket){
+       if(!this->isInMember(AddressAndPort(socket.peerAddress(),0),true)) return false;
+       connect(&socket,
+                       SIGNAL(msg_received(const serverSocket &,const QString &)),
+                       SLOT(tcpserver_msg_received(const serverSocket &,const QString &)));
+       return true;
+}
+void mainWindow::tcpserver_msg_received(const serverSocket &socket,const QString &msg){
+       this->receiveText->append(socket.header_data().senderName()+tr(" says:")+"<br />");
+       this->receiveText->append(msg);
+       this->receiveText->append("<br />");
+}
index a1aa824..37d9e86 100644 (file)
@@ -6,6 +6,7 @@
 #include "../settings.h"
 #include "../validator/ipaddressvalidator.h"
 #include "../server/tcpserver.h"
+#include "../client/tcpclient.h"
 
 using namespace structures;
 using namespace network;
@@ -44,6 +45,10 @@ private slots:
        //These slots are for receiveText.
        void invalidLink(const QString &);
        void selectedLink(const QUrl &);
+       //These functions are for tcpclient.
+       //These functions are for tcpserver.
+       bool tcpserver_pending(const serverSocket &);
+       void tcpserver_msg_received(const serverSocket &,const QString &);
 public slots:
        //These are for opening stuff.
        void openConfig(const QString &);
@@ -62,7 +67,7 @@ private:
        SettingDialog   *settingdialog;
        settings                setting;
 
-       int isInMember(const AddressAndPort &);
+       bool isInMember(const AddressAndPort &,bool matchIPOnly);
        void configAndShowSettingDialog(const SettingDialog::tab);
 public:
        settings app_setting() const;
index dc0cc67..37d49d7 100644 (file)
@@ -1,6 +1,9 @@
 #include "editdelegate.h"
 #include "memberlist.h"
 
+#define AddressIndex 1
+#define NameIndex 0
+
 MemberList::MemberList(QWidget *parent):QTableWidget(parent){
        this->verticalHeader()->setVisible(false);
        this->verticalHeader()->setDefaultSectionSize(15);
@@ -13,6 +16,21 @@ MemberList::MemberList(QWidget *parent):QTableWidget(parent){
        this->horizontalHeader()->setVisible(true);
        this->setItemDelegateForColumn(1,new MemberListEditDelegate(this));
 }
+QList<AddressAndPort> MemberList::addressPortList() const{
+       QList<AddressAndPort> ret;
+       for(int row=0;row<this->rowCount();row++){
+               QStringList address_port_str=this->item(row,AddressIndex)->text().split(":");
+               if(address_port_str.size()<2||address_port_str[1].isEmpty()) address_port_str[1]=QString("%1").arg(default_port);
+               AddressAndPort address_port(QHostAddress(address_port_str[0]),address_port_str[1].isEmpty()?default_port:address_port_str[1].toUShort());
+               ret<<address_port;
+       }
+       return ret;
+}
+QStringList MemberList::name() const{
+       QStringList ret;
+       for(int row=0;row<this->rowCount();row++) ret<<this->item(row,NameIndex)->text();
+       return ret;
+}
 void MemberList::remove_selected(){
        while(this->selectedItems().size()>0&&this->selectedRanges()[0].rowCount()>0)
                this->removeRow(this->selectedRanges()[0].topRow());
index 226ed2c..1494216 100644 (file)
@@ -9,6 +9,8 @@ public:
 public slots:
        void remove_selected();
        void remove_all_contents();
+       QList<AddressAndPort> addressPortList() const;
+       QStringList name() const;
        friend QDataStream &operator<<(QDataStream &out,const MemberList &value){
                QHash<QString,AddressAndPort> AddressList;
                for(int row=0;row<value.rowCount();row++){