void Run () {
		CSettings settings;
		ASSERT (settings.GetConnectionPipe ());
		LOGDEBUG (TEXT ("Connecting to ") << settings.GetConnectionPipe ());
		CNamedPipe *poPipe = CNamedPipe::ClientWrite (settings.GetConnectionPipe ());
		ASSERT (poPipe);
		LOGDEBUG (TEXT ("Client connected"));
		ClientConnect cc;
		memset (&cc, 0, sizeof (cc));
		cc._userName = TEST_USERNAME;
		cc._CPPToJavaPipe = TEST_CPP2JAVA;
		cc._JavaToCPPPipe = TEST_JAVA2CPP;
		cc._languageID = TEST_LANGUAGE;
#ifdef _DEBUG
		cc._debug = FUDGE_TRUE;
#endif /* ifdef _DEBUG */
		FudgeMsg msg;
		ASSERT (ClientConnect_toFudgeMsg (&cc, &msg) == FUDGE_OK);
		FudgeMsgEnvelope env;
		ASSERT (FudgeMsgEnvelope_create (&env, 0, 0, 0, msg) == FUDGE_OK);
		fudge_byte *ptrBuffer;
		fudge_i32 cbBuffer;
		ASSERT (FudgeCodec_encodeMsg (env, &ptrBuffer, &cbBuffer) == FUDGE_OK);
		FudgeMsgEnvelope_release (env);
		FudgeMsg_release (msg);
		LOGDEBUG (TEXT ("Writing connection packet"));
		ASSERT (poPipe->Write (ptrBuffer, cbBuffer, TIMEOUT_CONNECT) == (size_t)cbBuffer);
		LOGDEBUG (TEXT ("Connection packet written"));
		delete ptrBuffer;
		LOGDEBUG (TEXT ("Disconnecting"));
		delete poPipe;
	}
Example #2
0
bool CClientService::ConnectPipes () {
	LOGINFO (TEXT ("Connecting pipes to JVM"));
	m_oPipesSemaphore.Wait ();
	if (!m_poPipes) {
		m_oPipesSemaphore.Signal ();
		LOGFATAL (TEXT ("Pipes not created"));
		assert (0);
		return false;
	}
	if (!m_poJVM) {
		m_oPipesSemaphore.Signal ();
		LOGFATAL (TEXT ("JVM not created"));
		assert (0);
		return false;
	}
	CSettings oSettings;
	const TCHAR *pszPipeName = oSettings.GetConnectionPipe ();
	LOGDEBUG (TEXT ("Connecting to ") << pszPipeName);
	unsigned long lTimeout = m_poJVM->FirstConnection () ? oSettings.GetStartTimeout () : oSettings.GetConnectTimeout ();
	m_lSendTimeout = lTimeout;
	m_lShortTimeout = oSettings.GetSendTimeout ();
	unsigned long lTime = GetTickCount ();
	CNamedPipe *poPipe;
	do {
		poPipe = CNamedPipe::ClientWrite (pszPipeName);
		if (poPipe) {
			break;
		} else {
			int ec = GetLastError ();
			if (ec == ENOENT) {
				if (GetTickCount () - lTime > lTimeout) {
					m_oPipesSemaphore.Signal ();
					LOGWARN (TEXT ("Timeout waiting for JVM service to open ") << pszPipeName);
					return false;
				} else {
					if (m_poJVM->IsAlive ()) {
						LOGDEBUG (TEXT ("Waiting for JVM service to open pipe"));
						CThread::Sleep (oSettings.GetServicePoll ());
					} else {
						m_oPipesSemaphore.Signal ();
						LOGWARN (TEXT ("JVM service terminated before opening ") << pszPipeName);
						return false;
					}
				}
			} else {
				m_oPipesSemaphore.Signal ();
				LOGWARN (TEXT ("Couldn't connect to ") << pszPipeName << TEXT (", error ") << ec);
				return false;
			}
		}
	} while (true);
	LOGDEBUG (TEXT ("Connected to service"));
	bool bResult = m_poPipes->Connect (m_pszLanguageID, poPipe, lTimeout);
	if (!bResult) {
		LOGWARN (TEXT ("Couldn't connect to JVM service, error ") << GetLastError ());
	}
	delete poPipe;
	m_oPipesSemaphore.Signal ();
	return bResult;
}