[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-02-20 7:02:25
Message-ID: 1171954945.459308.22922.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 635491 by vanrijn:

- making naming consistent for fPilotMasterSocket and fPilotCurrentSocket
- changing thread to not touch sockets directly.  it has its own sockets
  now which are passed back in the DeviceCommEvent when they're ready.
- adding debugging for stopping the thread.  this seems to be working, but
  when something goes wrong, it's good to have debugging.  =:/


 M  +91 -55    branches/work/kdepim-3.5.5+/kpilot/lib/kpilotdevicelink.cc  
 M  +2 -2      branches/work/kdepim-3.5.5+/kpilot/lib/kpilotdevicelink.h  


--- branches/work/kdepim-3.5.5+/kpilot/lib/kpilotdevicelink.cc #635490:635491
@@ -184,7 +184,9 @@
 	DeviceCommEvent ( DeviceCustomEvents type, QString msg = QString::null, int progress = 0)
 		: QCustomEvent( type ),
 		fMessage( msg ),
-		fProgress( progress ) {}
+		fProgress( progress ),
+		fPilotMasterSocket(-1),
+		fPilotCurrentSocket(-1) {}
 	QString message() const
 	{
 		return fMessage;
@@ -193,9 +195,20 @@
 	{
 		return fProgress;
 	}
+
+	inline void setMasterSocket(int i) { fPilotMasterSocket = i; }
+	inline void setCurrentSocket(int i) { fPilotCurrentSocket = i; }
+
+	inline int masterSocket() { return fPilotMasterSocket; }
+	inline int currentSocket() { return fPilotCurrentSocket; }
 private:
 	QString fMessage;
 	int fProgress;
+	/**
+	* Pilot-link library handles for the device once it's opened.
+	*/
+	int fPilotMasterSocket;
+	int fPilotCurrentSocket;
 };
 
 
@@ -209,7 +222,9 @@
 	DeviceCommThread(KPilotDeviceLink *d) :
 		QThread(),
 		fDone(true),
-		fHandle(d)
+		fHandle(d),
+		fPilotMasterSocket(-1),
+		fPilotCurrentSocket(-1)
 	{ };
 	virtual ~DeviceCommThread();
 
@@ -244,6 +259,12 @@
 		}
 	}
 
+	/**
+	* Pilot-link library handles for the device once it's opened.
+	*/
+	int fPilotMasterSocket;
+	int fPilotCurrentSocket;
+
 protected slots:
 	/**
 	* Attempt to open the device. Called regularly to check
@@ -349,12 +370,12 @@
 	int e = 0;
 	QString msg;
 
-	if (link()->fCurrentPilotSocket != -1)
+	if (fPilotCurrentSocket != -1)
 	{
-		pi_close(link()->fCurrentPilotSocket);
-		::close(link()->fCurrentPilotSocket);
+		pi_close(fPilotCurrentSocket);
+		::close(fPilotCurrentSocket);
 	}
-	link()->fCurrentPilotSocket = (-1);
+	fPilotCurrentSocket = (-1);
 
 	link()->fRealPilotPath = KStandardDirs::realFilePath(device.isEmpty() ? link()->fPilotPath : device );
 
@@ -374,14 +395,14 @@
 	}
 
 
-	if (link()->fPilotMasterSocket == -1)
+	if (fPilotMasterSocket == -1)
 	{
 		DEBUGKPILOT << fname << ": Trying to create master socket." << endl;
 
-		link()->fPilotMasterSocket = pi_socket(PI_AF_PILOT,
+		fPilotMasterSocket = pi_socket(PI_AF_PILOT,
 			PI_SOCK_STREAM, PI_PF_DLP);
 
-		if (link()->fPilotMasterSocket < 0)
+		if (fPilotMasterSocket < 0)
 		{
 			e = errno;
 			msg = i18n("Cannot create socket for communicating "
@@ -398,7 +419,7 @@
 		}
 
 		DEBUGKPILOT << fname
-			<< ": Got master " << link()->fPilotMasterSocket << endl;
+			<< ": Got master " << fPilotMasterSocket << endl;
 
 		link()->fLinkStatus = CreatedSocket;
 	
@@ -409,7 +430,7 @@
 	DEBUGKPILOT << fname << ": Binding to path "
 		<< link()->fRealPilotPath << endl;
 
-	ret = pi_bind(link()->fPilotMasterSocket, QFile::encodeName(link()->fRealPilotPath));
+	ret = pi_bind(fPilotMasterSocket, QFile::encodeName(link()->fRealPilotPath));
 
 	if (ret >= 0)
 	{
@@ -438,7 +459,7 @@
 			QApplication::postEvent(link(),
 				new DeviceCommEvent(EventLogError, msg));
 		}
-	
+
 		return false;
 	}
 
@@ -453,9 +474,9 @@
 	DEBUGKPILOT << fname
 		<< ": Current status "
 		<< link()->statusString()
-		<< " and master " << link()->fPilotMasterSocket << endl;
+		<< " and master " << fPilotMasterSocket << endl;
 
-	ret = pi_listen(link()->fPilotMasterSocket, 1);
+	ret = pi_listen(fPilotMasterSocket, 1);
 	if (ret < 0)
 	{
 		char *s = strerror(errno);
@@ -470,7 +491,6 @@
 				arg(QString::fromLocal8Bit(s)))
 			);
 
-		close();
 		return false;
 	}
 
@@ -484,9 +504,9 @@
 	if (link()->fWorkaroundUSB)
 		timeout=10;
 
-	link()->fCurrentPilotSocket = pi_accept_to(link()->fPilotMasterSocket, 0, 0, timeout);
+	fPilotCurrentSocket = pi_accept_to(fPilotMasterSocket, 0, 0, timeout);
 
-	if (link()->fCurrentPilotSocket < 0)
+	if (fPilotCurrentSocket < 0)
 	{
 		char *s = strerror(errno);
 
@@ -497,14 +517,13 @@
 			.arg(QString::fromLocal8Bit(s))));
 
 		link()->fLinkStatus = PilotLinkError;
-		close();
 		return false;
 	}
 
 	DEBUGKPILOT << fname <<
 		": Link accept done." << endl;
 
-	if ((link()->fLinkStatus != DeviceOpen) || (link()->fPilotMasterSocket == -1))
+	if ((link()->fLinkStatus != DeviceOpen) || (fPilotMasterSocket == -1))
 	{
 		link()->fLinkStatus = PilotLinkError;
 		WARNINGKPILOT << "Already connected or unable to connect!" << endl;
@@ -513,7 +532,6 @@
 			new DeviceCommEvent(EventLogError, i18n("Cannot accept Pilot (%1)")
 			.arg(i18n("already connected"))));
 
-		close();
 		return false;
 	}
 
@@ -522,14 +540,13 @@
 
         KPILOT_DELETE(link()->fPilotSysInfo);
 	link()->fPilotSysInfo = new KPilotSysInfo();
-	if (dlp_ReadSysInfo(link()->fCurrentPilotSocket, link()->fPilotSysInfo->sysInfo()) < 0)
+	if (dlp_ReadSysInfo(fPilotCurrentSocket, link()->fPilotSysInfo->sysInfo()) < 0)
 	{
 		QApplication::postEvent(link(),
 			new DeviceCommEvent(EventLogError,
 			i18n("Unable to read system information from Pilot")));
 
 		link()->fLinkStatus=PilotLinkError;
-		close();
 		return false;
 	}
 	else
@@ -548,7 +565,7 @@
 	link()->fPilotUser = new KPilotUser;
 
 	/* Ask the pilot who it is.  And see if it's who we think it is. */
-	dlp_ReadUserInfo(link()->fCurrentPilotSocket, link()->fPilotUser->data());
+	dlp_ReadUserInfo(fPilotCurrentSocket, link()->fPilotUser->data());
 
 	QString n = link()->getPilotUser().name();
 	DEBUGKPILOT << fname
@@ -560,7 +577,7 @@
 		new DeviceCommEvent(EventLogProgress, i18n("Checking last PC..."), 90));
 
 	/* Tell user (via Pilot) that we are starting things up */
-	if ((ret=dlp_OpenConduit(link()->fCurrentPilotSocket)) < 0)
+	if ((ret=dlp_OpenConduit(fPilotCurrentSocket)) < 0)
 	{
 		DEBUGKPILOT << fname
 			<< ": dlp_OpenConduit returned " << ret << endl;
@@ -584,25 +601,27 @@
 	FUNCTIONSETUP;
 
 	DEBUGKPILOT << fname
-		<< ": Closing sockets "
-		<< link()->fCurrentPilotSocket
+		<< ": device comm thread closing sockets "
+		<< fPilotCurrentSocket
 		<< " and "
-		<< link()->fPilotMasterSocket
+		<< fPilotMasterSocket
 		<< endl;
 
-	if (link()->fCurrentPilotSocket != -1)
+	if (fPilotCurrentSocket != -1)
 	{
-		pi_close(link()->fCurrentPilotSocket);
-		::close(link()->fCurrentPilotSocket);
+		pi_close(fPilotCurrentSocket);
+		::close(fPilotCurrentSocket);
 	}
-	if (link()->fPilotMasterSocket != -1)
+	fPilotCurrentSocket = (-1);
+
+	if (fPilotMasterSocket != -1)
 	{
-		pi_close(link()->fPilotMasterSocket);
-		::close(link()->fPilotMasterSocket);
+		pi_close(fPilotMasterSocket);
+		::close(fPilotMasterSocket);
 	}
+	fPilotMasterSocket = (-1);
+
 	DeviceMap::self()->unbindDevice( link()->fRealPilotPath );
-	link()->fPilotMasterSocket = (-1);
-	link()->fCurrentPilotSocket = (-1);
 }
 
 
@@ -624,8 +643,10 @@
 
 		if (link()->fLinkStatus == AcceptedDevice)
 		{
-			QApplication::postEvent(link(),
-				new DeviceCommEvent(EventDeviceReady));
+			DeviceCommEvent * ev = new DeviceCommEvent(EventDeviceReady);
+			ev->setMasterSocket(fPilotMasterSocket);
+			ev->setCurrentSocket(fPilotCurrentSocket);
+			QApplication::postEvent(link(), ev);
 
 			fDone = true;
 		}
@@ -658,7 +679,7 @@
 	fLinkStatus(Init),
 	fWorkaroundUSB(false),
 	fPilotMasterSocket(-1),
-	fCurrentPilotSocket(-1),
+	fPilotCurrentSocket(-1),
 	fTempDevice(tempDevice),
 	fMessages(new Messages(this)),
 	fDeviceCommThread(0L)
@@ -688,6 +709,9 @@
 {
 	if ((int)e->type() == EventDeviceReady)
 	{
+		DeviceCommEvent* t = dynamic_cast<DeviceCommEvent*>(e);
+		fPilotMasterSocket = t->masterSocket();
+		fPilotCurrentSocket = t->currentSocket();
 		emit deviceReady( this );
 	}
 	else if ((int)e->type() == EventLogMessage)
@@ -720,14 +744,26 @@
 
 		// try to wait for our thread to finish, but don't
 		// block the main thread forever
-		fDeviceCommThread->wait(30000);
-
-		if (fDeviceCommThread)
+		if (fDeviceCommThread->running()) 
 		{
-			// not normally to be done, but we must make sure
-			// that this device doesn't come back alive
-			fDeviceCommThread->terminate();
+			DEBUGKPILOT << fname
+				<< ": comm thread still running. "
+				<< "waiting for it to complete." << endl;
+			bool done = fDeviceCommThread->wait(30000);
+			if (!done)
+			{
+				DEBUGKPILOT << fname
+					<< ": comm thread still running "
+					<< "after wait(). "
+					<< "going to have to terminate it."
+					<< endl;
+				// not normally to be done, but we must make sure
+				// that this device doesn't come back alive
+				fDeviceCommThread->terminate();
+				fDeviceCommThread->wait();
+			}
 		}
+
 		KPILOT_DELETE(fDeviceCommThread);
 	}
 }
@@ -739,16 +775,16 @@
 	stopCommThread();
 
 	DEBUGKPILOT << fname
-		<< ": Closing sockets "
-		<< fCurrentPilotSocket
+		<< ": Device link closing sockets "
+		<< fPilotCurrentSocket
 		<< " and "
 		<< fPilotMasterSocket
 		<< endl;
 
-	if (fCurrentPilotSocket != -1)
+	if (fPilotCurrentSocket != -1)
 	{
-		pi_close(fCurrentPilotSocket);
-		::close(fCurrentPilotSocket);
+		pi_close(fPilotCurrentSocket);
+		::close(fPilotCurrentSocket);
 	}
 	if (fPilotMasterSocket != -1)
 	{
@@ -757,7 +793,7 @@
 	}
 	DeviceMap::self()->unbindDevice( fRealPilotPath );
 	fPilotMasterSocket = (-1);
-	fCurrentPilotSocket = (-1);
+	fPilotCurrentSocket = (-1);
 }
 
 void KPilotDeviceLink::reset(const QString & dP)
@@ -815,9 +851,9 @@
 
 	checkDevice();
 
+	fLinkStatus = WaitingForDevice;
+
 	startCommThread();
-
-	fLinkStatus = WaitingForDevice;
 }
 
 void KPilotDeviceLink::checkDevice()
@@ -865,7 +901,7 @@
 
 /* virtual */ void KPilotDeviceLink::addSyncLogEntryImpl( const QString &entry )
 {
-	dlp_AddSyncLogEntry(fCurrentPilotSocket,
+	dlp_AddSyncLogEntry(fPilotCurrentSocket,
 		const_cast<char *>((const char *)Pilot::toPilot(entry)));
 }
 
@@ -893,7 +929,7 @@
 		return false;
 	}
 
-	if (pi_file_install(pf, fCurrentPilotSocket, 0, 0L) < 0)
+	if (pi_file_install(pf, fPilotCurrentSocket, 0, 0L) < 0)
 	{
 		WARNINGKPILOT << "Cannot pi_file_install " << f << endl;
 		emit logError(i18n
@@ -911,7 +947,7 @@
 
 int KPilotDeviceLink::openConduit()
 {
-	return dlp_OpenConduit(fCurrentPilotSocket);
+	return dlp_OpenConduit(fPilotCurrentSocket);
 }
 
 QString KPilotDeviceLink::statusString(LinkStatus l)
--- branches/work/kdepim-3.5.5+/kpilot/lib/kpilotdevicelink.h #635490:635491
@@ -135,7 +135,7 @@
 	virtual void addSyncLogEntryImpl( const QString &s );
 	virtual int pilotSocket() const
 	{
-		return fCurrentPilotSocket;
+		return fPilotCurrentSocket;
 	}
 
 
@@ -197,7 +197,7 @@
 	* Pilot-link library handles for the device once it's opened.
 	*/
 	int fPilotMasterSocket;
-	int fCurrentPilotSocket;
+	int fPilotCurrentSocket;
 	QString fTempDevice;
 
 	/**
[prev in list] [next in list] [prev in thread] [next in thread] 

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