-cmake_minimum_required(VERSION 2.6)
+cmake_minimum_required(VERSION 2.8)
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})
-#
+if(CMAKE_BUILD_TYPE STREQUAL Debug)
+ message(STATUS "Debugging mode is being enabled. This is not recommended for normal use.")
+ add_definitions(-DDEBUG)
+ set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -Wall)
+endif(CMAKE_BUILD_TYPE STREQUAL Debug)
#No mocs
set(main main.cxx settings.cxx)
set(validator
delete socket;
return;
}
- emit (emit this->pending(*socket))?this->newConnection():socket->disconnectFromHost();
+ emit (emit this->pending(*socket))?this->newConnection():socket->abort();
}
tcpSocket::tcpSocket(quint64 buffersize, QObject *parent):QTcpSocket(parent){
}
void tcpSocket::read_data(){
while(this->bytesAvailable()>0){
+#ifdef DEBUG
qDebug()<<"Server Event Mode:"<<this->event<<"Server Available bytes:"<<this->bytesAvailable();
+#endif
switch(this->event){
case tcpSocket::headsize: this->size_event(); break;
case tcpSocket::header_receive: this->header_event(); break;
this->event=tcpSocket::data;
}
void tcpSocket::data_event(){
- if(this->bytesAvailable()<this->header_data().datasize()) return;
+ if((quint64)this->bytesAvailable()<this->header_data().datasize()) return;
quint64 final_readsize=this->head_data.datasize()%this->buffer_size,
read_count=(this->head_data.datasize()-final_readsize)/this->buffer_size;
tmp_buffer=msg.toUtf8();
QBuffer memoryStream(&tmp_buffer,this);
if(!memoryStream.open(QIODevice::ReadOnly)){
- this->setErrorString("Memory Stream couldn't open.");
+ this->setErrorString(tr("Memory Stream couldn't open."));
emit this->error(QAbstractSocket::UnknownSocketError);
return (*this);
}
emit this->sentData();
return (*this);
}
-tcpSocket &tcpSocket::operator<<(QFile &file){
+tcpSocket &tcpSocket::operator<<(QFile &src_file){
+ QFile file(src_file.fileName(),this);
if(!this->state()!=QAbstractSocket::ConnectedState)return (*this);
this->head_data=header(this->senderName,QFileInfo(file));
QByteArray tmp_buffer;
this->write(tmp_buffer);
if(!this->flush())return (*this);
tmp_buffer.clear();
- while(this->write(file.read(this->buffer_size))>0)
+ emit this->file_header_sent();
+ if(!file.open(QIODevice::ReadOnly)){
+ this->setErrorString(tr("The file stream couldn't open."));
+ emit this->error(QAbstractSocket::UnknownSocketError);
+ return (*this);
+ }
+ while(this->write(file.read(this->buffer_size))>0&&!this->check_canceled_then_abort()){
if(!this->flush())return (*this);
+ emit this->sending_file_progress(file.pos());
+ }
emit this->sentData();
return (*this);
}
tcpSocket(const QString &senderName,quint64 buffersize=default_buffer_size,QObject *parent=NULL);
QString path_to_save() const;
structures::header header_data() const;
-
+ //Calling operator<<(QFile), the specified file stream will be copied.
tcpSocket &operator<<(const QString &),
&operator<<(QFile &);
signals:
void file_receive_progress(const quint64 streamPos) const;
void file_saved() const;
+
void sentData();
+ void file_header_sent();
+ void sending_file_progress(const quint64 pos);
private slots:
void read_data();
void cancel();
}
}
void mainWindow::tcpclient_error(const QAbstractSocket::SocketError &error){
+ Q_UNUSED(error);
tcpSocket *client=qobject_cast<tcpSocket *>(this->sender());
+#ifdef DEBUG
qDebug()<<"Error:"<<client->errorString();
+#endif
client->close();
}
//Implementations for main server.
bool mainWindow::tcpserver_pending(const tcpSocket &socket){
+#ifdef DEBUG
+ qDebug()<<"Pending:"<<socket.peerAddress().toString()<<" Port:"<<socket.peerPort();
+#endif
if(!this->isInMember(AddressAndPort(socket.peerAddress(),0),true)) return false;
connect(&socket,
SIGNAL(msg_received(const QString &)),
return true;
}
void mainWindow::tcpserver_error(const QAbstractSocket::SocketError &error){
+ Q_UNUSED(error);
tcpSocket *sender=qobject_cast<tcpSocket *>(this->sender());
+#ifdef DEBUG
qDebug()<<"Receive aborted:"<<sender->errorString();
+#endif
+ sender->close();
}
void mainWindow::tcpserver_msg_received(const QString &msg){
tcpSocket *sender=qobject_cast<tcpSocket *>(this->sender());
- this->receiveText->append(sender->header_data().senderName()+"("+sender->peerAddress().toString()+")"+tr(" says:"));
- this->receiveText->append(msg+"\n");
- //this->receiveText->append("<br />");
+ this->receiveText->append(sender->header_data().senderName()+"("+this->memberList->name(AddressAndPort(sender->peerAddress(),0),true)+", "+sender->peerAddress().toString()+")"+tr(" says:"));
+ this->receiveText->append(msg);
+ this->receiveText->append("<br />");
}
}
return ret;
}
-QStringList MemberList::name() const{
- QStringList ret;
- for(int row=0;row<this->rowCount();row++) ret<<this->item(row,NameIndex)->text();
- return ret;
+int MemberList::isInMember(const AddressAndPort &peer,bool matchIPOnly)const{
+ 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<<QString("%1").arg(default_port);
+ if(address_port_str[1].isEmpty()) address_port_str[1]=QString("%1").arg(default_port);
+ if(matchIPOnly&&address_port_str[0]==peer.first.toString()) return row;
+ else if(address_port_str[0]==peer.first.toString()&&
+ address_port_str[1]==QString::number(peer.second))
+ return row;
+ }
+ return -1;
+}
+QString MemberList::name(const AddressAndPort &addressport, bool matchAddressOnly) const{
+ int index=this->isInMember(addressport,matchAddressOnly);
+ return this->item(index,NameIndex)->text();
}
void MemberList::remove_selected(){
while(this->selectedItems().size()>0&&this->selectedRanges()[0].rowCount()>0)
void remove_selected();
void remove_all_contents();
QList<AddressAndPort> addressPortList() const;
- QStringList name() const;
+ QString name(const AddressAndPort&,bool) const;
+ int isInMember(const AddressAndPort &peer,bool matchIPOnly)const;
friend QDataStream &operator<<(QDataStream &out,const MemberList &value){
QHash<QString,AddressAndPort> AddressList;
for(int row=0;row<value.rowCount();row++){
this->italic->setCheckable(true);
this->link->setCheckable(true);
this->color->setCheckable(true);
+#ifdef DEBUG
qDebug()<<"Rtf editor has initialized. When you press any key(s), keysym will be output to stdout.";
+#endif
}
void RtfEditor::selectionChnged(){
this->bold->setChecked(this->editor->currentFont().bold());
event->accept();
}
void RtfEditor::keyPressEvent(QKeyEvent *event){
+#ifdef DEBUG
qDebug()<<hex<<event->modifiers()<<":"<<event->key();
+#endif
if(event->modifiers()==Qt::ControlModifier&&(event->key()==Qt::Key_Enter||event->key()==Qt::Key_Return)) emit this->sendTriggered();
event->accept();
}