From 693b9a448a07b07cf5e257c15f54a6a50f391d0c Mon Sep 17 00:00:00 2001 From: me_desk Date: Sun, 4 May 2014 22:27:29 +0000 Subject: initial minimal working file transfer implementation, new class FileTransferController git-svn-id: svn://localhost/project_picapica/trunk@306 fb96df88-216d-11de-830c-f3023f12d8ac --- pica-client/skynet.cpp | 161 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 154 insertions(+), 7 deletions(-) (limited to 'pica-client/skynet.cpp') diff --git a/pica-client/skynet.cpp b/pica-client/skynet.cpp index e868903..1077df2 100644 --- a/pica-client/skynet.cpp +++ b/pica-client/skynet.cpp @@ -23,7 +23,12 @@ PICA_client_callbacks cbs = { notfound_cb, channel_closed_cb, nodelist_cb, - peer_cert_verify_cb + peer_cert_verify_cb, + accept_file_cb, + accepted_file_cb, + denied_file_cb, + file_progress, + file_control }; PICA_client_init(&cbs); @@ -70,7 +75,14 @@ void SkyNet::nodethread_connected(QString addr, quint16 port, NodeThread *thread msgqueues = h.GetUndeliveredMessages(); } - c2c_peer_ids = msgqueues.keys(); + //load file transfers to be completed + // sndfilequeues = ... + + QMap > queues = msgqueues; + + queues.unite(sndfilequeues); + + c2c_peer_ids = queues.uniqueKeys(); for (int i = 0; i < c2c_peer_ids.size(); i++) { @@ -157,7 +169,10 @@ void SkyNet::timerEvent(QTimerEvent *e) { if (write_mutex.tryLock(10)) { - QList c2c_peer_ids = msgqueues.keys(); + QMap > queues = msgqueues; + queues.unite(sndfilequeues); + + QList c2c_peer_ids = queues.uniqueKeys(); c2c_peer_ids = filter_existing_chans(c2c_peer_ids); @@ -221,6 +236,68 @@ bool SkyNet::isSelfAware() return self_aware; } +void SkyNet::SendFile(QByteArray to, QString filepath) +{ +int ret = PICA_OK; +struct PICA_chaninfo *iptr; + +write_mutex.lock();//<< + +if ( (iptr = find_active_chan(to)) ) + { + ret = PICA_send_file(iptr, filepath.toUtf8().data()); + + if (ret != PICA_OK) + { + //show error somewhere somehow + } + + write_mutex.unlock();//>> + return; + } + + if (sndfilequeues.contains(to)) + { + //sending multiple files is not supported yet + } + else + { + struct PICA_chaninfo *chan = NULL; + + ret = PICA_create_channel(nodelink, (const unsigned char*)to.constData(), &chan); + + QList l; + l.append(filepath); + sndfilequeues[to] = l; + } + +write_mutex.unlock();//>> + + if (ret!= PICA_OK) + { + //report error + } +} + +void SkyNet::AcceptFile(QByteArray from, QString filepath) +{ +int ret = PICA_OK; +struct PICA_chaninfo *iptr; + +write_mutex.lock();//<< +if ( (iptr = find_active_chan(from)) ) + { + ret = PICA_accept_file(iptr, filepath.toUtf8().data(), filepath.toUtf8().size()); + + qDebug() << "PICA_accept_file(" << filepath.toUtf8().data() << ", " << filepath.toUtf8().size() << ") returned " << ret << "\n"; + if (ret != PICA_OK) + { + //show error somewhere somehow + } + } +write_mutex.unlock();//>> +} + void SkyNet::SendMessage(QByteArray to, QString msg) { int ret = PICA_OK; @@ -241,7 +318,6 @@ if ( (iptr = find_active_chan(to)) ) return; } - if (msgqueues.contains(to)) { msgqueues[to].append(msg); @@ -265,17 +341,18 @@ write_mutex.unlock();//>> } -void SkyNet::flush_msgqueue(QByteArray to) +void SkyNet::flush_queues(QByteArray to) { struct PICA_chaninfo *chan; int ret; - if (self_aware && msgqueues.contains(to)) + if (self_aware && (msgqueues.contains(to) || sndfilequeues.contains(to))) { qDebug()<<"flushing "<emit_Delivered(QByteArray((const char*)peer_id, PICA_ID_SIZE)); void SkyNet::channel_established_cb(const unsigned char *peer_id) { -skynet->flush_msgqueue(QByteArray((const char*)peer_id, PICA_ID_SIZE)); +skynet->flush_queues(QByteArray((const char*)peer_id, PICA_ID_SIZE)); } void SkyNet::channel_failed(const unsigned char *peer_id) @@ -426,3 +541,35 @@ int SkyNet::peer_cert_verify_cb(const unsigned char *peer_id, const char *cert_p return 1; } + +int SkyNet::accept_file_cb(const unsigned char *peer_id, uint64_t file_size, const char *filename, unsigned int filename_size) +{ + skynet->emit_IncomingFileRequestReceived(QByteArray((const char*)peer_id, PICA_ID_SIZE), + file_size, QString::fromUtf8(filename, filename_size)); + + return 2;//??? accept later code +} + +void SkyNet::accepted_file_cb(const unsigned char *peer_id) +{ + qDebug()<<"FILE: file was accepted by remote side\n"; + + skynet->emit_OutgoingFileRequestAccepted(QByteArray((const char *)peer_id, PICA_ID_SIZE)); +} + +void SkyNet::denied_file_cb(const unsigned char *peer_id) +{ + skynet->emit_OutgoingFileRequestDenied(QByteArray((const char *)peer_id, PICA_ID_SIZE)); +} + +void SkyNet::file_progress(const unsigned char *peer_id, uint64_t sent, uint64_t received) +{ + qDebug()<<"FILE: file progress" << sent << " sent " << received << "received\n"; + skynet->emit_FileProgress(QByteArray((const char *)peer_id, PICA_ID_SIZE), sent, received); +} + +void SkyNet::file_control(const unsigned char *peer_id, unsigned int sender_cmd, unsigned int receiver_cmd) +{ + //implement me +} + -- cgit v1.2.1