[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    branches/work/kdepim-3.5.5+/kpilot/lib
From:       Jason vanRijn Kasper <vR () movingparts ! net>
Date:       2007-03-07 7:26:55
Message-ID: 1173252415.037334.15852.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 640165 by vanrijn:

- for starters, we don't need to keep track of 2 different sockets.  taking
  jpilot's lead and only tracking the one that matters
- cleaning up debugging format
- we CANNOT use pi_close() and then ::close(), apparently.  libusb is a
  funky, ugly, weird creature.  pilot-link actually creates a pthread to
  get libusb to work. this seems to be working now.  
- hmmm. just noticed that we seem to be eating sockets for lunch now.  will
  have to look into freeing the temp socket tomorrow...


 M  +86 -146   branches/work/kdepim-3.5.5+/kpilot/lib/kpilotdevicelink.cc  
 M  +2 -3      branches/work/kdepim-3.5.5+/kpilot/lib/kpilotdevicelink.h  


--- branches/work/kdepim-3.5.5+/kpilot/lib/kpilotdevicelink.cc #640164:640165
@@ -108,8 +108,10 @@
 	{
 		if ( !(mBoundDevices.count() > 0) ) return;
 		FUNCTIONSETUPL(3);
-		DEBUGKPILOT << fname << ": Bound devices: "
-			<< ((mBoundDevices.count() > 0) ? mBoundDevices.join(CSL1(", ")) : CSL1("<none>")) << endl;
+		DEBUGKPILOT << fname << ": Bound devices: ["
+			<< ((mBoundDevices.count() > 0) ?
+			mBoundDevices.join(CSL1(", ")) : CSL1("<none>")) 
+			<< "]" << endl;
 	}
 } ;
 
@@ -185,8 +187,7 @@
 		: QCustomEvent( type ),
 		fMessage( msg ),
 		fProgress( progress ),
-		fPilotMasterSocket(-1),
-		fPilotCurrentSocket(-1) {}
+		fPilotSocket(-1) {}
 	QString message() const
 	{
 		return fMessage;
@@ -196,19 +197,16 @@
 		return fProgress;
 	}
 
-	inline void setMasterSocket(int i) { fPilotMasterSocket = i; }
-	inline void setCurrentSocket(int i) { fPilotCurrentSocket = i; }
+	inline void setCurrentSocket(int i) { fPilotSocket = i; }
 
-	inline int masterSocket() { return fPilotMasterSocket; }
-	inline int currentSocket() { return fPilotCurrentSocket; }
+	inline int currentSocket() { return fPilotSocket; }
 private:
 	QString fMessage;
 	int fProgress;
 	/**
 	* Pilot-link library handles for the device once it's opened.
 	*/
-	int fPilotMasterSocket;
-	int fPilotCurrentSocket;
+	int fPilotSocket;
 };
 
 
@@ -219,19 +217,19 @@
 
 class DeviceCommThread : public QThread
 {
+friend class KPilotDeviceLink;
 public:
 	DeviceCommThread(KPilotDeviceLink *d) :
 		QThread(),
 		fDone(true),
 		fHandle(d),
-		fPilotMasterSocket(-1),
-		fPilotCurrentSocket(-1)
+		fPilotSocket(-1)
 	{ };
 	virtual ~DeviceCommThread();
 
 	virtual void run();
 
-	static const int SecondsBetweenPoll = 1;
+	static const int SecondsBetweenPoll = 2;
 
 	void setDone(bool b) 
 	{ 
@@ -263,8 +261,7 @@
 	/**
 	* Pilot-link library handles for the device once it's opened.
 	*/
-	int fPilotMasterSocket;
-	int fPilotCurrentSocket;
+	int fPilotSocket;
 
 protected:
 	/**
@@ -275,8 +272,6 @@
 	*/
 	bool openDevice();
 
-	bool acceptDevice();
-
 	void close();
 
 protected:
@@ -373,20 +368,13 @@
 	int e = 0;
 	QString msg;
 
-	if (fPilotCurrentSocket != -1)
-	{
-		pi_close(fPilotCurrentSocket);
-		::close(fPilotCurrentSocket);
-	}
-	fPilotCurrentSocket = (-1);
-
 	link()->fRealPilotPath = KStandardDirs::realFilePath(device.isEmpty() ? link()->fPilotPath : device );
 
 	if ( !DeviceMap::self()->canBind( link()->fRealPilotPath ) ) {
 		msg = i18n("Already listening on that device");
 		
-		WARNINGKPILOT << "Pilot Path "
-			<< link()->fRealPilotPath << " already connected." << endl;
+		WARNINGKPILOT << "Pilot Path: ["
+			<< link()->fRealPilotPath << "] already connected." << endl;
 		WARNINGKPILOT << msg << endl;
 
 		link()->fLinkStatus = PilotLinkError;
@@ -398,56 +386,40 @@
 	}
 
 
-	if (fPilotMasterSocket == -1)
-	{
-		DEBUGKPILOT << fname << ": Trying to create master socket." << endl;
+	DEBUGKPILOT << fname << ": Trying to create socket." << endl;
 
-		fPilotMasterSocket = pi_socket(PI_AF_PILOT,
-			PI_SOCK_STREAM, PI_PF_DLP);
+	fPilotSocket = pi_socket(PI_AF_PILOT, PI_SOCK_STREAM, PI_PF_DLP);
 
-		if (fPilotMasterSocket < 0)
-		{
-			e = errno;
-			msg = i18n("Cannot create socket for communicating "
-				"with the Pilot (%1)").arg(errorMessage(e));
-			WARNINGKPILOT << msg << endl;
-			WARNINGKPILOT << "(" << strerror(e) << ")" << endl;
+	if (fPilotSocket < 0)
+	{
+		e = errno;
+		msg = i18n("Cannot create socket for communicating "
+			"with the Pilot (%1)").arg(errorMessage(e));
+		WARNINGKPILOT << msg << endl;
+		WARNINGKPILOT << "(" << strerror(e) << ")" << endl;
+	
+		link()->fLinkStatus = PilotLinkError;
 		
-			link()->fLinkStatus = PilotLinkError;
-		
-			QApplication::postEvent(link(),
-				new DeviceCommEvent(EventLogError, msg));
-		
-			return false;
-		}
-
-		DEBUGKPILOT << fname
-			<< ": Got master " << fPilotMasterSocket << endl;
-
-		link()->fLinkStatus = CreatedSocket;
+		QApplication::postEvent(link(),
+			new DeviceCommEvent(EventLogError, msg));
 	
-		Q_ASSERT(link()->fLinkStatus == CreatedSocket);
-
+		return false;
 	}
 
-	DEBUGKPILOT << fname << ": Binding to path "
-		<< link()->fRealPilotPath << endl;
+	DEBUGKPILOT << fname << ": Got socket: [" << fPilotSocket << "]" << endl;
 
-	ret = pi_bind(fPilotMasterSocket, QFile::encodeName(link()->fRealPilotPath));
+	link()->fLinkStatus = CreatedSocket;
 
-	if (ret >= 0)
-	{
-		link()->fLinkStatus = DeviceOpen;
+	DEBUGKPILOT << fname << ": Binding to path: [" 
+		<< link()->fRealPilotPath << "]" << endl;
 
-		DeviceMap::self()->bindDevice( link()->fRealPilotPath );
+	ret = pi_bind(fPilotSocket, QFile::encodeName(link()->fRealPilotPath));
 
-		return acceptDevice();
-	}
-	else
+	if (ret < 0)
 	{
 		DEBUGKPILOT << fname
-			<< ": Tried " << link()->fRealPilotPath << " and got "
-			<< strerror(errno) << endl;
+			<< ": pi_bind error: ["
+			<< strerror(errno) << "]" << endl;
 
 		e = errno;
 		msg = i18n("Cannot open Pilot port \"%1\". ").arg(link()->fRealPilotPath);
@@ -466,25 +438,20 @@
 		return false;
 	}
 
-}
+	link()->fLinkStatus = DeviceOpen;
+	DeviceMap::self()->bindDevice( link()->fRealPilotPath );
 
-bool DeviceCommThread::acceptDevice()
-{
-	FUNCTIONSETUP;
-
-	int ret;
-
 	DEBUGKPILOT << fname
-		<< ": Current status "
+		<< ": Current status: ["
 		<< link()->statusString()
-		<< " and master " << fPilotMasterSocket << endl;
+		<< "] and socket: [" << fPilotSocket << "]" << endl;
 
-	ret = pi_listen(fPilotMasterSocket, 1);
+	ret = pi_listen(fPilotSocket, 1);
 	if (ret < 0)
 	{
 		char *s = strerror(errno);
 
-		WARNINGKPILOT << "pi_listen returned: " << s << endl;
+		WARNINGKPILOT << "pi_listen returned: [" << s << "]" << endl;
 
 		// Presumably, strerror() returns things in
 		// local8Bit and not latin1.
@@ -507,13 +474,13 @@
 	if (link()->fWorkaroundUSB)
 		timeout=10;
 
-	fPilotCurrentSocket = pi_accept_to(fPilotMasterSocket, 0, 0, timeout);
+	fPilotSocket = pi_accept_to(fPilotSocket, 0, 0, timeout);
 
-	if (fPilotCurrentSocket < 0)
+	if (fPilotSocket < 0)
 	{
 		char *s = strerror(errno);
 
-		WARNINGKPILOT << "pi_accept returned: " << s << endl;
+		WARNINGKPILOT << "pi_accept returned: [" << s << "]" << endl;
 
 		QApplication::postEvent(link(),
 			new DeviceCommEvent(EventLogError, i18n("Cannot accept Pilot (%1)")
@@ -523,10 +490,9 @@
 		return false;
 	}
 
-	DEBUGKPILOT << fname <<
-		": Link accept done." << endl;
+	DEBUGKPILOT << fname << ": Link accept done." << endl;
 
-	if ((link()->fLinkStatus != DeviceOpen) || (fPilotMasterSocket == -1))
+	if ((link()->fLinkStatus != DeviceOpen) || (fPilotSocket == -1))
 	{
 		link()->fLinkStatus = PilotLinkError;
 		WARNINGKPILOT << "Already connected or unable to connect!" << endl;
@@ -543,7 +509,7 @@
 
         KPILOT_DELETE(link()->fPilotSysInfo);
 	link()->fPilotSysInfo = new KPilotSysInfo();
-	if (dlp_ReadSysInfo(fPilotCurrentSocket, link()->fPilotSysInfo->sysInfo()) < 0)
+	if (dlp_ReadSysInfo(fPilotSocket, link()->fPilotSysInfo->sysInfo()) < 0)
 	{
 		QApplication::postEvent(link(),
 			new DeviceCommEvent(EventLogError,
@@ -555,10 +521,10 @@
 	else
 	{
 		DEBUGKPILOT << fname
-			<< ": RomVersion=" << link()->fPilotSysInfo->getRomVersion()
-			<< " Locale=" << link()->fPilotSysInfo->getLocale()
-			<< " Product=" << link()->fPilotSysInfo->getProductID()
-			<< endl;
+			<< ": RomVersion: [" << link()->fPilotSysInfo->getRomVersion()
+			<< "] Locale: [" << link()->fPilotSysInfo->getLocale()
+			<< "] Product: [" << link()->fPilotSysInfo->getProductID()
+			<< "]" << endl;
 	}
 
 	QApplication::postEvent(link(),
@@ -568,22 +534,20 @@
 	link()->fPilotUser = new KPilotUser;
 
 	/* Ask the pilot who it is.  And see if it's who we think it is. */
-	dlp_ReadUserInfo(fPilotCurrentSocket, link()->fPilotUser->data());
+	dlp_ReadUserInfo(fPilotSocket, link()->fPilotUser->data());
 
 	QString n = link()->getPilotUser().name();
 	DEBUGKPILOT << fname
-		<< ": Read user name "
-		<< n
-		<< endl;
+		<< ": Read user name: [" << n << "]" << endl;
 
 	QApplication::postEvent(link(),
 		new DeviceCommEvent(EventLogProgress, i18n("Checking last PC..."), 90));
 
 	/* Tell user (via Pilot) that we are starting things up */
-	if ((ret=dlp_OpenConduit(fPilotCurrentSocket)) < 0)
+	if ((ret=dlp_OpenConduit(fPilotSocket)) < 0)
 	{
 		DEBUGKPILOT << fname
-			<< ": dlp_OpenConduit returned " << ret << endl;
+			<< ": dlp_OpenConduit returned: [" << ret << "]" << endl;
 
 		QApplication::postEvent(link(),
 			new DeviceCommEvent(EventLogError,
@@ -603,26 +567,15 @@
 {
 	FUNCTIONSETUP;
 
-	DEBUGKPILOT << fname
-		<< ": device comm thread closing sockets "
-		<< fPilotCurrentSocket
-		<< " and "
-		<< fPilotMasterSocket
-		<< endl;
-
-	if (fPilotCurrentSocket != -1)
+	if (fPilotSocket != -1)
 	{
-		pi_close(fPilotCurrentSocket);
-		::close(fPilotCurrentSocket);
-	}
-	fPilotCurrentSocket = (-1);
+		DEBUGKPILOT << fname
+			<< ": device comm thread closing socket: ["
+			<< fPilotSocket << "]" << endl;
 
-	if (fPilotMasterSocket != -1)
-	{
-		pi_close(fPilotMasterSocket);
-		::close(fPilotMasterSocket);
+		pi_close(fPilotSocket);
 	}
-	fPilotMasterSocket = (-1);
+	fPilotSocket = (-1);
 
 	DeviceMap::self()->unbindDevice( link()->fRealPilotPath );
 }
@@ -636,9 +589,6 @@
 
 	DEBUGKPILOT << fname << ": Polling every: ["
 		<< sleepBetweenPoll << "] seconds." << endl;
-	DEBUGKPILOT << fname << ": This: [@" << (void *) this 
-		<< "], handle: [@" << (void *) link() 
-		<< "]." << endl;
 
 	while (!fDone)
 	{
@@ -647,11 +597,10 @@
 		if (link()->fLinkStatus == AcceptedDevice)
 		{
 			DeviceCommEvent * ev = new DeviceCommEvent(EventDeviceReady);
-			ev->setMasterSocket(fPilotMasterSocket);
-			ev->setCurrentSocket(fPilotCurrentSocket);
+			ev->setCurrentSocket(fPilotSocket);
 			QApplication::postEvent(link(), ev);
 
-			fDone = true;
+			break;
 		}
 		else
 		{
@@ -667,12 +616,22 @@
 			// sleep before trying again
 			QThread::sleep(sleepBetweenPoll);
 		}
+	}
 
-
+	// keep the thread alive until we're supposed to be done
+	while (!fDone)
+	{
+		DEBUGKPILOT << fname << ": comm thread waiting to be done..." << endl;
+		QThread::sleep(sleepBetweenPoll);
 	}
-}
 
+	close();
+	// now sleep one last bit to make sure the pthread inside
+	// pilot-link (potentially, if it's libusb) is done before we exit
+	QThread::sleep(5);
 
+	DEBUGKPILOT << fname << ": comm thread now done..." << endl;
+}
 
 
 
@@ -681,8 +640,7 @@
 	KPilotLink(parent, name),
 	fLinkStatus(Init),
 	fWorkaroundUSB(false),
-	fPilotMasterSocket(-1),
-	fPilotCurrentSocket(-1),
+	fPilotSocket(-1),
 	fTempDevice(tempDevice),
 	fMessages(new Messages(this)),
 	fDeviceCommThread(0L)
@@ -690,8 +648,8 @@
 	FUNCTIONSETUP;
 
 	DEBUGKPILOT << fname
-		<< ": Pilot-link version " << PILOT_LINK_NUMBER
-		<< endl;
+		<< ": Pilot-link version: [" << PILOT_LINK_NUMBER
+		<< "]" << endl;
 }
 
 KPilotDeviceLink::~KPilotDeviceLink()
@@ -713,8 +671,7 @@
 	if ((int)e->type() == EventDeviceReady)
 	{
 		DeviceCommEvent* t = dynamic_cast<DeviceCommEvent*>(e);
-		fPilotMasterSocket = t->masterSocket();
-		fPilotCurrentSocket = t->currentSocket();
+		fPilotSocket = t->currentSocket();
 		emit deviceReady( this );
 	}
 	else if ((int)e->type() == EventLogMessage)
@@ -767,6 +724,8 @@
 			}
 		}
 
+		fDeviceCommThread->close();
+
 		KPILOT_DELETE(fDeviceCommThread);
 	}
 }
@@ -777,26 +736,7 @@
 
 	stopCommThread();
 
-	DEBUGKPILOT << fname
-		<< ": Device link closing sockets "
-		<< fPilotCurrentSocket
-		<< " and "
-		<< fPilotMasterSocket
-		<< endl;
-
-	if (fPilotCurrentSocket != -1)
-	{
-		pi_close(fPilotCurrentSocket);
-		::close(fPilotCurrentSocket);
-	}
-	if (fPilotMasterSocket != -1)
-	{
-		pi_close(fPilotMasterSocket);
-		::close(fPilotMasterSocket);
-	}
-	DeviceMap::self()->unbindDevice( fRealPilotPath );
-	fPilotMasterSocket = (-1);
-	fPilotCurrentSocket = (-1);
+	fPilotSocket = (-1);
 }
 
 void KPilotDeviceLink::reset(const QString & dP)
@@ -904,7 +844,7 @@
 
 /* virtual */ void KPilotDeviceLink::addSyncLogEntryImpl( const QString &entry )
 {
-	dlp_AddSyncLogEntry(fPilotCurrentSocket,
+	dlp_AddSyncLogEntry(fPilotSocket,
 		const_cast<char *>((const char *)Pilot::toPilot(entry)));
 }
 
@@ -932,7 +872,7 @@
 		return false;
 	}
 
-	if (pi_file_install(pf, fPilotCurrentSocket, 0, 0L) < 0)
+	if (pi_file_install(pf, fPilotSocket, 0, 0L) < 0)
 	{
 		WARNINGKPILOT << "Cannot pi_file_install " << f << endl;
 		emit logError(i18n
@@ -950,7 +890,7 @@
 
 int KPilotDeviceLink::openConduit()
 {
-	return dlp_OpenConduit(fPilotCurrentSocket);
+	return dlp_OpenConduit(fPilotSocket);
 }
 
 QString KPilotDeviceLink::statusString(LinkStatus l)
--- branches/work/kdepim-3.5.5+/kpilot/lib/kpilotdevicelink.h #640164:640165
@@ -135,7 +135,7 @@
 	virtual void addSyncLogEntryImpl( const QString &s );
 	virtual int pilotSocket() const
 	{
-		return fPilotCurrentSocket;
+		return fPilotSocket;
 	}
 
 
@@ -196,8 +196,7 @@
 	/**
 	* Pilot-link library handles for the device once it's opened.
 	*/
-	int fPilotMasterSocket;
-	int fPilotCurrentSocket;
+	int fPilotSocket;
 	QString fTempDevice;
 
 	/**
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic