2007-11-14 22:18:48 -05:00
|
|
|
/*
|
2008-01-25 01:36:40 -05:00
|
|
|
* libretroshare/src/pqi: pqipersongrp.cc
|
2007-11-14 22:18:48 -05:00
|
|
|
*
|
|
|
|
* 3P/PQI network interface for RetroShare.
|
|
|
|
*
|
2007-12-11 20:29:14 -05:00
|
|
|
* Copyright 2004-2008 by Robert Fernie.
|
2007-11-14 22:18:48 -05:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License Version 2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
* USA.
|
|
|
|
*
|
|
|
|
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pqi/pqipersongrp.h"
|
2008-01-25 01:36:40 -05:00
|
|
|
#include "pqi/p3connmgr.h"
|
2008-07-10 12:29:18 -04:00
|
|
|
#include "util/rsdebug.h"
|
2008-01-25 01:36:40 -05:00
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
#include <sstream>
|
2009-05-11 10:30:53 -04:00
|
|
|
#include <stdio.h>
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
const int pqipersongrpzone = 354;
|
|
|
|
|
2008-04-03 08:51:28 -04:00
|
|
|
/****
|
2008-07-09 05:55:09 -04:00
|
|
|
*#define PGRP_DEBUG 1
|
|
|
|
****/
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2008-11-22 08:15:07 -05:00
|
|
|
/* MUTEX NOTES:
|
|
|
|
* Functions like GetRsRawItem() lock itself (pqihandler) and
|
|
|
|
* likewise ServiceServer and ConfigMgr mutex themselves.
|
|
|
|
* This means the only things we need to worry about are:
|
|
|
|
* pqilistener and when accessing pqihandlers data.
|
|
|
|
*/
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
// handle the tunnel services.
|
2007-12-11 20:29:14 -05:00
|
|
|
int pqipersongrp::tickServiceRecv()
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2007-12-11 20:29:14 -05:00
|
|
|
RsRawItem *pqi = NULL;
|
2007-11-14 22:18:48 -05:00
|
|
|
int i = 0;
|
|
|
|
{
|
|
|
|
std::ostringstream out;
|
|
|
|
out << "pqipersongrp::tickTunnelServer()";
|
|
|
|
pqioutput(PQL_DEBUG_ALL, pqipersongrpzone, out.str());
|
|
|
|
}
|
|
|
|
|
2007-12-11 20:29:14 -05:00
|
|
|
//p3ServiceServer::tick();
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2007-12-11 20:29:14 -05:00
|
|
|
while(NULL != (pqi = GetRsRawItem()))
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
++i;
|
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqipersongrpzone,
|
2007-12-11 20:29:14 -05:00
|
|
|
"pqipersongrp::tickTunnelServer() Incoming TunnelItem");
|
2007-11-14 22:18:48 -05:00
|
|
|
incoming(pqi);
|
|
|
|
}
|
2007-12-11 20:29:14 -05:00
|
|
|
|
|
|
|
if (0 < i)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2007-12-11 20:29:14 -05:00
|
|
|
return 1;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
2007-12-11 20:29:14 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle the tunnel services.
|
|
|
|
int pqipersongrp::tickServiceSend()
|
|
|
|
{
|
|
|
|
RsRawItem *pqi = NULL;
|
|
|
|
int i = 0;
|
|
|
|
{
|
|
|
|
std::ostringstream out;
|
|
|
|
out << "pqipersongrp::tickServiceSend()";
|
|
|
|
pqioutput(PQL_DEBUG_ALL, pqipersongrpzone, out.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
p3ServiceServer::tick();
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2008-11-22 08:15:07 -05:00
|
|
|
while(NULL != (pqi = outgoing())) /* outgoing has own locking */
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
++i;
|
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqipersongrpzone,
|
2007-12-11 20:29:14 -05:00
|
|
|
"pqipersongrp::tickTunnelServer() OutGoing RsItem");
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2008-11-22 08:15:07 -05:00
|
|
|
SendRsRawItem(pqi); /* Locked by pqihandler */
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
if (0 < i)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
// init
|
|
|
|
pqipersongrp::pqipersongrp(SecurityPolicy *glob, unsigned long flags)
|
|
|
|
:pqihandler(glob), pqil(NULL), config(NULL), initFlags(flags)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
int pqipersongrp::tick()
|
|
|
|
{
|
|
|
|
/* could limit the ticking of listener / tunnels to 1/sec...
|
|
|
|
* but not to important.
|
|
|
|
*/
|
|
|
|
|
2008-11-22 08:15:07 -05:00
|
|
|
{ RsStackMutex stack(coreMtx); /**************** LOCKED MUTEX ****************/
|
2007-11-14 22:18:48 -05:00
|
|
|
if (pqil)
|
|
|
|
{
|
|
|
|
pqil -> tick();
|
|
|
|
}
|
2008-11-22 08:15:07 -05:00
|
|
|
} /* UNLOCKED */
|
|
|
|
|
2007-12-11 20:29:14 -05:00
|
|
|
int i = 0;
|
|
|
|
|
2008-04-03 08:51:28 -04:00
|
|
|
if (tickServiceSend())
|
|
|
|
{
|
|
|
|
i = 1;
|
2008-07-09 05:55:09 -04:00
|
|
|
#ifdef PGRP_DEBUG
|
2008-04-03 08:51:28 -04:00
|
|
|
std::cerr << "pqipersongrp::tick() moreToTick from tickServiceSend()" << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pqihandler::tick()) /* does actual Send/Recv */
|
|
|
|
{
|
|
|
|
i = 1;
|
2008-07-09 05:55:09 -04:00
|
|
|
#ifdef PGRP_DEBUG
|
2008-04-03 08:51:28 -04:00
|
|
|
std::cerr << "pqipersongrp::tick() moreToTick from pqihandler::tick()" << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2007-12-11 20:29:14 -05:00
|
|
|
|
2008-04-03 08:51:28 -04:00
|
|
|
if (tickServiceRecv())
|
|
|
|
{
|
|
|
|
i = 1;
|
2008-07-09 05:55:09 -04:00
|
|
|
#ifdef PGRP_DEBUG
|
2008-04-03 08:51:28 -04:00
|
|
|
std::cerr << "pqipersongrp::tick() moreToTick from tickServiceRecv()" << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
2007-12-11 20:29:14 -05:00
|
|
|
|
2008-04-03 08:51:28 -04:00
|
|
|
return i;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int pqipersongrp::status()
|
|
|
|
{
|
2008-11-22 08:15:07 -05:00
|
|
|
{ RsStackMutex stack(coreMtx); /**************** LOCKED MUTEX ****************/
|
2007-11-14 22:18:48 -05:00
|
|
|
if (pqil)
|
|
|
|
{
|
|
|
|
pqil -> status();
|
|
|
|
}
|
2008-11-22 08:15:07 -05:00
|
|
|
} /* UNLOCKED */
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
return pqihandler::status();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
/* Initialise pqilistener */
|
|
|
|
int pqipersongrp::init_listener()
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2008-01-25 01:36:40 -05:00
|
|
|
/* extract our information from the p3ConnectMgr */
|
|
|
|
if (initFlags & PQIPERSON_NO_LISTENER)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2008-01-25 01:36:40 -05:00
|
|
|
pqil = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* extract details from
|
|
|
|
*/
|
|
|
|
peerConnectState state;
|
|
|
|
mConnMgr->getOwnNetStatus(state);
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2008-11-22 08:15:07 -05:00
|
|
|
RsStackMutex stack(coreMtx); /**************** LOCKED MUTEX ****************/
|
2009-11-11 11:43:51 -05:00
|
|
|
pqil = createListener(state.currentlocaladdr);
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
2008-01-25 01:36:40 -05:00
|
|
|
return 1;
|
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
int pqipersongrp::restart_listener()
|
|
|
|
{
|
2009-08-04 19:22:44 -04:00
|
|
|
std::cerr << "pqipersongrp::restart_listener()" << std::endl;
|
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
// stop it,
|
|
|
|
// change the address.
|
|
|
|
// restart.
|
2008-11-22 08:15:07 -05:00
|
|
|
bool haveListener = false;
|
|
|
|
{ RsStackMutex stack(coreMtx); /**************** LOCKED MUTEX ****************/
|
|
|
|
haveListener = (pqil != NULL);
|
|
|
|
} /* UNLOCKED */
|
|
|
|
|
|
|
|
|
|
|
|
if (haveListener)
|
2007-12-11 20:29:14 -05:00
|
|
|
{
|
2009-08-04 19:22:44 -04:00
|
|
|
std::cerr << "pqipersongrp::restart_listener() haveListener" << std::endl;
|
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
peerConnectState state;
|
|
|
|
mConnMgr->getOwnNetStatus(state);
|
2008-11-22 08:15:07 -05:00
|
|
|
|
|
|
|
RsStackMutex stack(coreMtx); /**************** LOCKED MUTEX ****************/
|
2008-01-25 01:36:40 -05:00
|
|
|
|
|
|
|
pqil -> resetlisten();
|
2009-11-11 11:43:51 -05:00
|
|
|
pqil -> setListenAddr(state.currentlocaladdr);
|
2008-01-25 01:36:40 -05:00
|
|
|
pqil -> setuplisten();
|
2009-08-04 19:22:44 -04:00
|
|
|
|
|
|
|
std::cerr << "pqipersongrp::restart_listener() done!" << std::endl;
|
|
|
|
|
2007-12-11 20:29:14 -05:00
|
|
|
}
|
2008-01-25 01:36:40 -05:00
|
|
|
return 1;
|
|
|
|
}
|
2007-12-11 20:29:14 -05:00
|
|
|
|
2008-11-22 08:15:07 -05:00
|
|
|
/* NOT bothering to protect Config with a mutex.... it is not going to change
|
|
|
|
* and has its own internal mutexs.
|
|
|
|
*/
|
2008-01-25 01:36:40 -05:00
|
|
|
int pqipersongrp::setConfig(p3GeneralConfig *cfg)
|
|
|
|
{
|
|
|
|
config = cfg;
|
2008-08-31 14:50:31 -04:00
|
|
|
return 1;
|
2008-01-25 01:36:40 -05:00
|
|
|
}
|
2007-12-11 20:29:14 -05:00
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
static const std::string pqih_ftr("PQIH_FTR");
|
2007-12-11 20:29:14 -05:00
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
int pqipersongrp::save_config()
|
|
|
|
{
|
|
|
|
char line[512];
|
2009-06-08 13:09:00 -04:00
|
|
|
sprintf(line, "%f %f", getMaxRate(true), getMaxRate(false));
|
2008-01-25 01:36:40 -05:00
|
|
|
if (config)
|
|
|
|
{
|
|
|
|
config -> setSetting(pqih_ftr, std::string(line));
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pqipersongrp::load_config()
|
|
|
|
{
|
|
|
|
std::string line;
|
|
|
|
if (config)
|
|
|
|
{
|
|
|
|
line = config -> getSetting(pqih_ftr);
|
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2009-06-08 13:09:00 -04:00
|
|
|
float mri, mro;
|
|
|
|
|
|
|
|
if (2 == sscanf(line.c_str(), "%f %f", &mri, &mro))
|
2008-01-25 01:36:40 -05:00
|
|
|
{
|
|
|
|
setMaxRate(true, mri);
|
|
|
|
setMaxRate(false, mro);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqipersongrpzone,
|
|
|
|
"pqipersongrp::load_config() Loading Default Rates!");
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2008-03-26 11:35:09 -04:00
|
|
|
setMaxRate(true, 500.0);
|
|
|
|
setMaxRate(false, 500.0);
|
2008-01-25 01:36:40 -05:00
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void pqipersongrp::statusChange(const std::list<pqipeer> &plist)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* iterate through, only worry about the friends */
|
|
|
|
std::list<pqipeer>::const_iterator it;
|
|
|
|
for(it = plist.begin(); it != plist.end(); it++)
|
|
|
|
{
|
|
|
|
if (it->state & RS_PEER_S_FRIEND)
|
|
|
|
{
|
|
|
|
/* now handle add/remove */
|
|
|
|
if ((it->actions & RS_PEER_NEW)
|
|
|
|
|| (it->actions & RS_PEER_MOVED))
|
|
|
|
{
|
|
|
|
addPeer(it->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (it->actions & RS_PEER_CONNECT_REQ)
|
|
|
|
{
|
|
|
|
connectPeer(it->id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else /* Not Friend */
|
|
|
|
{
|
|
|
|
if (it->actions & RS_PEER_MOVED)
|
|
|
|
{
|
|
|
|
removePeer(it->id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
int pqipersongrp::addPeer(std::string id)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::ostringstream out;
|
|
|
|
out << "pqipersongrp::addPeer() PeerId: " << id;
|
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqipersongrpzone, out.str());
|
|
|
|
}
|
|
|
|
|
2008-07-09 05:55:09 -04:00
|
|
|
#ifdef PGRP_DEBUG
|
2008-01-25 01:36:40 -05:00
|
|
|
std::cerr << " pqipersongrp::addPeer() id: " << id;
|
|
|
|
std::cerr << std::endl;
|
2008-07-09 05:55:09 -04:00
|
|
|
#endif
|
2008-01-25 01:36:40 -05:00
|
|
|
|
|
|
|
SearchModule *sm = NULL;
|
2008-11-22 08:15:07 -05:00
|
|
|
{ RsStackMutex stack(coreMtx); /**************** LOCKED MUTEX ****************/
|
2008-01-25 01:36:40 -05:00
|
|
|
std::map<std::string, SearchModule *>::iterator it;
|
|
|
|
it = mods.find(id);
|
|
|
|
if (it != mods.end())
|
|
|
|
{
|
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqipersongrpzone,
|
|
|
|
"pqipersongrp::addPeer() Peer already in Use!");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pqiperson *pqip = createPerson(id, pqil);
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
// attach to pqihandler
|
2008-01-25 01:36:40 -05:00
|
|
|
sm = new SearchModule();
|
|
|
|
sm -> peerid = id;
|
2007-11-14 22:18:48 -05:00
|
|
|
sm -> pqi = pqip;
|
|
|
|
sm -> sp = secpolicy_create();
|
|
|
|
|
|
|
|
// reset it to start it working.
|
2008-01-25 01:36:40 -05:00
|
|
|
pqip -> reset();
|
2008-01-28 00:40:32 -05:00
|
|
|
pqip -> listen();
|
2008-11-22 08:15:07 -05:00
|
|
|
} /* UNLOCKED */
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
return AddSearchModule(sm);
|
|
|
|
}
|
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
|
|
|
|
int pqipersongrp::removePeer(std::string id)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2007-12-11 20:29:14 -05:00
|
|
|
std::map<std::string, SearchModule *>::iterator it;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2008-07-09 05:55:09 -04:00
|
|
|
#ifdef PGRP_DEBUG
|
2008-01-25 01:36:40 -05:00
|
|
|
std::cerr << " pqipersongrp::removePeer() id: " << id;
|
|
|
|
std::cerr << std::endl;
|
2008-07-09 05:55:09 -04:00
|
|
|
#endif
|
2008-01-25 01:36:40 -05:00
|
|
|
|
2008-11-22 08:15:07 -05:00
|
|
|
RsStackMutex stack(coreMtx); /**************** LOCKED MUTEX ****************/
|
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
it = mods.find(id);
|
|
|
|
if (it != mods.end())
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2008-01-25 01:36:40 -05:00
|
|
|
SearchModule *mod = it->second;
|
2008-02-27 10:43:02 -05:00
|
|
|
// Don't duplicate remove!!!
|
|
|
|
//RemoveSearchModule(mod);
|
2008-01-25 01:36:40 -05:00
|
|
|
secpolicy_delete(mod -> sp);
|
|
|
|
pqiperson *p = (pqiperson *) mod -> pqi;
|
|
|
|
p -> reset();
|
|
|
|
delete p;
|
|
|
|
mods.erase(it);
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-12-13 16:59:26 -05:00
|
|
|
pqiperson *pqipersongrp::getPeer(std::string id)
|
|
|
|
{
|
|
|
|
std::map<std::string, SearchModule *>::iterator it;
|
|
|
|
|
|
|
|
#ifdef PGRP_DEBUG
|
|
|
|
std::cerr << " pqipersongrp::getPeer() id: " << id;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
RsStackMutex stack(coreMtx); /**************** LOCKED MUTEX ****************/
|
|
|
|
|
|
|
|
it = mods.find(id);
|
|
|
|
if (it != mods.end())
|
|
|
|
{
|
|
|
|
SearchModule *mod = it->second;
|
|
|
|
pqiperson *p = (pqiperson *) mod -> pqi;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
int pqipersongrp::connectPeer(std::string id)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2008-01-25 01:36:40 -05:00
|
|
|
/* get status from p3connectMgr */
|
2008-07-09 05:55:09 -04:00
|
|
|
#ifdef PGRP_DEBUG
|
2008-01-25 01:36:40 -05:00
|
|
|
std::cerr << " pqipersongrp::connectPeer() id: " << id << " does nothing yet! ";
|
|
|
|
std::cerr << std::endl;
|
2008-07-09 05:55:09 -04:00
|
|
|
#endif
|
2008-01-25 01:36:40 -05:00
|
|
|
|
2008-11-22 08:15:07 -05:00
|
|
|
{ RsStackMutex stack(coreMtx); /**************** LOCKED MUTEX ****************/
|
2007-12-11 20:29:14 -05:00
|
|
|
std::map<std::string, SearchModule *>::iterator it;
|
2008-01-25 01:36:40 -05:00
|
|
|
it = mods.find(id);
|
|
|
|
if (it == mods.end())
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2008-01-25 01:36:40 -05:00
|
|
|
return 0;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
2008-01-25 01:36:40 -05:00
|
|
|
/* get the connect attempt details from the p3connmgr... */
|
|
|
|
SearchModule *mod = it->second;
|
|
|
|
pqiperson *p = (pqiperson *) mod -> pqi;
|
|
|
|
|
|
|
|
|
|
|
|
/* get address from p3connmgr */
|
|
|
|
if (!mConnMgr)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
struct sockaddr_in addr;
|
2008-02-26 21:32:20 -05:00
|
|
|
uint32_t delay;
|
|
|
|
uint32_t period;
|
2008-03-26 11:35:09 -04:00
|
|
|
uint32_t timeout;
|
2008-01-25 01:36:40 -05:00
|
|
|
uint32_t type;
|
|
|
|
|
2008-02-26 21:32:20 -05:00
|
|
|
if (!mConnMgr->connectAttempt(id, addr, delay, period, type))
|
2008-01-28 00:40:32 -05:00
|
|
|
{
|
2008-07-09 05:55:09 -04:00
|
|
|
#ifdef PGRP_DEBUG
|
2008-01-28 00:40:32 -05:00
|
|
|
std::cerr << " pqipersongrp::connectPeer() No Net Address";
|
|
|
|
std::cerr << std::endl;
|
2008-07-09 05:55:09 -04:00
|
|
|
#endif
|
2008-01-28 00:40:32 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-07-09 05:55:09 -04:00
|
|
|
#ifdef PGRP_DEBUG
|
2008-01-28 00:40:32 -05:00
|
|
|
std::cerr << " pqipersongrp::connectPeer() connectAttempt data id: " << id;
|
|
|
|
std::cerr << " addr: " << inet_ntoa(addr.sin_addr) << ":" << ntohs(addr.sin_port);
|
2008-02-26 21:32:20 -05:00
|
|
|
std::cerr << " delay: " << delay;
|
|
|
|
std::cerr << " period: " << period;
|
2008-01-28 00:40:32 -05:00
|
|
|
std::cerr << " type: " << type;
|
|
|
|
std::cerr << std::endl;
|
2008-07-09 05:55:09 -04:00
|
|
|
#endif
|
2008-01-28 00:40:32 -05:00
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
|
|
|
|
uint32_t ptype;
|
|
|
|
if (type & RS_NET_CONN_TCP_ALL)
|
|
|
|
{
|
|
|
|
ptype = PQI_CONNECT_TCP;
|
2008-03-26 11:35:09 -04:00
|
|
|
timeout = RS_TCP_STD_TIMEOUT_PERIOD;
|
2008-07-09 05:55:09 -04:00
|
|
|
#ifdef PGRP_DEBUG
|
2008-03-26 11:35:09 -04:00
|
|
|
std::cerr << " pqipersongrp::connectPeer() connecting with TCP: Timeout :" << timeout;
|
|
|
|
std::cerr << std::endl;
|
2008-07-09 05:55:09 -04:00
|
|
|
#endif
|
2008-01-25 01:36:40 -05:00
|
|
|
}
|
|
|
|
else if (type & RS_NET_CONN_UDP_ALL)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2008-01-25 01:36:40 -05:00
|
|
|
ptype = PQI_CONNECT_UDP;
|
2008-03-26 11:35:09 -04:00
|
|
|
timeout = period * 2;
|
2008-07-09 05:55:09 -04:00
|
|
|
#ifdef PGRP_DEBUG
|
2008-03-26 11:35:09 -04:00
|
|
|
std::cerr << " pqipersongrp::connectPeer() connecting with UDP: Timeout :" << timeout;
|
|
|
|
std::cerr << std::endl;
|
2009-12-13 16:59:26 -05:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else if (type & RS_NET_CONN_TUNNEL)
|
|
|
|
{
|
|
|
|
ptype = PQI_CONNECT_TUNNEL;
|
|
|
|
timeout = period * 2;
|
|
|
|
#ifdef PGRP_DEBUG
|
|
|
|
std::cerr << " pqipersongrp::connectPeer() connecting with UDP: Timeout :" << timeout;
|
|
|
|
std::cerr << std::endl;
|
2008-07-09 05:55:09 -04:00
|
|
|
#endif
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
2008-01-25 01:36:40 -05:00
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
|
2008-03-26 11:35:09 -04:00
|
|
|
p->connect(ptype, addr, delay, period, timeout);
|
2008-01-25 01:36:40 -05:00
|
|
|
|
2008-11-22 08:15:07 -05:00
|
|
|
} /* UNLOCKED */
|
|
|
|
|
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
/* */
|
2007-11-14 22:18:48 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-12-14 13:11:19 -05:00
|
|
|
bool pqipersongrp::notifyConnect(std::string id, uint32_t ptype, bool success) {
|
|
|
|
struct sockaddr_in remote_peer_address;
|
|
|
|
sockaddr_clear(&remote_peer_address);
|
|
|
|
notifyConnect(id, ptype, success, remote_peer_address);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool pqipersongrp::notifyConnect(std::string id, uint32_t ptype, bool success, struct sockaddr_in remote_peer_address)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2009-12-13 16:59:26 -05:00
|
|
|
uint32_t type = 0;
|
2008-03-26 11:35:09 -04:00
|
|
|
if (ptype == PQI_CONNECT_TCP)
|
|
|
|
{
|
|
|
|
type = RS_NET_CONN_TCP_ALL;
|
|
|
|
}
|
2009-12-13 16:59:26 -05:00
|
|
|
else if (ptype == PQI_CONNECT_UDP)
|
2008-03-26 11:35:09 -04:00
|
|
|
{
|
|
|
|
type = RS_NET_CONN_UDP_ALL;
|
|
|
|
}
|
2009-12-13 16:59:26 -05:00
|
|
|
else if (ptype == PQI_CONNECT_TUNNEL)
|
|
|
|
{
|
|
|
|
type = RS_NET_CONN_TUNNEL;
|
|
|
|
}
|
2008-03-26 11:35:09 -04:00
|
|
|
|
2009-12-13 16:59:26 -05:00
|
|
|
if (mConnMgr) {
|
|
|
|
if (ptype == PQI_CONNECT_DO_NEXT_ATTEMPT) {
|
|
|
|
mConnMgr->doNextAttempt(id);
|
|
|
|
} else {
|
2009-12-14 13:11:19 -05:00
|
|
|
mConnMgr->connectResult(id, success, type, remote_peer_address);
|
2009-12-13 16:59:26 -05:00
|
|
|
}
|
|
|
|
}
|
2008-01-25 01:36:40 -05:00
|
|
|
|
|
|
|
return (NULL != mConnMgr);
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
/******************************** DUMMY Specific features ***************************/
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
#include "pqi/pqibin.h"
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
pqilistener * pqipersongrpDummy::createListener(struct sockaddr_in laddr)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2008-01-25 01:36:40 -05:00
|
|
|
pqilistener *listener = new pqilistener();
|
|
|
|
return listener;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
pqiperson * pqipersongrpDummy::createPerson(std::string id, pqilistener *listener)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
{
|
2008-01-25 01:36:40 -05:00
|
|
|
std::ostringstream out;
|
|
|
|
out << "pqipersongrpDummy::createPerson() PeerId: " << id;
|
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqipersongrpzone, out.str());
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
pqiperson *pqip = new pqiperson(id, this);
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
// TCP
|
|
|
|
NetBinDummy *d1 = new NetBinDummy(pqip, id, PQI_CONNECT_TCP);
|
|
|
|
|
|
|
|
RsSerialiser *rss = new RsSerialiser();
|
|
|
|
rss->addSerialType(new RsFileItemSerialiser());
|
|
|
|
rss->addSerialType(new RsCacheItemSerialiser());
|
|
|
|
rss->addSerialType(new RsServiceSerialiser());
|
|
|
|
|
|
|
|
pqiconnect *pqic = new pqiconnect(rss, d1);
|
|
|
|
|
|
|
|
pqip -> addChildInterface(PQI_CONNECT_TCP, pqic);
|
|
|
|
|
|
|
|
// UDP.
|
|
|
|
NetBinDummy *d2 = new NetBinDummy(pqip, id, PQI_CONNECT_UDP);
|
|
|
|
|
|
|
|
RsSerialiser *rss2 = new RsSerialiser();
|
|
|
|
rss2->addSerialType(new RsFileItemSerialiser());
|
|
|
|
rss2->addSerialType(new RsCacheItemSerialiser());
|
|
|
|
rss2->addSerialType(new RsServiceSerialiser());
|
|
|
|
|
|
|
|
pqiconnect *pqic2 = new pqiconnect(rss2, d2);
|
|
|
|
|
|
|
|
pqip -> addChildInterface(PQI_CONNECT_UDP, pqic2);
|
|
|
|
|
|
|
|
return pqip;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
2008-01-25 01:36:40 -05:00
|
|
|
|
|
|
|
/******************************** DUMMY Specific features ***************************/
|
2007-11-14 22:18:48 -05:00
|
|
|
|