2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 // seems to be required for str.h
26 #include <glib/gstdio.h>
29 #if defined (__linux__) || defined (__APPLE__)
34 ===================================================
36 ===================================================
42 static PFN_SYN_PRINTF_VA g_pPrintf = NULL;
44 void Set_Syn_Printf(PFN_SYN_PRINTF_VA pf)
49 #define BUFFER_SIZE 4096
51 void Syn_Printf (const char *text, ...)
53 char buf[BUFFER_SIZE];
61 va_start (args, text);
62 (*g_pPrintf)(text, args);
67 va_start (args, text);
68 vsnprintf (buf, BUFFER_SIZE, text, args);
69 buf[BUFFER_SIZE-1] = 0;
78 =======================================================================
80 =======================================================================
83 // this must be kept in sync with EAPIType
84 static const char* APITypeName[4] =
92 CSynapseServer::CSynapseServer()
100 CSynapseServer::~CSynapseServer()
106 Syn_Printf("TODO: free API managers\n");
109 void CSynapseServer::AddSearchPath(char* path)
111 char *pLocalPath = new char[strlen(path)+1];
112 strcpy(pLocalPath, path);
113 mSearchPaths.push_front(pLocalPath);
116 bool CSynapseServer::Initialize(const char* conf_file, PFN_SYN_PRINTF_VA pf)
118 // browse the paths to locate all potential modules
124 // if a config file is specified and we fail to load it, we fail
125 Syn_Printf("loading synapse XML config file '%s'\n", conf_file);
126 mpDoc = xmlParseFile(conf_file);
129 Syn_Printf("'%s' invalid/not found\n", conf_file);
134 for (list<char *>::iterator iPath=mSearchPaths.begin(); iPath!=mSearchPaths.end(); iPath++)
136 const char* path = *iPath;
138 Syn_Printf("Synapse Scanning modules path: %s\n", path);
140 GDir* dir = g_dir_open (path, 0, NULL);
146 const gchar* name = g_dir_read_name(dir);
150 // too small to be isolated in win32/ and linux/ directories..
152 const char* ext_so = ".dll";
153 #elif defined (__linux__) || defined (__APPLE__)
154 const char* ext_so = ".so";
156 const char* ext = strrchr (name, '.');
157 if ((ext == NULL) || (stricmp (ext, ext_so) != 0))
161 newModule.Format("%s%s", path, name);
162 Syn_Printf("Found '%s'\n", newModule.GetBuffer());
163 EnumerateInterfaces(newModule);
173 #define FORMAT_BUFSIZE 2048
174 const char* CSynapseServer::FormatGetLastError()
176 static char buf[FORMAT_BUFSIZE];
178 FORMAT_MESSAGE_FROM_SYSTEM |
179 FORMAT_MESSAGE_IGNORE_INSERTS,
182 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
190 void CSynapseServer::EnumerateInterfaces(Str &soname)
192 CSynapseClientSlot slot;
193 slot.mpDLL = LoadLibrary(soname.GetBuffer());
196 Syn_Printf("LoadLibrary '%s' failed\n", soname.GetBuffer());
197 Syn_Printf(" GetLastError: %s", FormatGetLastError());
200 slot.mpEnumerate = (PFN_SYNAPSE_ENUMERATEINTERFACES)GetProcAddress(slot.mpDLL, NAME_SYNAPSE_ENUMERATEINTERFACES);
201 if (!slot.mpEnumerate)
203 Syn_Printf("GetProcAddress('%s') failed\n", NAME_SYNAPSE_ENUMERATEINTERFACES);
204 Syn_Printf(" GetLastError: %s", FormatGetLastError());
207 Syn_Printf("Enumerate interfaces on '%s'\n", soname.GetBuffer());
208 slot.mpClient = slot.mpEnumerate(SYNAPSE_VERSION, this);
211 Syn_Printf("Enumerate interfaces on '%s' returned NULL, unloading.\n", soname.GetBuffer());
212 if (!FreeLibrary(slot.mpDLL))
214 Syn_Printf(" FreeLibrary failed: GetLastError: '%s'\n", CSynapseServer::FormatGetLastError());
218 slot.mFileName = soname;
219 mClients.push_front(slot);
222 void CSynapseClientSlot::ReleaseSO()
226 Syn_Printf("ERROR: no shared object handle for client '%s' in CSynapseClientSlot::ReleaseSO\n", mpClient->GetInfo());
229 Syn_Printf("FreeLibrary '%s'\n", mpClient->GetInfo());
230 if (!FreeLibrary(mpDLL))
232 Syn_Printf(" FreeLibrary failed: GetLastError: '%s'\n", CSynapseServer::FormatGetLastError());
237 #elif defined(__linux__) || defined (__APPLE__)
238 void CSynapseServer::EnumerateInterfaces(Str &soname)
240 CSynapseClientSlot slot;
241 slot.mpDLL = dlopen (soname.GetBuffer(), RTLD_NOW);
242 PFN_SYNAPSE_ENUMERATEINTERFACES *pEnumerate;
246 if ((error = (char *)dlerror()) == NULL)
248 Syn_Printf("dlopen '%s' failed\n dlerror: '%s'\n", soname.GetBuffer(), error);
251 slot.mpEnumerate = (PFN_SYNAPSE_ENUMERATEINTERFACES)dlsym(slot.mpDLL, NAME_SYNAPSE_ENUMERATEINTERFACES);
252 if (!slot.mpEnumerate)
255 if ((error = (char *)dlerror()) == NULL)
257 Syn_Printf("dlsym '%s' failed on shared object '%s'\n dlerror: '%s'\n", NAME_SYNAPSE_ENUMERATEINTERFACES, soname.GetBuffer(), error);
260 Syn_Printf("Enumerate interfaces on '%s'\n", soname.GetBuffer());
261 slot.mpClient = slot.mpEnumerate(SYNAPSE_VERSION, this);
264 Syn_Printf("Enumerate interfaces on '%s' returned NULL, unloading.\n", soname.GetBuffer());
265 if (dlclose(slot.mpDLL))
268 if ((error = (char *)dlerror()) == NULL)
270 Syn_Printf(" dlclose failed: dlerror: '%s'\n", error);
274 slot.mFileName = soname;
275 mClients.push_front(slot);
278 void CSynapseClientSlot::ReleaseSO()
282 Syn_Printf("ERROR: no shared object handle for client '%s' in CSynapseClientSlot::ReleaseSO\n", mpClient->GetInfo());
285 Syn_Printf("dlclose '%s'\n", mpClient->GetInfo());
289 if ((error = (char *)dlerror()) == NULL)
291 Syn_Printf(" dlclose failed: dlerror: '%s'\n", error);
298 void CSynapseServer::EnumerateBuiltinModule(CSynapseBuiltinClient *pClient)
300 CSynapseClientSlot slot;
301 pClient->EnumerateInterfaces(this);
302 slot.mpClient = pClient;
303 slot.mType = SYN_BUILTIN;
304 mClients.push_front(slot);
307 PFN_SYN_PRINTF_VA CSynapseServer::Get_Syn_Printf()
312 void CSynapseServer::TryPushStack(APIDescriptor_t *pAPI)
314 list<APIDescriptor_t*>::iterator iAPI;
315 for(iAPI=mStack.begin(); iAPI!=mStack.end(); iAPI++)
322 mStack.push_front(pAPI);
323 mbStackChanged = true;
326 list<CSynapseClientSlot>::iterator CSynapseServer::ShutdownClient(list<CSynapseClientSlot>::iterator iSlot)
328 CSynapseClientSlot *pClientSlot = &(*iSlot);
329 if (pClientSlot->mpClient->IsActive())
331 // this should not happen except during core shutdown (i.e. editor is shutting down)
332 Syn_Printf("WARNING: ShutdownClient attempted on an active module '%s'\n", pClientSlot->mpClient->GetInfo());
336 api_count = pClientSlot->mpClient->GetAPICount();
337 for(i=0; i<api_count; i++)
339 APIDescriptor_t *pAPI = pClientSlot->mpClient->GetAPIDescriptor(i);
340 // search this API in mStack
341 list< APIDescriptor_t *>::iterator iStack = mStack.begin();
342 while(iStack != mStack.end())
348 if (iStack != mStack.end())
350 if (pAPI->mType == SYN_REQUIRE)
352 if (pAPI->mbTableInitDone)
354 // even if non active, some SYN_REQUIRE may have been filled up
355 // look for the corresponding SYN_PROVIDE and decref
356 list< APIDescriptor_t *>::iterator iStackRequire = mStack.begin();
357 APIDescriptor_t *pMatchAPI;
358 while (iStackRequire != mStack.end())
360 pMatchAPI = *iStackRequire;
361 if ( pMatchAPI->mType == SYN_PROVIDE && MatchAPI( pMatchAPI, pAPI ) )
365 if (iStackRequire != mStack.end())
367 // we have found the corresponding SYN_PROVIDE
368 pMatchAPI->mRefCount--;
372 // this is not supposed to happen at all
373 Syn_Printf("ERROR: couldn't find the SYN_PROVIDE for an initialized SYN_REQUIRE API '%s' '%s' '%s'\n", pAPI->major_name, pAPI->minor_name, pClientSlot->mpClient->GetInfo());
377 else if (pAPI->mType == SYN_PROVIDE)
379 // this should never happen on non active clients, it may happen during a core shutdown though
380 // if the mRefCount is != 0, that means there is at least a function table out there that will segfault things
381 Syn_Printf("WARNING: found a SYN_PROVIDE API '%s' '%s' with refcount %d in CSynapseServer::ShutdownClient for '%s'\n", pAPI->major_name, pAPI->minor_name, pAPI->mRefCount, pClientSlot->mpClient->GetInfo());
383 // mostly safe to remove it now
384 mStack.erase(iStack);
387 // we can actually release the module now
388 // NOTE: do we want to have a 'final shutdown' call to the client? (not as long as we don't have a use for it)
389 if (pClientSlot->mType == SYN_SO)
391 pClientSlot->ReleaseSO();
393 return mClients.erase(iSlot);
396 void CSynapseServer::PushRequired(CSynapseClient *pClient)
398 /* walk through the standard APIs and push them in */
399 int i,max = pClient->GetAPICount();
402 APIDescriptor_t* pAPI = pClient->GetAPIDescriptor(i);
403 if (pAPI->mType == SYN_REQUIRE && !pAPI->mbTableInitDone)
409 /* if this client has 'List' API Manager types, walk through them for addition too */
410 max = pClient->GetManagerListCount();
413 CSynapseAPIManager *pManager = pClient->GetManagerList(i);
414 assert(pManager->GetType() == API_LIST);
415 pManager->InitializeAPIList();
417 for(j=0; j<pManager->GetAPICount(); j++)
419 TryPushStack(pManager->GetAPI(j));
423 /* if there are loose match managers, prompt them against the current list of SYN_PROVIDE interfaces
424 * and let them decide which ones they might want
427 max = pClient->GetManagerMatchCount();
431 CSynapseAPIManager *pManager = pClient->GetManagerMatch(i);
432 // start matching all known SYN_PROVIDE APIs against this manager
433 list<CSynapseClientSlot>::iterator iClientSlot;
434 for(iClientSlot=mClients.begin(); iClientSlot!=mClients.end(); iClientSlot++)
436 CSynapseClient *pScanClient = (*iClientSlot).
438 int j,jmax = pScanClient->GetAPICount();
439 for(j=0; j<jmax; j++)
441 APIDescriptor_t *pAPI = pScanClient->GetAPIDescriptor(j);
442 if (pAPI->mType == SYN_PROVIDE)
444 if (pManager->MatchAPI(pAPI->major_name, pAPI->minor_name))
446 /*! we are going to want to load this one
447 * NOTE TTimo: what if this can not be resolved in the end?
448 * if this happens, then the whole startup will fail instead
449 * or we can use SYN_REQUIRE_ANY and drop it without consequences
451 APIDescriptor_t *pPushAPI = pManager->BuildRequireAPI(pAPI);
452 TryPushStack(pPushAPI);
460 bool CSynapseServer::MatchAPI(APIDescriptor_t *p1, APIDescriptor_t *p2)
462 return MatchAPI( p1->major_name, p1->minor_name, p2->major_name, p2->minor_name );
465 bool CSynapseServer::MatchAPI(const char* major1, const char* minor1, const char* major2, const char* minor2)
467 if ( strcmp( major1, major2 ) ) {
470 // either no minor at all for this API, or matching
471 if ( ( minor1 && minor2 ) && !strcmp( minor1, minor2 ) ) {
474 // or one of the minors says "*" (knowing that the majors match already)
475 if ( ( minor1 && !strcmp( minor1, "*" ) ) || ( minor2 && !strcmp( minor2, "*" ) ) ) {
481 bool CSynapseServer::ResolveAPI(APIDescriptor_t* pAPI)
483 //Syn_Printf("In ResolveAPI %s %p '%s' '%s'\n", APITypeName[pAPI->mType], pAPI, pAPI->major_name, pAPI->minor_name);
484 // loop through active clients, search for a client providing what we are looking for
485 list<CSynapseClientSlot>::iterator iClient;
486 for(iClient=mClients.begin(); iClient!=mClients.end(); iClient++)
488 // walk through interfaces on this client for a match
489 CSynapseClient *pScanClient = (*iClient).mpClient;
490 int i,max = pScanClient->GetAPICount();
493 APIDescriptor_t *pScanClientAPI = pScanClient->GetAPIDescriptor(i);
494 if (pScanClientAPI->mType == SYN_PROVIDE)
496 if (MatchAPI(pAPI, pScanClientAPI))
498 // can this client provide APIs yet
499 // it is possible that all of it's APIs have been filled and it's not been activated yet
500 pScanClient->CheckSetActive();
501 if (pScanClient->IsActive())
503 // make sure this interface has correct size (this is our version check)
504 if (pAPI->mSize != pScanClientAPI->mSize)
506 Syn_Printf("ERROR: version mismatch for API '%s' '%s' found in '%s' (size %d != %d)\n", pAPI->major_name, pAPI->minor_name, pScanClient->GetInfo(), pAPI->mSize, pScanClientAPI->mSize);
507 Syn_Printf(" the module and the server are incompatible\n");
508 // keep going to other APIs
511 // this is an active client, we can request
512 #ifdef SYNAPSE_VERBOSE
513 Syn_Printf("RequestAPI '%s' '%s' from '%s' for API %p\n", pAPI->major_name, pAPI->minor_name, pScanClient->GetInfo(), pAPI);
515 if (!pScanClient->RequestAPI(pAPI))
517 // this should never happen, means we think this module provides the API, but it answers that it doesn't
518 Syn_Printf("ERROR: RequestAPI failed\n");
521 pScanClientAPI->mRefCount++;
522 pAPI->mbTableInitDone = true;
523 return true; // job done
527 // this client is not active yet, some of it's required interfaces are not filled in
528 PushRequired(pScanClient);
529 // we will exit the scan through the APIDescriptor of this client and look at other clients
539 bool CSynapseServer::DoResolve(CSynapseClient *pClient)
541 list<CSynapseClientSlot>::iterator iSlot;
542 for(iSlot=mClients.begin(); iSlot != mClients.end(); iSlot++)
544 if ((*iSlot).mpClient == pClient)
547 if (iSlot == mClients.end())
549 Syn_Printf("CSynapserServer::Resolve adding new client slot '%s'\n", pClient->GetInfo());
550 CSynapseClientSlot slot;
551 slot.mpClient = pClient;
552 slot.mFileName = "local client";
553 // make it active so we can request the interfaces already
554 pClient->ForceSetActive();
555 mClients.push_front(slot);
559 // make it active so we can request the interfaces already
560 (*iSlot).mpClient->ForceSetActive();
563 // push the interfaces that need to be resolved for this client
564 // NOTE: this doesn't take care of the SYN_REQUIRE_ANY interfaces
565 PushRequired(pClient);
566 // start resolving now
567 // working till the stack is emptied or till we reach a dead end situation
568 // we do a depth first traversal, we will grow the interface stack to be resolved till we start finding solutions
569 list<APIDescriptor_t*>::iterator iCurrent;
570 mbStackChanged = true; // init to true so we try the first elem
571 while (!mStack.empty())
576 // the stack didn't change last loop
578 if (iCurrent==mStack.end())
580 Syn_Printf("ERROR: CSynapseServer::Resolve, failed to resolve\n");
584 if (ResolveAPI(*iCurrent))
586 iCurrent = mStack.erase(iCurrent);
587 mbStackChanged = true;
592 // the stack changed at last loop
593 mbStackChanged = false;
594 iCurrent = mStack.begin();
595 if (ResolveAPI(*iCurrent))
597 iCurrent = mStack.erase(iCurrent);
598 mbStackChanged = true;
605 bool CSynapseServer::Resolve(CSynapseClient *pClient)
607 bool ret = DoResolve(pClient);
608 list<CSynapseClientSlot>::iterator iClient;
609 iClient = mClients.begin();
610 while(iClient != mClients.end())
612 CSynapseClient *pClient = (*iClient).mpClient;
613 if (!pClient->IsActive())
615 Syn_Printf("Unloading an unused module: '%s'\n", pClient->GetInfo());
616 iClient = ShutdownClient(iClient);
624 void CSynapseServer::Shutdown()
626 Syn_Printf("Synapse server core is shutting down\n");
627 // do a first pass to shutdown the clients nicely (i.e. decref, release memory and drop everything)
628 // we seperate the client shutdown calls from the dlclose cause that part is a clean decref / free situation whereas dlclose will break links without advice
629 list<CSynapseClientSlot>::iterator iClient;
630 iClient = mClients.begin();
631 for(iClient = mClients.begin(); iClient != mClients.end(); iClient++)
633 (*iClient).mpClient->Shutdown();
635 // now release them from the server's point of view
636 iClient = mClients.begin();
637 while(iClient != mClients.end())
639 iClient = ShutdownClient(iClient);
643 void CSynapseServer::DumpStack()
645 list<APIDescriptor_t*>::iterator iCurrent;
646 for(iCurrent=mStack.begin(); iCurrent != mStack.end(); iCurrent++)
648 APIDescriptor_t*pAPI = *iCurrent;
649 Syn_Printf("interface %s %p '%s' '%s'\n", APITypeName[pAPI->mType], pAPI, pAPI->major_name, pAPI->minor_name);
653 void CSynapseServer::DumpActiveClients()
655 list<CSynapseClientSlot>::iterator iClient;
656 for(iClient=mClients.begin(); iClient!=mClients.end(); iClient++)
658 CSynapseClient *pClient = (*iClient).mpClient;
659 Syn_Printf("%s", pClient->GetInfo());
660 if (pClient->IsActive())
663 Syn_Printf(" (not active)\n");
667 bool CSynapseServer::SelectClientConfig(const char *client_name)
671 xmlNodePtr pNode = xmlDocGetRootElement(mpDoc);
674 // look for the client
675 pNode=pNode->children;
678 if (pNode->type == XML_ELEMENT_NODE)
680 xmlChar *prop = xmlGetProp(pNode, (const xmlChar *)"name");
681 if (!strcmp((const char *)prop, client_name))
691 return false; // config you asked for isn't there
693 mpFocusedNode = pNode->children;
694 mpCurrentClientConfig = pNode;
698 bool CSynapseServer::GetNextConfig(char **api_name, char **minor)
700 while(mpFocusedNode && mpFocusedNode->name)
702 if (mpFocusedNode->type == XML_ELEMENT_NODE && !strcmp((const char *)mpFocusedNode->name, "api"))
706 m_api_name = xmlGetProp(mpFocusedNode, (const xmlChar *)"name");
707 *api_name = (char *)m_api_name;
710 m_content = g_strdup((const gchar *)mpFocusedNode->children->content);
711 g_strstrip(m_content);
713 mpFocusedNode = mpFocusedNode->next;
716 mpFocusedNode = mpFocusedNode->next;
721 bool CSynapseServer::GetConfigForAPI( const char *api, char **minor ) {
722 xmlNodePtr pNode = mpCurrentClientConfig->children;
723 while ( pNode && pNode->name ) {
724 if ( pNode->type == XML_ELEMENT_NODE && !strcmp( (const char *)pNode->name, "api" ) ) {
726 xmlFree( m_api_name );
728 m_api_name = xmlGetProp( pNode, (const xmlChar *)"name" );
729 if ( !strcmp( (const char *)m_api_name, api ) ) {
733 m_content = g_strdup( (const gchar *)pNode->children->content );
734 g_strstrip( m_content );
744 const char *CSynapseServer::GetModuleFilename(CSynapseClient *pClient)
746 list<CSynapseClientSlot>::iterator iSlot;
747 for(iSlot=mClients.begin(); iSlot != mClients.end(); iSlot++)
749 if ((*iSlot).mpClient == pClient)
751 if ((*iSlot).mType == SYN_BUILTIN)
757 return (*iSlot).mFileName;
765 =======================================================================
767 =======================================================================
770 CSynapseClient::CSynapseClient()
774 void CSynapseClient::Shutdown()
776 vector<APIDescriptor_t *>::iterator iAPI;
777 for(iAPI=mAPIDescriptors.begin(); iAPI!=mAPIDescriptors.end(); iAPI++)
779 APIDescriptor_t *pAPI = *iAPI;
780 if (pAPI->mRefCount != 0)
781 Syn_Printf("WARNING: ~CSynapseClient '%s' has non-zero ref count for interface '%s' '%s'\n", GetInfo(), pAPI->major_name, pAPI->minor_name);
786 mAPIDescriptors.clear();
787 vector<CSynapseAPIManager *>::iterator iManager;
788 for(iManager=mManagersList.begin(); iManager!=mManagersList.end(); iManager++)
790 CSynapseAPIManager *pManager = *iManager;
794 mManagersList.clear();
795 for(iManager=mManagersMatch.begin(); iManager!=mManagersMatch.end(); iManager++)
797 CSynapseAPIManager *pManager = *iManager;
801 mManagersMatch.clear();
804 CSynapseClient::~CSynapseClient()
806 // this should not be doing anything when called from here if everything went right
807 // otherwise it's likely to crash .. at least that's the sign we missed something
811 int CSynapseClient::GetAPICount()
813 return mAPIDescriptors.size();
816 APIDescriptor_t* CSynapseClient::GetAPIDescriptor(int i)
818 return mAPIDescriptors[i];
821 int CSynapseClient::GetManagerMatchCount()
823 return mManagersMatch.size();
826 CSynapseAPIManager* CSynapseClient::GetManagerMatch(int i)
828 return mManagersMatch[i];
831 int CSynapseClient::GetManagerListCount()
833 return mManagersList.size();
836 CSynapseAPIManager* CSynapseClient::GetManagerList(int i)
838 return mManagersList[i];
841 bool CSynapseClient::AddAPI(const char *major, const char *minor, int size, EAPIType type, void *pTable)
843 // do some safe checks before actual addition
844 if (type == SYN_REQUIRE && !pTable)
846 Syn_Printf("ERROR: interface '%s' '%s' from '%s' is SYN_REQUIRE and doesn't provide a function table pointer\n", major, minor, GetInfo());
851 int *pi = (int *)pTable;
854 Syn_Printf("ERROR: forgot to init function table size for interface '%s' '%s' from '%s'?\n", major, minor, GetInfo());
858 APIDescriptor_t *pAPI = new APIDescriptor_t;
859 memset(pAPI, 0, sizeof(APIDescriptor_t));
860 strncpy(pAPI->major_name, major, MAX_APINAME);
862 strncpy(pAPI->minor_name, minor, MAX_APINAME);
864 pAPI->mpTable = pTable;
865 // store the interface size
866 if (type == SYN_PROVIDE)
870 Syn_Printf("ERROR: size of the interface required for a SYN_PROVIDE ('%s' '%s' from '%s')\n", major, minor, GetInfo());
876 else if (type == SYN_REQUIRE)
880 // if a non-zero value is given in function call, use this instead of the val in table
881 *((int *)pAPI->mpTable) = size;
886 pAPI->mSize = *((int *)pAPI->mpTable);
887 if (pAPI->mSize == 0)
889 Syn_Printf("ERROR: didn't get an interface size ('%s' '%s' from '%s')\n", major, minor, GetInfo());
897 Syn_Printf("ERROR: AddAPI type '%d' not supported\n", type);
900 mAPIDescriptors.push_back(pAPI);
901 #ifdef SYNAPSE_VERBOSE
902 Syn_Printf("AddAPI: %s %p '%s' '%s' from '%s', size %d\n", APITypeName[pAPI->mType], pAPI, major, minor, GetInfo(), pAPI->mSize);
909 const char* CSynapseClient::GetInfo()
911 return "CSynapseClient built " __DATE__ " " RADIANT_VERSION;
914 bool CSynapseClient::CheckSetActive()
918 int i,max=GetAPICount();
921 APIDescriptor_t *pAPI = GetAPIDescriptor(i);
922 if (pAPI->mType == SYN_REQUIRE && !pAPI->mbTableInitDone)
925 // if we have managers with fixed list, those need to be completely filled in too
926 vector<CSynapseAPIManager *>::iterator iManager;
927 for(iManager=mManagersList.begin(); iManager!=mManagersList.end(); iManager++)
929 if (!(*iManager)->CheckSetActive())
930 return false; // one of the managers doesn't have all it needs yet
932 // call OnActivate to let the client perform last minute checks
933 // NOTE: this should be fatal instead of letting the engine try other combinations
937 // yes, all required interfaces have been initialized
938 Syn_Printf("'%s' activated\n", GetInfo());
943 bool CSynapseClient::ConfigXML( CSynapseServer *pServer, const char *client_name, const XMLConfigEntry_t entries[] ) {
945 if ( !client_name ) {
946 client_name = GetName();
949 Syn_Printf("Dynamic APIs for client '%s'\n", GetInfo());
950 if ( !pServer->SelectClientConfig( client_name ) )
952 Syn_Printf( "Failed to select synapse client config '%s'\n", client_name );
957 while ( entries[i].type != SYN_UNKNOWN ) { // don't test pTable, for a SYN_PROVIDE it will be empty
959 if ( !pServer->GetConfigForAPI( entries[i].api, &minor ) ) {
960 Syn_Printf( "GetConfigForAPI '%s' failed - invalid XML config file?\n", entries[i].api );
963 AddAPI( entries[i].api, minor, entries[i].size, entries[i].type, entries[i].pTable );
966 Syn_Printf( "%d dynamic interfaces parsed for '%s'\n", i, client_name );
970 void CSynapseClient::AddManager(CSynapseAPIManager *pManager)
973 if (pManager->GetType() == API_LIST)
974 mManagersList.push_back(pManager);
976 mManagersMatch.push_back(pManager);
979 CSynapseAPIManager::~CSynapseAPIManager()
981 vector<APIDescriptor_t *>::iterator iAPI;
982 for(iAPI=mAPIs.begin(); iAPI!=mAPIs.end(); iAPI++)
984 APIDescriptor_t *pAPI = *iAPI;
985 if (pAPI->mRefCount != 0)
987 Syn_Printf("WARNING: ~CSynapseAPIManager has non-zero ref count for interface '%s' '%s'\n", pAPI->major_name, pAPI->minor_name);
994 APIDescriptor_t* CSynapseAPIManager::PrepareRequireAPI(APIDescriptor_t *pAPI)
997 if (pAPI->mType != SYN_PROVIDE)
999 Syn_Printf("ERROR: unexpected pAPI->mType != SYN_PROVIDE in CSynapseAPIManager::PrepareRequireAPI\n");
1003 APIDescriptor_t *pRequireAPI = new APIDescriptor_t;
1004 memcpy(pRequireAPI, pAPI, sizeof(APIDescriptor_t));
1005 pRequireAPI->mType = SYN_REQUIRE_ANY;
1006 pRequireAPI->mpTable = NULL;
1007 pRequireAPI->mbTableInitDone = false;
1008 pRequireAPI->mSize = 0; // this will have to be set correctly by the child for version checking
1009 pRequireAPI->mRefCount = 0;
1013 void CSynapseAPIManager::SetMatchAPI(const char *major, const char *minor)
1015 if (strlen(minor)>MAX_PATTERN_STRING)
1017 Syn_Printf("ERROR: MAX_TOKEN_STRING exceeded in CSynapseAPIManager::SetMatchAPI: '%s'\n", minor);
1020 strcpy(major_pattern, major);
1021 strcpy(minor_pattern, minor);
1022 if (strcmp(minor, "*"))
1028 bool CSynapseAPIManager::MatchAPI(const char *major, const char *minor)
1030 assert(mType == API_MATCH);
1033 if this interface has been allocated already, avoid requesting it again..
1035 vector<APIDescriptor_t *>::iterator iAPI;
1036 for(iAPI=mAPIs.begin(); iAPI!=mAPIs.end(); iAPI++)
1038 if (CSynapseServer::MatchAPI((*iAPI)->major_name, (*iAPI)->minor_name, major, minor))
1042 if (!strcmp(major, major_pattern))
1047 bool CSynapseAPIManager::CheckSetActive()
1049 if (mType == API_MATCH)
1051 // mType == API_LIST
1052 int i,max = GetAPICount();
1053 for(i=0; i<max; i++)
1055 if (!GetAPI(i)->mbTableInitDone)
1061 void CSynapseAPIManager::InitializeAPIList()
1063 char minor_tok[MAX_PATTERN_STRING];
1068 Syn_Printf("WARNING: CSynapseAPIManager::InitializeAPIList on an already initialized APIManager\n");
1072 strncpy(minor_tok, minor_pattern, MAX_PATTERN_STRING);
1073 token = strtok(minor_tok, " ");
1076 /* ask the child to build from scratch */
1077 APIDescriptor_t *pAPI = new APIDescriptor_t;
1078 memset(pAPI, 0, sizeof(APIDescriptor_t));
1079 strncpy(pAPI->major_name, major_pattern, MAX_APINAME);
1080 strncpy(pAPI->minor_name, token, MAX_APINAME);
1081 pAPI->mType = SYN_REQUIRE_ANY;
1083 mAPIs.push_back(pAPI);
1084 token = strtok(NULL, " ");
1088 int CSynapseAPIManager::GetAPICount()
1090 return mAPIs.size();
1093 APIDescriptor_t* CSynapseAPIManager::GetAPI(int i)
1098 // http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=879
1100 printf( "fini_stub\n" );