Changeset 99 in pacpussensors for trunk/PacpusSocket
- Timestamp:
- Oct 15, 2015, 2:47:01 PM (9 years ago)
- Location:
- trunk/PacpusSocket
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/PacpusSocket/CMakeLists.txt
r85 r99 1 #########################quick start############################################ 2 #cmake ../ -G "CodeBlocks - Unix Makefiles" 3 #make 4 #make install 5 #make clean 6 ################################################################################ 1 7 project(PacpusSocket) 8 set(${PROJECT_NAME}_VERSION_MAJOR 0) 9 set(${PROJECT_NAME}_VERSION_MINOR 1) 10 add_definitions( -DPACPUSSOCKET_EXPORTS ) 2 11 ################################################################################ 3 add_definitions(-DPACPUSSOCKET_EXPORTS) 12 create_export(EXPORT_HDR ${PROJECT_NAME}) 13 pacpus_plugin(PLUGIN_CPP PLUGIN_HDR ${PROJECT_NAME}) 14 add_definitions (${QT_DEFINITIONS}) 4 15 5 # ======================================== 6 # Configure qt4 7 # ======================================== 8 if(QT4_FOUND) 9 set(QT_USE_QTXML true) 10 set(QT_USE_QTNETWORK true) 11 include(${QT_USE_FILE}) 12 else() 13 message(ERROR "Qt4 needed") 14 endif() 15 16 # ======================================== 17 # Compiler definitions 18 # ======================================== 19 add_definitions( 20 ${QT_DEFINITIONS} 21 ) 22 23 # ======================================== 24 # Include directories 25 # ======================================== 26 include_directories( 27 ${PROJECT_BINARY_DIR} 28 ${QT_INCLUDE_DIR} 16 find_package(Qt5Network REQUIRED) 17 ################################################################################ 18 # DIRECTORIES 19 include_directories( 20 ${PROJECT_BINARY_DIR} 21 ${QT_INCLUDE_DIR} 22 ${PACPUS_INCLUDE_DIR} 23 ${PACPUS_INCLUDE_DIR}/Pacpus/ 29 24 ) 30 25 … … 32 27 # Link directories 33 28 # ======================================== 34 link_directories( 29 link_directories( 30 ${PACPUS_LIB_DIR} 35 31 ) 36 32 37 pacpus_plugin(PLUGIN_CPP PLUGIN_H ${PROJECT_NAME} )38 33 39 # ======================================== 40 # List of sources 41 # ======================================== 42 set( 43 PROJECT_SRCS 44 PacpusSocketAirplug.cpp 45 PacpusSocketAirplug.h 46 PacpusUDPSocket.cpp 47 PacpusUDPSocket.h 34 ################################################################################ 35 # FILES 36 set(PROJECT_HDRS 37 ${EXPORT_HDR} 38 PacpusSocketAirplug.h 39 PacpusUDPSocket.h 40 ) 41 set(PROJECT_SRCS 48 42 ${PLUGIN_CPP} 43 PacpusSocketAirplug.cpp 44 PacpusUDPSocket.cpp 49 45 ) 50 46 51 # ======================================== 52 # Files to MOC 53 # ======================================== 54 set( 55 FILES_TO_MOC 47 set(FILES_TO_MOC 48 ${PLUGIN_HDR} 56 49 PacpusSocketAirplug.h 57 PacpusUDPSocket.h 58 ${PLUGIN_H} 50 PacpusUDPSocket.h 59 51 ) 60 52 61 set( 62 UI_FILES 63 53 set(UI_FILES 64 54 ) 65 55 66 # ======================================== 67 # Call MOC 68 # ======================================== 69 qt4_wrap_cpp( 70 PROJECT_MOC_SRCS 56 ################################################################################ 57 # Qt: call moc, uic 58 qt_wrap_cpp(PROJECT_MOC_SRCS 71 59 ${FILES_TO_MOC} 72 60 ) 73 61 74 qt4_wrap_ui( 75 PROJECT_UI_SRCS 62 qt_wrap_ui(PROJECT_UI_SRCS 76 63 ${UI_FILES} 77 64 ) 78 65 79 # ======================================== 80 # Build a library 81 # ======================================== 82 pacpus_add_library( 83 ${PROJECT_NAME} SHARED 66 ################################################################################ 67 # BUILD and LINK 68 pacpus_add_library(${PROJECT_NAME} SHARED 69 ${PROJECT_HDRS} 84 70 ${PROJECT_SRCS} 85 71 ${PROJECT_MOC_SRCS} … … 87 73 ) 88 74 75 89 76 # ======================================== 90 77 # Libraries 91 78 # ======================================== 92 # All the platform 93 target_link_libraries( 94 ${PROJECT_NAME} 95 ${PACPUS_LIBRARIES} 96 ${QT_LIBRARIES} 97 ${PACPUS_DEPENDENCIES_LIB} 79 set(LIBS 80 optimized FileLib debug FileLib_d 81 optimized PacpusLib debug PacpusLib_d 82 optimized PacpusTools debug PacpusTools_d 98 83 ) 84 if (WIN32) 85 list(APPEND LIBS 86 optimized ROAD_TIME debug ROAD_TIME_d 87 ) 88 endif() 89 # LINK 90 target_link_libraries(${PROJECT_NAME} 91 ${PACPUS_LIBRARIES} 92 ${PACPUS_DEPENDENCIES_LIB} 93 ${LIBS} 94 ${QT_LIBRARIES} 95 ) 96 qt5_use_modules(${PROJECT_NAME} Network) 97 ################################################################################ 98 # FOLDERS 99 pacpus_folder(${PROJECT_NAME} "components") 100 ################################################################################ 101 # INSTALL 102 pacpus_install(${PROJECT_NAME}) 99 103 100 # ======================================== 101 # Install 102 # ======================================== 103 pacpus_install(${PROJECT_NAME}) 104 install( 105 DIRECTORY ${PacpusSocket_SOURCE_DIR} 106 DESTINATION ${PACPUS_INSTALL_DIR}/include 107 FILES_MATCHING PATTERN "*.h" 108 ) -
trunk/PacpusSocket/PacpusUDPSocket.cpp
r87 r99 5 5 // author: Pierre Hudelaine 6 6 // Copyright Heudiasyc UMR UTC/CNRS 7253 7 // 7 // 8 8 // version: $Id: $ 9 9 // … … 12 12 *********************************************************************/ 13 13 14 #include "PacpusUDPSocket.h" 14 #include "PacpusUDPSocket.h" 15 15 16 16 #include <qbuffer.h> … … 27 27 // Construct the factory 28 28 ////////////////////////////////////////////////////////////////////////// 29 static ComponentFactory <PacpusUDPSocket> sFactory("PacpusUDPSocket"); 29 static ComponentFactory <PacpusUDPSocket> sFactory("PacpusUDPSocket"); 30 30 31 31 … … 34 34 ////////////////////////////////////////////////////////////////////////// 35 35 PacpusUDPSocket::PacpusUDPSocket(QString name) 36 : ComponentBase(name) 37 { 38 } 36 : ComponentBase(name) 37 { 38 } 39 39 40 40 … … 42 42 // Destructor 43 43 ////////////////////////////////////////////////////////////////////////// 44 PacpusUDPSocket::~PacpusUDPSocket() 44 PacpusUDPSocket::~PacpusUDPSocket() 45 45 { 46 46 } … … 52 52 void PacpusUDPSocket::addOutputs() 53 53 { 54 addOutput<QString, PacpusUDPSocket>(" udpSocketOutput");54 addOutput<QString, PacpusUDPSocket>("string"); 55 55 } 56 56 … … 61 61 void PacpusUDPSocket::addInputs() 62 62 { 63 addInput<QString, PacpusUDPSocket>("udpSocketInput", &PacpusUDPSocket::sendDatagrams);64 } 65 66 67 ////////////////////////////////////////////////////////////////////////// 68 // Called by the ComponentManager to pass the XML parameters to the 69 // component 70 ////////////////////////////////////////////////////////////////////////// 71 ComponentBase::COMPONENT_CONFIGURATION PacpusUDPSocket::configureComponent(XmlComponentConfig config) 72 { 63 addInput<QString, PacpusUDPSocket>("string", &PacpusUDPSocket::sendQString); 64 } 65 66 67 ////////////////////////////////////////////////////////////////////////// 68 // Called by the ComponentManager to pass the XML parameters to the 69 // component 70 ////////////////////////////////////////////////////////////////////////// 71 ComponentBase::COMPONENT_CONFIGURATION PacpusUDPSocket::configureComponent(XmlComponentConfig config) 72 { 73 73 74 74 socketType_ = config.getProperty("typeSocket"); 75 75 76 76 if (socketType_ == "client" || socketType_ == "Client") 77 77 { 78 78 address2send_.setAddress(config.getProperty("address")); 79 79 port2send_ = config.getProperty("port").toUInt(); 80 socketType_ = "client"; 80 socketType_ = "client"; 81 81 } 82 82 else if (socketType_ == "server" || socketType_ == "Server") … … 92 92 socketType_ = "client"; 93 93 } 94 95 return ComponentBase::CONFIGURED_OK; 96 } 94 95 return ComponentBase::CONFIGURED_OK; 96 } 97 97 98 98 … … 100 100 // Called by the ComponentManager to start the component 101 101 ////////////////////////////////////////////////////////////////////////// 102 void PacpusUDPSocket::startActivity() 103 { 102 void PacpusUDPSocket::startActivity() 103 { 104 104 if (!(udpSocket_ = new QUdpSocket())) 105 105 qFatal("Failed to init UDP socket!"); 106 106 107 107 if (socketType_ == "server") 108 108 { … … 112 112 qWarning() << "Failed to bind socket for server on port" << port2bind_; 113 113 } 114 114 115 115 connect(udpSocket_, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams())); 116 117 u pdSocketOutput_ = getTypedOutput<QString, PacpusUDPSocket>("udpSocketOutput");116 117 udpSocketOutput_ = getTypedOutput<QString, PacpusUDPSocket>("string"); 118 118 } 119 119 … … 122 122 // Send datagram QString 123 123 ////////////////////////////////////////////////////////////////////////// 124 void PacpusUDPSocket::send Datagrams(QString frame)125 { 124 void PacpusUDPSocket::sendQString(const QString& frame) 125 { 126 126 int sent=0; 127 127 128 128 if (socketType_ == "client") 129 129 { 130 if ((sent = udpSocket_->writeDatagram(frame.toLocal8Bit(), address2send_, port2send_)) == -1) 131 qDebug() << "Failed to send the frame: " << address2send_ << port2send_ << frame << endl; 130 if ((sent = udpSocket_->writeDatagram(frame.toLocal8Bit(), address2send_, port2send_)) == -1) 131 qDebug() << "Failed to send the frame: " << address2send_ << port2send_ << frame << endl; 132 132 } 133 133 else if (socketType_ == "server") … … 135 135 for (int i = 0; i < listClients.size(); i++) 136 136 { 137 if ((sent = udpSocket_->writeDatagram(frame.toLocal8Bit(), listClients[i]->getAddress(), listClients[i]->getPort())) == -1) 138 qDebug() << "Failed to send the frame: " << listClients[i]->getAddress() << listClients[i]->getPort() << frame << endl; 137 if ((sent = udpSocket_->writeDatagram(frame.toLocal8Bit(), listClients[i]->getAddress(), listClients[i]->getPort())) == -1) 138 qDebug() << "Failed to send the frame: " << listClients[i]->getAddress() << listClients[i]->getPort() << frame << endl; 139 139 } 140 140 } 141 } 141 } 142 142 143 143 … … 145 145 // Send datagram QByteArray 146 146 ////////////////////////////////////////////////////////////////////////// 147 void PacpusUDPSocket::sendDatagrams(QByteArray frame) 148 { 147 void PacpusUDPSocket::sendDatagrams(QByteArray frame) 148 { 149 149 int sent=0; 150 150 151 151 if (socketType_ == "client") 152 152 { 153 if ((sent = udpSocket_->writeDatagram(frame, address2send_, port2send_)) == -1) 154 qDebug() << "Failed to send the frame: " << address2send_ << port2send_ << frame << endl; 153 if ((sent = udpSocket_->writeDatagram(frame, address2send_, port2send_)) == -1) 154 qDebug() << "Failed to send the frame: " << address2send_ << port2send_ << frame << endl; 155 155 } 156 156 else if (socketType_ == "server") … … 158 158 for (int i = 0; i < listClients.size(); i++) 159 159 { 160 if ((sent = udpSocket_->writeDatagram(frame, listClients[i]->getAddress(), listClients[i]->getPort())) == -1) 161 qDebug() << "Failed to send the frame: " << listClients[i]->getAddress() << listClients[i]->getPort() << frame << endl; 160 if ((sent = udpSocket_->writeDatagram(frame, listClients[i]->getAddress(), listClients[i]->getPort())) == -1) 161 qDebug() << "Failed to send the frame: " << listClients[i]->getAddress() << listClients[i]->getPort() << frame << endl; 162 162 } 163 163 } 164 } 165 166 167 ////////////////////////////////////////////////////////////////////////// 168 // Called when the socket receive a new datagram 164 } 165 166 167 ////////////////////////////////////////////////////////////////////////// 168 // Called when the socket receive a new datagram 169 169 ////////////////////////////////////////////////////////////////////////// 170 170 void PacpusUDPSocket::readPendingDatagrams() 171 171 { 172 while (udpSocket_->hasPendingDatagrams()) 173 { 174 QByteArray datagram; 175 datagram.resize(udpSocket_->pendingDatagramSize()); 176 QHostAddress sender; 172 while (udpSocket_->hasPendingDatagrams()) 173 { 174 QByteArray datagram; 175 datagram.resize(udpSocket_->pendingDatagramSize()); 176 QHostAddress sender; 177 177 quint16 senderPort; 178 178 179 179 if(udpSocket_->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort) != -1) 180 { 180 { 181 181 if (socketType_ == "server") 182 182 { 183 183 bool flag = false; 184 185 for (int i = 0; i < listClients.size(); i++) 186 { 184 185 for (int i = 0; i < listClients.size(); i++) 186 { 187 187 if (listClients[i]->getAddress() == sender && listClients[i]->getPort() == senderPort) 188 188 flag = true; 189 189 } 190 190 191 191 if (flag == false) 192 192 listClients << new Client(sender, senderPort); 193 193 } 194 195 if (u pdSocketOutput_ && updSocketOutput_->hasConnection())196 u pdSocketOutput_->send(QString(datagram.data()));194 195 if (udpSocketOutput_ && udpSocketOutput_->hasConnection()) 196 udpSocketOutput_->send(QString(datagram.data())); 197 197 } 198 198 else -
trunk/PacpusSocket/PacpusUDPSocket.h
r87 r99 5 5 // author: Pierre Hudelaine 6 6 // Copyright Heudiasyc UMR UTC/CNRS 7253 7 // 7 // 8 8 // version: $Id: $ 9 9 // … … 15 15 16 16 #include "Pacpus/kernel/Log.h" 17 #include "Pacpus/kernel/ComponentBase.h" 18 #include "Pacpus/kernel/ComponentFactory.h" 17 #include "Pacpus/kernel/ComponentBase.h" 18 #include "Pacpus/kernel/ComponentFactory.h" 19 19 20 20 // Export macro for PacpusSocket DLL for Windows only … … 28 28 # endif 29 29 #else 30 // On other platforms, simply ignore this 31 # define PACPUSSOCKET_API 30 // On other platforms, simply ignore this 31 # define PACPUSSOCKET_API 32 32 #endif 33 33 … … 38 38 class PACPUSSOCKET_API Client 39 39 { 40 public: 40 public: 41 41 /// Constructor 42 42 Client(); … … 44 44 /// Destructor 45 45 ~Client(); 46 46 47 47 QHostAddress address_; 48 48 quint16 port_; 49 50 QHostAddress getAddress() {return address_;}; 49 50 QHostAddress getAddress() {return address_;}; 51 51 quint16 getPort() {return port_;}; 52 52 }; … … 54 54 class PACPUSSOCKET_API PacpusUDPSocket 55 55 : public QObject 56 , public ComponentBase 57 { 56 , public ComponentBase 57 { 58 58 Q_OBJECT 59 59 60 public: 60 public: 61 61 /// Constructor 62 62 PacpusUDPSocket(QString name); … … 71 71 72 72 /// Slot d'envoie des paquets 73 void send Datagrams(QStringframe);73 void sendQString(const QString& frame); 74 74 void sendDatagrams(QByteArray frame); 75 75 76 76 Q_SIGNALS: 77 77 void newDatagram(QByteArray datagram); … … 83 83 virtual void stopActivity(); 84 84 virtual COMPONENT_CONFIGURATION configureComponent(XmlComponentConfig config); 85 86 85 86 virtual void addInputs(); 87 87 virtual void addOutputs(); 88 89 88 89 OutputInterface<QString, PacpusUDPSocket>* udpSocketOutput_; 90 90 91 91 QUdpSocket * udpSocket_; … … 94 94 quint16 port2send_; 95 95 QHostAddress address2send_; 96 QString socketType_; 97 96 QString socketType_; 97 98 98 QList <Client *> listClients; 99 99 };
Note:
See TracChangeset
for help on using the changeset viewer.