2007-11-14 22:18:48 -05:00
|
|
|
/*
|
2007-12-11 20:29:14 -05:00
|
|
|
* libretroshare/src/pqi pqiperson.cc
|
2007-11-14 22:18:48 -05:00
|
|
|
*
|
|
|
|
* 3P/PQI network interface for RetroShare.
|
|
|
|
*
|
|
|
|
* Copyright 2004-2006 by Robert Fernie.
|
|
|
|
*
|
|
|
|
* 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/pqi.h"
|
|
|
|
#include "pqi/pqiperson.h"
|
2008-01-25 01:36:40 -05:00
|
|
|
#include "pqi/pqipersongrp.h"
|
2009-12-22 05:41:19 -05:00
|
|
|
#include "services/p3disc.h"
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
const int pqipersonzone = 82371;
|
2008-07-10 12:29:18 -04:00
|
|
|
#include "util/rsdebug.h"
|
2007-11-14 22:18:48 -05:00
|
|
|
#include <sstream>
|
|
|
|
|
2008-07-09 05:55:09 -04:00
|
|
|
/****
|
|
|
|
* #define PERSON_DEBUG
|
|
|
|
****/
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
pqiperson::pqiperson(std::string id, pqipersongrp *pg)
|
|
|
|
:PQInterface(id), active(false), activepqi(NULL),
|
|
|
|
inConnectAttempt(false), waittimes(0),
|
|
|
|
pqipg(pg)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
/* must check id! */
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pqiperson::~pqiperson()
|
|
|
|
{
|
|
|
|
// clean up the children.
|
2008-01-25 01:36:40 -05:00
|
|
|
std::map<uint32_t, pqiconnect *>::iterator it;
|
|
|
|
for(it = kids.begin(); it != kids.end(); it++)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2008-01-25 01:36:40 -05:00
|
|
|
pqiconnect *pc = (it->second);
|
2007-11-14 22:18:48 -05:00
|
|
|
delete pc;
|
|
|
|
}
|
2008-01-25 01:36:40 -05:00
|
|
|
kids.clear();
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// The PQInterface interface.
|
2011-09-04 16:01:30 -04:00
|
|
|
int pqiperson::SendItem(RsItem *i,uint32_t& serialized_size)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
std::ostringstream out;
|
|
|
|
out << "pqiperson::SendItem()";
|
|
|
|
if (active)
|
|
|
|
{
|
2009-12-13 16:59:26 -05:00
|
|
|
out << " Active: Sending On" << std::endl;
|
|
|
|
i->print(out, 5);
|
|
|
|
#ifdef PERSON_DEBUG
|
|
|
|
std::cerr << out.str() << std::endl;
|
|
|
|
#endif
|
2011-09-04 16:01:30 -04:00
|
|
|
return activepqi -> SendItem(i,serialized_size);
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
out << " Not Active: Used to put in ToGo Store";
|
|
|
|
out << std::endl;
|
|
|
|
out << " Now deleting...";
|
|
|
|
delete i;
|
|
|
|
}
|
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqipersonzone, out.str());
|
|
|
|
return 0; // queued.
|
|
|
|
}
|
|
|
|
|
2007-12-11 20:29:14 -05:00
|
|
|
RsItem *pqiperson::GetItem()
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
if (active)
|
|
|
|
return activepqi -> GetItem();
|
|
|
|
// else not possible.
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pqiperson::status()
|
|
|
|
{
|
|
|
|
if (active)
|
|
|
|
return activepqi -> status();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-12-22 05:41:19 -05:00
|
|
|
int pqiperson::receiveHeartbeat()
|
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
pqioutput(PQL_WARNING, pqipersonzone, "pqiperson::receiveHeartbeat() from peer : " + PeerId());
|
2009-12-22 05:41:19 -05:00
|
|
|
lastHeartbeatReceived = time(NULL);
|
2010-01-27 03:24:39 -05:00
|
|
|
|
|
|
|
return true ;
|
2009-12-22 05:41:19 -05:00
|
|
|
}
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
// tick......
|
|
|
|
int pqiperson::tick()
|
|
|
|
{
|
2009-12-22 05:41:19 -05:00
|
|
|
//if lastHeartbeatReceived is 0, it might be not activated so don't do a net reset.
|
2012-01-19 11:23:57 -05:00
|
|
|
if (active && (lastHeartbeatReceived != 0) &&
|
2011-07-18 20:06:09 -04:00
|
|
|
(time(NULL) - lastHeartbeatReceived) > HEARTBEAT_REPEAT_TIME * 5)
|
|
|
|
{
|
2012-01-19 11:23:57 -05:00
|
|
|
int ageLastIncoming = time(NULL) - activepqi->getLastIncomingTS();
|
2011-07-18 20:06:09 -04:00
|
|
|
std::ostringstream out;
|
2012-01-19 11:23:57 -05:00
|
|
|
out << "pqiperson::tick() WARNING No heartbeat from: " << PeerId();
|
|
|
|
//out << " assume dead. calling pqissl::reset(), LastHeartbeat was: ";
|
|
|
|
out << " LastHeartbeat was: ";
|
2011-07-18 20:06:09 -04:00
|
|
|
out << time(NULL) - lastHeartbeatReceived << " secs ago";
|
2012-01-19 11:23:57 -05:00
|
|
|
out << " LastIncoming was: ";
|
|
|
|
out << ageLastIncoming << " secs ago";
|
2011-07-18 20:06:09 -04:00
|
|
|
pqioutput(PQL_WARNING, pqipersonzone, out.str());
|
2009-12-22 05:41:19 -05:00
|
|
|
|
2012-01-19 11:23:57 -05:00
|
|
|
#define NO_PACKET_TIMEOUT 60
|
|
|
|
|
|
|
|
if (ageLastIncoming > NO_PACKET_TIMEOUT)
|
|
|
|
{
|
|
|
|
std::ostringstream out2;
|
|
|
|
out2 << "pqiperson::tick() " << PeerId();
|
|
|
|
out2 << " No Heartbeat & No Packets -> assume dead. calling pqissl::reset()";
|
|
|
|
pqioutput(PQL_WARNING, pqipersonzone, out2.str());
|
|
|
|
|
|
|
|
this->reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int activeTick = 0;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
{
|
|
|
|
std::ostringstream out;
|
2008-01-25 01:36:40 -05:00
|
|
|
out << "pqiperson::tick() Id: " << PeerId() << " ";
|
2007-11-14 22:18:48 -05:00
|
|
|
if (active)
|
|
|
|
out << "***Active***";
|
|
|
|
else
|
|
|
|
out << ">>InActive<<";
|
|
|
|
|
|
|
|
out << std::endl;
|
|
|
|
out << "Activepqi: " << activepqi << " inConnectAttempt: ";
|
|
|
|
|
|
|
|
if (inConnectAttempt)
|
|
|
|
out << "In Connection Attempt";
|
|
|
|
else
|
|
|
|
out << " Not Connecting ";
|
|
|
|
out << std::endl;
|
|
|
|
|
|
|
|
|
|
|
|
// tick the children.
|
2008-01-25 01:36:40 -05:00
|
|
|
std::map<uint32_t, pqiconnect *>::iterator it;
|
2007-11-14 22:18:48 -05:00
|
|
|
for(it = kids.begin(); it != kids.end(); it++)
|
|
|
|
{
|
2008-01-25 01:36:40 -05:00
|
|
|
if (0 < (it->second) -> tick())
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
activeTick = 1;
|
|
|
|
}
|
2008-01-25 01:36:40 -05:00
|
|
|
out << "\tTicking Child: " << (it->first) << std::endl;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pqioutput(PQL_DEBUG_ALL, pqipersonzone, out.str());
|
|
|
|
} // end of pqioutput.
|
|
|
|
|
|
|
|
return activeTick;
|
|
|
|
}
|
|
|
|
|
|
|
|
// callback function for the child - notify of a change.
|
|
|
|
// This is only used for out-of-band info....
|
|
|
|
// otherwise could get dangerous loops.
|
|
|
|
int pqiperson::notifyEvent(NetInterface *ni, int newState)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::ostringstream out;
|
2008-01-25 01:36:40 -05:00
|
|
|
out << "pqiperson::notifyEvent() Id: " << PeerId();
|
2007-11-14 22:18:48 -05:00
|
|
|
out << std::endl;
|
|
|
|
out << "Message: " << newState << " from: " << ni << std::endl;
|
2009-12-13 16:59:26 -05:00
|
|
|
out << "Active pqi : " << activepqi;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqipersonzone, out.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find the pqi, */
|
2010-06-25 18:00:38 -04:00
|
|
|
pqiconnect *pqi = NULL;
|
2008-03-26 11:35:09 -04:00
|
|
|
uint32_t type = 0;
|
2008-01-25 01:36:40 -05:00
|
|
|
std::map<uint32_t, pqiconnect *>::iterator it;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
/* start again */
|
|
|
|
int i = 0;
|
|
|
|
for(it = kids.begin(); it != kids.end(); it++)
|
|
|
|
{
|
|
|
|
std::ostringstream out;
|
|
|
|
out << "pqiperson::connectattempt() Kid# ";
|
|
|
|
out << i << " of " << kids.size();
|
2010-06-25 18:00:38 -04:00
|
|
|
out << std::endl;
|
|
|
|
out << " type: " << (it->first);
|
2012-01-08 17:15:19 -05:00
|
|
|
//out << " ni: " << (it->second)->ni;
|
2007-11-14 22:18:48 -05:00
|
|
|
out << " in_ni: " << ni;
|
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqipersonzone, out.str());
|
2010-06-25 18:00:38 -04:00
|
|
|
i++;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
if ((it->second)->thisNetInterface(ni))
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2010-06-25 18:00:38 -04:00
|
|
|
pqi = (it->second);
|
2008-03-26 11:35:09 -04:00
|
|
|
type = (it->first);
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pqi)
|
|
|
|
{
|
|
|
|
pqioutput(PQL_WARNING, pqipersonzone, "Unknown notfyEvent Source!");
|
|
|
|
return -1;
|
|
|
|
}
|
2010-06-25 18:00:38 -04:00
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
switch(newState)
|
|
|
|
{
|
|
|
|
case CONNECT_RECEIVED:
|
|
|
|
case CONNECT_SUCCESS:
|
2008-01-25 01:36:40 -05:00
|
|
|
|
|
|
|
/* notify */
|
2009-12-14 13:11:19 -05:00
|
|
|
if (pqipg) {
|
|
|
|
struct sockaddr_in remote_peer_address;
|
|
|
|
pqi->getConnectAddress(remote_peer_address);
|
|
|
|
pqipg->notifyConnect(PeerId(), type, true, remote_peer_address);
|
|
|
|
}
|
2008-01-25 01:36:40 -05:00
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
if ((active) && (activepqi != pqi)) // already connected - trouble
|
|
|
|
{
|
2012-01-18 17:51:38 -05:00
|
|
|
{
|
|
|
|
std::ostringstream out;
|
|
|
|
out << "pqiperson::notifyEvent() Id: " << PeerId();
|
|
|
|
out << " CONNECT_SUCCESS+active-> activing new connection, shutting others";
|
|
|
|
pqioutput(PQL_WARNING, pqipersonzone, out.str());
|
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
// This is the RESET that's killing the connections.....
|
2009-12-13 16:59:26 -05:00
|
|
|
//activepqi -> reset();
|
2007-11-14 22:18:48 -05:00
|
|
|
// this causes a recursive call back into this fn.
|
|
|
|
// which cleans up state.
|
|
|
|
// we only do this if its not going to mess with new conn.
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now install a new one. */
|
|
|
|
{
|
|
|
|
|
2012-01-18 17:51:38 -05:00
|
|
|
{
|
|
|
|
std::ostringstream out;
|
|
|
|
out << "pqiperson::notifyEvent() Id: " << PeerId();
|
|
|
|
out << " CONNECT_SUCCESS->marking so! (resetting others)";
|
|
|
|
pqioutput(PQL_WARNING, pqipersonzone, out.str());
|
|
|
|
}
|
|
|
|
|
2010-06-25 18:00:38 -04:00
|
|
|
// mark as active.
|
2007-11-14 22:18:48 -05:00
|
|
|
active = true;
|
2009-12-22 05:41:19 -05:00
|
|
|
lastHeartbeatReceived = 0;
|
2007-11-14 22:18:48 -05:00
|
|
|
activepqi = pqi;
|
2010-06-25 18:00:38 -04:00
|
|
|
inConnectAttempt = false;
|
2008-03-26 11:35:09 -04:00
|
|
|
|
|
|
|
/* reset all other children? (clear up long UDP attempt) */
|
|
|
|
for(it = kids.begin(); it != kids.end(); it++)
|
|
|
|
{
|
2010-01-08 20:40:50 -05:00
|
|
|
if (!(it->second)->thisNetInterface(ni))
|
|
|
|
{
|
2010-01-08 20:41:57 -05:00
|
|
|
std::ostringstream out;
|
2010-01-08 20:40:50 -05:00
|
|
|
out << "Resetting pqi ref : " << &(it->second) << std::endl;
|
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqipersonzone, out.str());
|
2009-12-13 16:59:26 -05:00
|
|
|
it->second->reset();
|
|
|
|
} else {
|
2010-02-08 09:06:43 -05:00
|
|
|
//std::cerr << "Active pqi : not resetting." << std::endl;
|
2009-12-13 16:59:26 -05:00
|
|
|
}
|
2008-03-26 11:35:09 -04:00
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CONNECT_UNREACHABLE:
|
|
|
|
case CONNECT_FIREWALLED:
|
|
|
|
case CONNECT_FAILED:
|
2008-01-25 01:36:40 -05:00
|
|
|
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
if (active)
|
|
|
|
{
|
2010-06-25 18:00:38 -04:00
|
|
|
if (activepqi == pqi)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2012-01-18 17:51:38 -05:00
|
|
|
{
|
|
|
|
std::ostringstream out;
|
|
|
|
out << "pqiperson::notifyEvent() Id: " << PeerId();
|
|
|
|
out << " CONNECT_FAILED->marking so!";
|
|
|
|
pqioutput(PQL_WARNING, pqipersonzone, out.str());
|
|
|
|
}
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
active = false;
|
|
|
|
activepqi = NULL;
|
2012-01-18 17:51:38 -05:00
|
|
|
}
|
|
|
|
else
|
2011-07-11 08:11:58 -04:00
|
|
|
{
|
2012-01-18 17:51:38 -05:00
|
|
|
{
|
|
|
|
std::ostringstream out;
|
|
|
|
out << "pqiperson::notifyEvent() Id: " << PeerId();
|
|
|
|
out << " CONNECT_FAILED-> from an unactive connection, don't flag the peer as not connected, just try next attempt !";
|
|
|
|
pqioutput(PQL_WARNING, pqipersonzone, out.str());
|
|
|
|
}
|
2009-12-13 16:59:26 -05:00
|
|
|
}
|
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
else
|
|
|
|
{
|
2012-01-18 17:51:38 -05:00
|
|
|
{
|
|
|
|
std::ostringstream out;
|
|
|
|
out << "pqiperson::notifyEvent() Id: " << PeerId();
|
|
|
|
out << " CONNECT_FAILED+NOT active -> try connect again";
|
|
|
|
pqioutput(PQL_WARNING, pqipersonzone, out.str());
|
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
2008-03-21 15:06:34 -04:00
|
|
|
|
2011-07-11 08:11:58 -04:00
|
|
|
/* notify up */
|
2010-06-25 18:00:38 -04:00
|
|
|
if (pqipg)
|
|
|
|
{
|
|
|
|
struct sockaddr_in raddr;
|
|
|
|
sockaddr_clear(&raddr);
|
|
|
|
pqipg->notifyConnect(PeerId(), type, false, raddr);
|
|
|
|
}
|
2008-03-21 15:06:34 -04:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************** Not PQInterface Fns ***********************/
|
|
|
|
|
|
|
|
int pqiperson::reset()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::ostringstream out;
|
2011-08-06 08:27:23 -04:00
|
|
|
out << "pqiperson::reset() resetting all pqiconnect for Id: " << PeerId();
|
|
|
|
pqioutput(PQL_WARNING, pqipersonzone, out.str());
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
std::map<uint32_t, pqiconnect *>::iterator it;
|
2007-11-14 22:18:48 -05:00
|
|
|
for(it = kids.begin(); it != kids.end(); it++)
|
|
|
|
{
|
2008-01-25 01:36:40 -05:00
|
|
|
(it->second) -> reset();
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
activepqi = NULL;
|
|
|
|
active = false;
|
2009-12-22 05:41:19 -05:00
|
|
|
lastHeartbeatReceived = 0;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
int pqiperson::addChildInterface(uint32_t type, pqiconnect *pqi)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
{
|
|
|
|
std::ostringstream out;
|
2008-01-25 01:36:40 -05:00
|
|
|
out << "pqiperson::addChildInterface() : Id " << PeerId() << " " << type;
|
2007-11-14 22:18:48 -05:00
|
|
|
out << std::endl;
|
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqipersonzone, out.str());
|
|
|
|
}
|
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
kids[type] = pqi;
|
2007-11-14 22:18:48 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************** PRIVATE FUNCTIONS ***********************/
|
|
|
|
// functions to iterate over the connects and change state.
|
|
|
|
|
|
|
|
|
|
|
|
int pqiperson::listen()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::ostringstream out;
|
2008-01-25 01:36:40 -05:00
|
|
|
out << "pqiperson::listen() Id: " << PeerId();
|
2007-11-14 22:18:48 -05:00
|
|
|
out << std::endl;
|
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqipersonzone, out.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!active)
|
|
|
|
{
|
2008-01-25 01:36:40 -05:00
|
|
|
std::map<uint32_t, pqiconnect *>::iterator it;
|
2007-11-14 22:18:48 -05:00
|
|
|
for(it = kids.begin(); it != kids.end(); it++)
|
|
|
|
{
|
|
|
|
// set them all listening.
|
2008-01-25 01:36:40 -05:00
|
|
|
(it->second) -> listen();
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int pqiperson::stoplistening()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::ostringstream out;
|
2008-01-25 01:36:40 -05:00
|
|
|
out << "pqiperson::stoplistening() Id: " << PeerId();
|
2007-11-14 22:18:48 -05:00
|
|
|
out << std::endl;
|
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqipersonzone, out.str());
|
|
|
|
}
|
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
std::map<uint32_t, pqiconnect *>::iterator it;
|
2007-11-14 22:18:48 -05:00
|
|
|
for(it = kids.begin(); it != kids.end(); it++)
|
|
|
|
{
|
|
|
|
// set them all listening.
|
2008-01-25 01:36:40 -05:00
|
|
|
(it->second) -> stoplistening();
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-07-14 10:56:33 -04:00
|
|
|
int pqiperson::connect(uint32_t type, struct sockaddr_in raddr,
|
|
|
|
struct sockaddr_in &proxyaddr, struct sockaddr_in &srcaddr,
|
|
|
|
uint32_t delay, uint32_t period, uint32_t timeout, uint32_t flags, uint32_t bandwidth)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2008-07-09 05:55:09 -04:00
|
|
|
#ifdef PERSON_DEBUG
|
2011-08-06 08:27:23 -04:00
|
|
|
#endif
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
std::ostringstream out;
|
2008-01-25 01:36:40 -05:00
|
|
|
out << "pqiperson::connect() Id: " << PeerId();
|
|
|
|
out << " type: " << type;
|
2011-07-14 10:56:33 -04:00
|
|
|
out << " addr: " << rs_inet_ntoa(raddr.sin_addr) << ":" << ntohs(raddr.sin_port);
|
|
|
|
out << " proxyaddr: " << rs_inet_ntoa(proxyaddr.sin_addr) << ":" << ntohs(proxyaddr.sin_port);
|
|
|
|
out << " srcaddr: " << rs_inet_ntoa(srcaddr.sin_addr) << ":" << ntohs(srcaddr.sin_port);
|
2008-03-26 11:35:09 -04:00
|
|
|
out << " delay: " << delay;
|
|
|
|
out << " period: " << period;
|
|
|
|
out << " timeout: " << timeout;
|
2011-07-13 18:23:40 -04:00
|
|
|
out << " flags: " << flags;
|
2011-07-14 10:56:33 -04:00
|
|
|
out << " bandwidth: " << bandwidth;
|
2007-11-14 22:18:48 -05:00
|
|
|
out << std::endl;
|
2011-08-06 08:27:23 -04:00
|
|
|
//std::cerr << out.str();
|
|
|
|
pqioutput(PQL_WARNING, pqipersonzone, out.str());
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
std::map<uint32_t, pqiconnect *>::iterator it;
|
|
|
|
|
|
|
|
it = kids.find(type);
|
2007-11-14 22:18:48 -05:00
|
|
|
if (it == kids.end())
|
|
|
|
{
|
2008-07-09 05:55:09 -04:00
|
|
|
#ifdef PERSON_DEBUG
|
2008-01-25 01:36:40 -05:00
|
|
|
std::ostringstream out;
|
|
|
|
out << "pqiperson::connect()";
|
|
|
|
out << " missing pqiconnect";
|
|
|
|
out << std::endl;
|
2008-02-26 21:32:20 -05:00
|
|
|
std::cerr << out.str();
|
|
|
|
//pqioutput(PQL_DEBUG_BASIC, pqipersonzone, out.str());
|
2008-07-09 05:55:09 -04:00
|
|
|
#endif
|
2010-07-04 06:35:38 -04:00
|
|
|
/* notify of fail! */
|
|
|
|
|
|
|
|
pqipg->notifyConnect(PeerId(), type, false, raddr);
|
|
|
|
|
2008-02-26 21:32:20 -05:00
|
|
|
return 0;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
2008-02-26 21:32:20 -05:00
|
|
|
|
2011-07-13 18:23:40 -04:00
|
|
|
#ifdef PERSON_DEBUG
|
2012-01-20 10:13:50 -05:00
|
|
|
std::cerr << "pqiperson::connect() WARNING, resetting for new connection attempt" << std::endl;
|
2011-07-13 18:23:40 -04:00
|
|
|
#endif
|
2008-02-26 21:32:20 -05:00
|
|
|
/* set the parameters */
|
2011-08-06 08:27:23 -04:00
|
|
|
pqioutput(PQL_WARNING, pqipersonzone, "pqiperson::connect reset() before connection attempt");
|
2008-03-21 15:06:34 -04:00
|
|
|
(it->second)->reset();
|
2008-03-26 11:35:09 -04:00
|
|
|
|
2012-01-20 10:13:50 -05:00
|
|
|
#ifdef PERSON_DEBUG
|
2012-01-08 13:40:09 -05:00
|
|
|
std::cerr << "pqiperson::connect() WARNING, clearing rate cap" << std::endl;
|
2012-01-20 10:13:50 -05:00
|
|
|
#endif
|
2012-01-08 13:40:09 -05:00
|
|
|
setRateCap(0,0);
|
|
|
|
|
2008-07-09 05:55:09 -04:00
|
|
|
#ifdef PERSON_DEBUG
|
2008-03-26 11:35:09 -04:00
|
|
|
std::cerr << "pqiperson::connect() setting connect_parameters" << std::endl;
|
2008-07-09 05:55:09 -04:00
|
|
|
#endif
|
2008-02-26 21:32:20 -05:00
|
|
|
(it->second)->connect_parameter(NET_PARAM_CONNECT_DELAY, delay);
|
|
|
|
(it->second)->connect_parameter(NET_PARAM_CONNECT_PERIOD, period);
|
2008-03-26 11:35:09 -04:00
|
|
|
(it->second)->connect_parameter(NET_PARAM_CONNECT_TIMEOUT, timeout);
|
2011-07-13 18:23:40 -04:00
|
|
|
(it->second)->connect_parameter(NET_PARAM_CONNECT_FLAGS, flags);
|
2011-07-14 10:56:33 -04:00
|
|
|
(it->second)->connect_parameter(NET_PARAM_CONNECT_BANDWIDTH, bandwidth);
|
2008-03-26 11:35:09 -04:00
|
|
|
|
2011-07-14 10:56:33 -04:00
|
|
|
(it->second)->connect_additional_address(NET_PARAM_CONNECT_PROXY, &proxyaddr);
|
|
|
|
(it->second)->connect_additional_address(NET_PARAM_CONNECT_SOURCE, &srcaddr);
|
|
|
|
|
|
|
|
(it->second)->connect(raddr);
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
// flag if we started a new connectionAttempt.
|
2008-01-25 01:36:40 -05:00
|
|
|
inConnectAttempt = true;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-13 16:59:26 -05:00
|
|
|
pqiconnect *pqiperson::getKid(uint32_t type)
|
|
|
|
{
|
|
|
|
std::map<uint32_t, pqiconnect *>::iterator it;
|
|
|
|
|
2010-02-25 17:44:04 -05:00
|
|
|
if (kids.empty()) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-12-13 16:59:26 -05:00
|
|
|
it = kids.find(type);
|
|
|
|
if (it == kids.end())
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
float pqiperson::getRate(bool in)
|
|
|
|
{
|
|
|
|
// get the rate from the active one.
|
|
|
|
if ((!active) || (activepqi == NULL))
|
|
|
|
return 0;
|
|
|
|
return activepqi -> getRate(in);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pqiperson::setMaxRate(bool in, float val)
|
|
|
|
{
|
|
|
|
// set to all of them. (and us)
|
|
|
|
PQInterface::setMaxRate(in, val);
|
|
|
|
// clean up the children.
|
2008-01-25 01:36:40 -05:00
|
|
|
std::map<uint32_t, pqiconnect *>::iterator it;
|
2007-11-14 22:18:48 -05:00
|
|
|
for(it = kids.begin(); it != kids.end(); it++)
|
|
|
|
{
|
2008-01-25 01:36:40 -05:00
|
|
|
(it->second) -> setMaxRate(in, val);
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-08 13:40:09 -05:00
|
|
|
void pqiperson::setRateCap(float val_in, float val_out)
|
|
|
|
{
|
|
|
|
// set to all of them. (and us)
|
|
|
|
PQInterface::setRateCap(val_in, val_out);
|
|
|
|
// clean up the children.
|
|
|
|
std::map<uint32_t, pqiconnect *>::iterator it;
|
|
|
|
for(it = kids.begin(); it != kids.end(); it++)
|
|
|
|
{
|
|
|
|
(it->second) -> setRateCap(val_in, val_out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|