示例#1
0
void ZServer::Start(ZRef<ZCaller> iCaller,
	ZRef<ZStreamerRWFactory> iFactory,
	ZRef<Callable_Connection> iCallable_Connection)
	{
	ZAssert(iCaller);
	ZAssert(iFactory);

	// Declared before the acq, so it goes out of scope after it, and any
	// callable on the roster is invoked with our mutex released.
	ZRef<ZRoster> priorRoster;

	ZAcqMtx acq(fMtx);

	ZAssert(not fWorker);
	ZAssert(not fFactory);
	ZAssert(not fCallable_Connection);

	priorRoster = fRoster;
	fRoster = new ZRoster;

	fFactory = iFactory;
	fCallable_Connection = iCallable_Connection;

	fWorker = new ZWorker(
		sCallable(sWeakRef(this), &ZServer::pWork),
		sCallable(sWeakRef(this), &ZServer::pWorkDetached));

	fWorker->Attach(iCaller);

	fWorker->Wake();
	}
示例#2
0
ZBlackBerryServer::ZBlackBerryServer(ZRef<ZBlackBerry::Manager> iManager)
:	fManager(iManager)
,	fCB_ManagerChanged(sCallable(this, &ZBlackBerryServer::pManagerChanged))
,	fCB_DeviceFinished(sCallable(this, &ZBlackBerryServer::pDeviceFinished))
	{
	fManager->SetCallable(fCB_ManagerChanged);
	this->pManagerChanged(fManager);
	}
示例#3
0
bool ZServer::pWork(ZRef<ZWorker> iWorker)
	{
	ZGuardMtx guard(fMtx);

	if (ZRef<ZStreamerRWFactory> theFactory = fFactory)
		{
		guard.Release();
		if (ZRef<ZStreamerRW> theSRW = theFactory->MakeStreamerRW())
			{
			guard.Acquire();
			if (ZRef<Callable_Connection> theCallable = fCallable_Connection)
				{
				guard.Release();
				try
					{
					ZRef<ZCallable_Void> theCallable_Kill =
						sBindR(sCallable(spKill), theSRW.DynamicCast<ZStreamerRWCon>());
					theCallable->Call(fRoster->MakeEntry(theCallable_Kill, null), theSRW);
					}
				catch (...)
					{}
				}
			}
		iWorker->Wake();
		return true;
		}
	return false;
	}
示例#4
0
ZRef<Callable_PullSuggested> Searcher_DatonSet::GetCallable_PullSuggested()
	{
	ZAcqMtxR acq(fMtxR);
	if (not fCallable_PullSuggested_Self)
		fCallable_PullSuggested_Self = sCallable(sWeakRef(this), &Searcher_DatonSet::pPullSuggested);
	return fCallable_PullSuggested_Self;
	}
ZRef<Expr_Bool> sQFromStrim(const ChanRU_UTF& iChanRU)
	{
	return Util_Strim_Expr_Bool::sQFromStrim(sCallable(spQReadValPred), iChanRU);
	}
示例#6
0
void sWrite(bool iPrettyPrint, const Any& iVal, const ChanW_UTF& iChanW)
	{
	PullPushPair<PPT> thePair = sMakePullPushPair<PPT>();
	sStartOnNewThread(sBindR(sCallable(spFromAny_Push_PPT), iVal, sGetClear(thePair.first)));
	sPull_PPT_Push_JSON(*thePair.second, 0, PushTextOptions(iPrettyPrint), iChanW);
	}
示例#7
0
void ZBlackBerryServer::HandleRequest(ZRef<ZStreamerRWCon> iSRWCon)
	{
	const ZStreamR& r = iSRWCon->GetStreamR();
	const ZStreamW& w = iSRWCon->GetStreamW();

	const int req = r.ReadUInt8();
	if (req == 0)
		{
		// Async changed notifications
		ZGuardRMtxR locker(fMutex);
		ZRef<Handler_ManagerChanged> theHandler = new Handler_ManagerChanged(iSRWCon, iSRWCon, this);
		fHandlers_ManagerChanged.push_back(theHandler);
		locker.Release();

		sStartCommerRunners(theHandler);
		}
	else if (req == 1)
		{
		// Synchronous get device IDs
		ZGuardRMtxR locker(fMutex);

		vector<uint64> theIDs;
		for (vector<Entry_t>::iterator i = fEntries.begin(); i != fEntries.end(); ++i)
			{
			if (i->fLive)
				theIDs.push_back(i->fID);
			}
		locker.Release();

		w.WriteCount(theIDs.size());
		for (vector<uint64>::iterator i = theIDs.begin(); i != theIDs.end(); ++i)
			w.WriteUInt64(*i);
		}
	else if (req == 2)
		{
		// Async device finished notifications
		const uint64 deviceID = r.ReadUInt64();

		ZGuardRMtxR locker(fMutex);

		bool gotIt = false;
		for (vector<Entry_t>::iterator i = fEntries.begin(); !gotIt && i != fEntries.end(); ++i)
			{
			if (i->fLive && i->fID == deviceID)
				{
				ZRef<Handler_DeviceFinished> theHandler =
					new Handler_DeviceFinished(iSRWCon, iSRWCon, this);
				i->fHandlers.push_back(theHandler);
				locker.Release();

				w.WriteBool(true);
				sStartCommerRunners(theHandler);
				gotIt = true;
				}
			}

		if (!gotIt)
			{
			locker.Release();
			w.WriteBool(false);
			}
		}
	else if (req == 4)
		{
		// Synchronous get attribute
		const uint64 deviceID = r.ReadUInt64();
		const uint16 object = r.ReadUInt16();
		const uint16 attribute = r.ReadUInt16();
		if (ZRef<ZBlackBerry::Device> theDevice = this->pGetDevice(deviceID))
			{
			if (ZQ<ZBlackBerry::Data> theQ = theDevice->GetAttribute(object, attribute))
				{
				ZBlackBerry::Data theMB = theQ.Get();
				w.WriteBool(true);
				w.WriteCount(theMB.GetSize());
				w.Write(theMB.GetPtr(), theMB.GetSize());
				return;
				}
			}
		w.WriteBool(false);
		}
	else if (req == 5)
		{
		// Synchronous get PIN
		const uint64 deviceID = r.ReadUInt64();
		if (ZRef<ZBlackBerry::Device> theDevice = this->pGetDevice(deviceID))
			{
			uint32 thePIN = theDevice->GetPIN();
			w.WriteBool(true);
			w.WriteUInt32(thePIN);
			}
		else
			{
			w.WriteBool(false);
			}
		}
	else if (req == 6)
		{
		// Open channel
		const uint64 deviceID = r.ReadUInt64();

		const bool preserveBoundaries = r.ReadBool();

		const bool gotHash = r.ReadBool();
		ZBlackBerry::PasswordHash thePasswordHash;
		if (gotHash)
			r.Read(&thePasswordHash, sizeof(thePasswordHash));

		const string channelName = spReadString(r);

		ZBlackBerry::Device::Error theError = ZBlackBerry::Device::error_DeviceClosed;

		if (ZRef<ZBlackBerry::Device> theDevice = this->pGetDevice(deviceID))
			{
			if (ZRef<ZBlackBerry::Channel> deviceCon = theDevice->Open(preserveBoundaries,
				channelName, gotHash ? &thePasswordHash : nullptr, &theError))
				{
				const size_t readSize = deviceCon->GetIdealSize_Read();
				const size_t writeSize = deviceCon->GetIdealSize_Write();
				w.WriteUInt32(ZBlackBerry::Device::error_None);
				w.WriteUInt32(readSize);
				w.WriteUInt32(writeSize);
				w.Flush();

				// Use a standard copier for the device-->client direction
				sCallOnNewThread(
					sBindR(sCallable(&spCopyAllCon), deviceCon, iSRWCon, readSize));

				// And our specialized copier for the client-->device direction.
				sCallOnNewThread(
					sBindR(sCallable(&spCopyChunked), iSRWCon, deviceCon));
				return;
				}
			}

		if (theError == ZBlackBerry::Device::error_None)
			{
			if (ZLOG(s, eDebug + 1, "ZBlackBerryServer::HandleRequest"))
				{
				s << "Open failed, but got error_None";
				}
			theError = ZBlackBerry::Device::error_Generic;
			}
		w.WriteUInt32(theError);
		}
	}