-
Notifications
You must be signed in to change notification settings - Fork 2
/
p3JsonRS.cpp
executable file
·122 lines (99 loc) · 3.72 KB
/
p3JsonRS.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
This file is part of the Zero Reserve Plugin for Retroshare.
Zero Reserve is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Zero Reserve is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Zero Reserve. If not, see <http://www.gnu.org/licenses/>.
*/
#include "p3JsonRS.h"
#include "pqi/p3linkmgr.h"
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
#include<QVariantMap>
// after getting data from 3 peers, we believe we're complete
static const int INIT_THRESHOLD = 3;
p3JsonRS::p3JsonRS(RsPluginHandler *pgHandler, RsPeers* peers, msgQue *msgin ) :
RsPQIService( RS_SERVICE_TYPE_JSON_PLUGIN, CONFIG_TYPE_JSON_PLUGIN, 0, pgHandler ),
m_peers(peers)
{
addSerialType(new RsJsonSerialiser());
pgHandler->getLinkMgr()->addMonitor( this );
mMsgque = msgin;
//tjd = tjdin;
//tjd->p3service = this;
}
void p3JsonRS::msgPeer(std::string peerId, std::string msg){//, std::string message){
RsJsonItem * item = new RsJsonItem();
item->PeerId( peerId );
item->setMessage(msg);
sendItem( item );
}
void p3JsonRS::testit(){
std::cout << "Would send data here" << std::endl;
}
void p3JsonRS::statusChange(const std::list< pqipeer > &plist)
{
//std::cerr << "Json: Status changed:" << std::endl;
for (std::list< pqipeer >::const_iterator peerIt = plist.begin(); peerIt != plist.end(); peerIt++ ){
if( RS_PEER_CONNECTED & (*peerIt).actions ){
RsJsonItem * item = new RsJsonItem();
//item->RS_PKT_SUBTYPE
item->PeerId( (*peerIt).id );
std::stringstream ss;
ss << "JINT " << "nameperhaps";
item->setMessage(ss.str());
std::cout << "sending message: " << item->getMessage() << "\n";
sendItem( item );
//tjd->addPeerItem((*peerIt).id);//do this on message recept
}else if( RS_PEER_DISCONNECTED & (*peerIt).actions ){
QString peerID((*peerIt).id.c_str());
if(compatablePeers.contains(peerID))
compatablePeers.remove(peerID);
}
}
}
int p3JsonRS::tick()
{
RsItem *item = NULL;
while(NULL != (item = recvItem())){
switch( item->PacketSubType() )
{
case RsJsonItem::JSON_ITEM:
handleJsonItem( dynamic_cast<RsJsonItem*>( item ) );
break;
default:
std::cerr << "Json: Received Item unknown" << std::endl;
}
delete item;
}
return 0;
}
void p3JsonRS::handleJsonItem( RsJsonItem * item )
{
//std::cout << "GOT MSG\n";
std::string msg = item->getMessage();
//std::cout << msg << "\n\n";
if (msg.substr(0,4).compare("JINT") == 0){
compatablePeers.insert(item->PeerId().c_str(),msg.c_str());
return;
}
QVariantMap qDict;
qDict.insert("message",QString(item->getMessage().c_str()));
qDict.insert("peerID",QString(item->PeerId().c_str()));
qDict.insert("peerName",QString(rsPeers->getPeerName(item->PeerId()).c_str()));
/*QStringList qMsg;
qMsg.append(QString(item->getMessage().c_str()));
qMsg.append(QString(item->PeerId().c_str()));
qMsg.append(QString(rsPeers->getPeerName(item->PeerId()).c_str()));*/
eBridge->pushMsgToJs(qDict);
//mMsgque->storeMsg(item);
}