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);
245 if ((error = (char *)dlerror()) == NULL)
247 Syn_Printf("dlopen '%s' failed\n dlerror: '%s'\n", soname.GetBuffer(), error);
250 slot.mpEnumerate = (PFN_SYNAPSE_ENUMERATEINTERFACES)dlsym(slot.mpDLL, NAME_SYNAPSE_ENUMERATEINTERFACES);
251 if (!slot.mpEnumerate)
254 if ((error = (char *)dlerror()) == NULL)
256 Syn_Printf("dlsym '%s' failed on shared object '%s'\n dlerror: '%s'\n", NAME_SYNAPSE_ENUMERATEINTERFACES, soname.GetBuffer(), error);
259 Syn_Printf("Enumerate interfaces on '%s'\n", soname.GetBuffer());
260 slot.mpClient = slot.mpEnumerate(SYNAPSE_VERSION, this);
263 Syn_Printf("Enumerate interfaces on '%s' returned NULL, unloading.\n", soname.GetBuffer());
264 if (dlclose(slot.mpDLL))
267 if ((error = (char *)dlerror()) == NULL)
269 Syn_Printf(" dlclose failed: dlerror: '%s'\n", error);
273 slot.mFileName = soname;
274 mClients.push_front(slot);
277 void CSynapseClientSlot::ReleaseSO()
281 Syn_Printf("ERROR: no shared object handle for client '%s' in CSynapseClientSlot::ReleaseSO\n", mpClient->GetInfo());
284 Syn_Printf("dlclose '%s'\n", mpClient->GetInfo());
288 if ((error = (char *)dlerror()) == NULL)
290 Syn_Printf(" dlclose failed: dlerror: '%s'\n", error);
297 void CSynapseServer::EnumerateBuiltinModule(CSynapseBuiltinClient *pClient)
299 CSynapseClientSlot slot;
300 pClient->EnumerateInterfaces(this);
301 slot.mpClient = pClient;
302 slot.mType = SYN_BUILTIN;
303 mClients.push_front(slot);
306 PFN_SYN_PRINTF_VA CSynapseServer::Get_Syn_Printf()
311 void CSynapseServer::TryPushStack(APIDescriptor_t *pAPI)
313 list<APIDescriptor_t*>::iterator iAPI;
314 for(iAPI=mStack.begin(); iAPI!=mStack.end(); iAPI++)
321 mStack.push_front(pAPI);
322 mbStackChanged = true;
325 list<CSynapseClientSlot>::iterator CSynapseServer::ShutdownClient(list<CSynapseClientSlot>::iterator iSlot)
327 CSynapseClientSlot *pClientSlot = &(*iSlot);
328 if (pClientSlot->mpClient->IsActive())
330 // this should not happen except during core shutdown (i.e. editor is shutting down)
331 Syn_Printf("WARNING: ShutdownClient attempted on an active module '%s'\n", pClientSlot->mpClient->GetInfo());
335 api_count = pClientSlot->mpClient->GetAPICount();
336 for(i=0; i<api_count; i++)
338 APIDescriptor_t *pAPI = pClientSlot->mpClient->GetAPIDescriptor(i);
339 // search this API in mStack
340 list< APIDescriptor_t *>::iterator iStack = mStack.begin();
341 while(iStack != mStack.end())
347 if (iStack != mStack.end())
349 if (pAPI->mType == SYN_REQUIRE)
351 if (pAPI->mbTableInitDone)
353 // even if non active, some SYN_REQUIRE may have been filled up
354 // look for the corresponding SYN_PROVIDE and decref
355 list< APIDescriptor_t *>::iterator iStackRequire = mStack.begin();
356 APIDescriptor_t *pMatchAPI;
357 while (iStackRequire != mStack.end())
359 pMatchAPI = *iStackRequire;
360 if ( pMatchAPI->mType == SYN_PROVIDE && MatchAPI( pMatchAPI, pAPI ) )
364 if (iStackRequire != mStack.end())
366 // we have found the corresponding SYN_PROVIDE
367 pMatchAPI->mRefCount--;
371 // this is not supposed to happen at all
372 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());
376 else if (pAPI->mType == SYN_PROVIDE)
378 // this should never happen on non active clients, it may happen during a core shutdown though
379 // if the mRefCount is != 0, that means there is at least a function table out there that will segfault things
380 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());
382 // mostly safe to remove it now
383 mStack.erase(iStack);
386 // we can actually release the module now
387 // 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)
388 if (pClientSlot->mType == SYN_SO)
390 pClientSlot->ReleaseSO();
392 return mClients.erase(iSlot);
395 void CSynapseServer::PushRequired(CSynapseClient *pClient)
397 /* walk through the standard APIs and push them in */
398 int i,max = pClient->GetAPICount();
401 APIDescriptor_t* pAPI = pClient->GetAPIDescriptor(i);
402 if (pAPI->mType == SYN_REQUIRE && !pAPI->mbTableInitDone)
408 /* if this client has 'List' API Manager types, walk through them for addition too */
409 max = pClient->GetManagerListCount();
412 CSynapseAPIManager *pManager = pClient->GetManagerList(i);
413 assert(pManager->GetType() == API_LIST);
414 pManager->InitializeAPIList();
416 for(j=0; j<pManager->GetAPICount(); j++)
418 TryPushStack(pManager->GetAPI(j));
422 /* if there are loose match managers, prompt them against the current list of SYN_PROVIDE interfaces
423 * and let them decide which ones they might want
426 max = pClient->GetManagerMatchCount();
430 CSynapseAPIManager *pManager = pClient->GetManagerMatch(i);
431 // start matching all known SYN_PROVIDE APIs against this manager
432 list<CSynapseClientSlot>::iterator iClientSlot;
433 for(iClientSlot=mClients.begin(); iClientSlot!=mClients.end(); iClientSlot++)
435 CSynapseClient *pScanClient = (*iClientSlot).
437 int j,jmax = pScanClient->GetAPICount();
438 for(j=0; j<jmax; j++)
440 APIDescriptor_t *pAPI = pScanClient->GetAPIDescriptor(j);
441 if (pAPI->mType == SYN_PROVIDE)
443 if (pManager->MatchAPI(pAPI->major_name, pAPI->minor_name))
445 /*! we are going to want to load this one
446 * NOTE TTimo: what if this can not be resolved in the end?
447 * if this happens, then the whole startup will fail instead
448 * or we can use SYN_REQUIRE_ANY and drop it without consequences
450 APIDescriptor_t *pPushAPI = pManager->BuildRequireAPI(pAPI);
451 TryPushStack(pPushAPI);
459 bool CSynapseServer::MatchAPI(APIDescriptor_t *p1, APIDescriptor_t *p2)
461 return MatchAPI( p1->major_name, p1->minor_name, p2->major_name, p2->minor_name );
464 bool CSynapseServer::MatchAPI(const char* major1, const char* minor1, const char* major2, const char* minor2)
466 if ( strcmp( major1, major2 ) ) {
469 // either no minor at all for this API, or matching
470 if ( ( minor1 && minor2 ) && !strcmp( minor1, minor2 ) ) {
473 // or one of the minors says "*" (knowing that the majors match already)
474 if ( ( minor1 && !strcmp( minor1, "*" ) ) || ( minor2 && !strcmp( minor2, "*" ) ) ) {
480 bool CSynapseServer::ResolveAPI(APIDescriptor_t* pAPI)
482 //Syn_Printf("In ResolveAPI %s %p '%s' '%s'\n", APITypeName[pAPI->mType], pAPI, pAPI->major_name, pAPI->minor_name);
483 // loop through active clients, search for a client providing what we are looking for
484 list<CSynapseClientSlot>::iterator iClient;
485 for(iClient=mClients.begin(); iClient!=mClients.end(); iClient++)
487 // walk through interfaces on this client for a match
488 CSynapseClient *pScanClient = (*iClient).mpClient;
489 int i,max = pScanClient->GetAPICount();
492 APIDescriptor_t *pScanClientAPI = pScanClient->GetAPIDescriptor(i);
493 if (pScanClientAPI->mType == SYN_PROVIDE)
495 if (MatchAPI(pAPI, pScanClientAPI))
497 // can this client provide APIs yet
498 // it is possible that all of it's APIs have been filled and it's not been activated yet
499 pScanClient->CheckSetActive();
500 if (pScanClient->IsActive())
502 // make sure this interface has correct size (this is our version check)
503 if (pAPI->mSize != pScanClientAPI->mSize)
505 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);
506 Syn_Printf(" the module and the server are incompatible\n");
507 // keep going to other APIs
510 // this is an active client, we can request
511 #ifdef SYNAPSE_VERBOSE
512 Syn_Printf("RequestAPI '%s' '%s' from '%s' for API %p\n", pAPI->major_name, pAPI->minor_name, pScanClient->GetInfo(), pAPI);
514 if (!pScanClient->RequestAPI(pAPI))
516 // this should never happen, means we think this module provides the API, but it answers that it doesn't
517 Syn_Printf("ERROR: RequestAPI failed\n");
520 pScanClientAPI->mRefCount++;
521 pAPI->mbTableInitDone = true;
522 return true; // job done
526 // this client is not active yet, some of it's required interfaces are not filled in
527 PushRequired(pScanClient);
528 // we will exit the scan through the APIDescriptor of this client and look at other clients
538 bool CSynapseServer::DoResolve(CSynapseClient *pClient)
540 list<CSynapseClientSlot>::iterator iSlot;
541 for(iSlot=mClients.begin(); iSlot != mClients.end(); iSlot++)
543 if ((*iSlot).mpClient == pClient)
546 if (iSlot == mClients.end())
548 Syn_Printf("CSynapserServer::Resolve adding new client slot '%s'\n", pClient->GetInfo());
549 CSynapseClientSlot slot;
550 slot.mpClient = pClient;
551 slot.mFileName = "local client";
552 // make it active so we can request the interfaces already
553 pClient->ForceSetActive();
554 mClients.push_front(slot);
558 // make it active so we can request the interfaces already
559 (*iSlot).mpClient->ForceSetActive();
562 // push the interfaces that need to be resolved for this client
563 // NOTE: this doesn't take care of the SYN_REQUIRE_ANY interfaces
564 PushRequired(pClient);
565 // start resolving now
566 // working till the stack is emptied or till we reach a dead end situation
567 // we do a depth first traversal, we will grow the interface stack to be resolved till we start finding solutions
568 list<APIDescriptor_t*>::iterator iCurrent;
569 mbStackChanged = true; // init to true so we try the first elem
570 while (!mStack.empty())
575 // the stack didn't change last loop
577 if (iCurrent==mStack.end())
579 Syn_Printf("ERROR: CSynapseServer::Resolve, failed to resolve\n");
583 if (ResolveAPI(*iCurrent))
585 iCurrent = mStack.erase(iCurrent);
586 mbStackChanged = true;
591 // the stack changed at last loop
592 mbStackChanged = false;
593 iCurrent = mStack.begin();
594 if (ResolveAPI(*iCurrent))
596 iCurrent = mStack.erase(iCurrent);
597 mbStackChanged = true;
604 bool CSynapseServer::Resolve(CSynapseClient *pClient)
606 bool ret = DoResolve(pClient);
607 list<CSynapseClientSlot>::iterator iClient;
608 iClient = mClients.begin();
609 while(iClient != mClients.end())
611 CSynapseClient *pClient = (*iClient).mpClient;
612 if (!pClient->IsActive())
614 Syn_Printf("Unloading an unused module: '%s'\n", pClient->GetInfo());
615 iClient = ShutdownClient(iClient);
623 void CSynapseServer::Shutdown()
625 Syn_Printf("Synapse server core is shutting down\n");
626 // do a first pass to shutdown the clients nicely (i.e. decref, release memory and drop everything)
627 // 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
628 list<CSynapseClientSlot>::iterator iClient;
629 iClient = mClients.begin();
630 for(iClient = mClients.begin(); iClient != mClients.end(); iClient++)
632 (*iClient).mpClient->Shutdown();
634 // now release them from the server's point of view
635 iClient = mClients.begin();
636 while(iClient != mClients.end())
638 iClient = ShutdownClient(iClient);
642 void CSynapseServer::DumpStack()
644 list<APIDescriptor_t*>::iterator iCurrent;
645 for(iCurrent=mStack.begin(); iCurrent != mStack.end(); iCurrent++)
647 APIDescriptor_t*pAPI = *iCurrent;
648 Syn_Printf("interface %s %p '%s' '%s'\n", APITypeName[pAPI->mType], pAPI, pAPI->major_name, pAPI->minor_name);
652 void CSynapseServer::DumpActiveClients()
654 list<CSynapseClientSlot>::iterator iClient;
655 for(iClient=mClients.begin(); iClient!=mClients.end(); iClient++)
657 CSynapseClient *pClient = (*iClient).mpClient;
658 Syn_Printf("%s", pClient->GetInfo());
659 if (pClient->IsActive())
662 Syn_Printf(" (not active)\n");
666 bool CSynapseServer::SelectClientConfig(const char *client_name)
670 xmlNodePtr pNode = xmlDocGetRootElement(mpDoc);
673 // look for the client
674 pNode=pNode->children;
677 if (pNode->type == XML_ELEMENT_NODE)
679 xmlChar *prop = xmlGetProp(pNode, (const xmlChar *)"name");
680 if (!strcmp((const char *)prop, client_name))
690 return false; // config you asked for isn't there
692 mpFocusedNode = pNode->children;
693 mpCurrentClientConfig = pNode;
697 bool CSynapseServer::GetNextConfig(char **api_name, char **minor)
699 while(mpFocusedNode && mpFocusedNode->name)
701 if (mpFocusedNode->type == XML_ELEMENT_NODE && !strcmp((const char *)mpFocusedNode->name, "api"))
705 m_api_name = xmlGetProp(mpFocusedNode, (const xmlChar *)"name");
706 *api_name = (char *)m_api_name;
709 m_content = g_strdup((const gchar *)mpFocusedNode->children->content);
710 g_strstrip(m_content);
712 mpFocusedNode = mpFocusedNode->next;
715 mpFocusedNode = mpFocusedNode->next;
720 bool CSynapseServer::GetConfigForAPI( const char *api, char **minor ) {
721 xmlNodePtr pNode = mpCurrentClientConfig->children;
722 while ( pNode && pNode->name ) {
723 if ( pNode->type == XML_ELEMENT_NODE && !strcmp( (const char *)pNode->name, "api" ) ) {
725 xmlFree( m_api_name );
727 m_api_name = xmlGetProp( pNode, (const xmlChar *)"name" );
728 if ( !strcmp( (const char *)m_api_name, api ) ) {
732 m_content = g_strdup( (const gchar *)pNode->children->content );
733 g_strstrip( m_content );
743 const char *CSynapseServer::GetModuleFilename(CSynapseClient *pClient)
745 list<CSynapseClientSlot>::iterator iSlot;
746 for(iSlot=mClients.begin(); iSlot != mClients.end(); iSlot++)
748 if ((*iSlot).mpClient == pClient)
750 if ((*iSlot).mType == SYN_BUILTIN)
756 return (*iSlot).mFileName;
764 =======================================================================
766 =======================================================================
769 CSynapseClient::CSynapseClient()
773 void CSynapseClient::Shutdown()
775 vector<APIDescriptor_t *>::iterator iAPI;
776 for(iAPI=mAPIDescriptors.begin(); iAPI!=mAPIDescriptors.end(); iAPI++)
778 APIDescriptor_t *pAPI = *iAPI;
779 if (pAPI->mRefCount != 0)
780 Syn_Printf("WARNING: ~CSynapseClient '%s' has non-zero ref count for interface '%s' '%s'\n", GetInfo(), pAPI->major_name, pAPI->minor_name);
785 mAPIDescriptors.clear();
786 vector<CSynapseAPIManager *>::iterator iManager;
787 for(iManager=mManagersList.begin(); iManager!=mManagersList.end(); iManager++)
789 CSynapseAPIManager *pManager = *iManager;
793 mManagersList.clear();
794 for(iManager=mManagersMatch.begin(); iManager!=mManagersMatch.end(); iManager++)
796 CSynapseAPIManager *pManager = *iManager;
800 mManagersMatch.clear();
803 CSynapseClient::~CSynapseClient()
805 // this should not be doing anything when called from here if everything went right
806 // otherwise it's likely to crash .. at least that's the sign we missed something
810 int CSynapseClient::GetAPICount()
812 return mAPIDescriptors.size();
815 APIDescriptor_t* CSynapseClient::GetAPIDescriptor(int i)
817 return mAPIDescriptors[i];
820 int CSynapseClient::GetManagerMatchCount()
822 return mManagersMatch.size();
825 CSynapseAPIManager* CSynapseClient::GetManagerMatch(int i)
827 return mManagersMatch[i];
830 int CSynapseClient::GetManagerListCount()
832 return mManagersList.size();
835 CSynapseAPIManager* CSynapseClient::GetManagerList(int i)
837 return mManagersList[i];
840 bool CSynapseClient::AddAPI(const char *major, const char *minor, int size, EAPIType type, void *pTable)
842 // do some safe checks before actual addition
843 if (type == SYN_REQUIRE && !pTable)
845 Syn_Printf("ERROR: interface '%s' '%s' from '%s' is SYN_REQUIRE and doesn't provide a function table pointer\n", major, minor, GetInfo());
850 int *pi = (int *)pTable;
853 Syn_Printf("ERROR: forgot to init function table size for interface '%s' '%s' from '%s'?\n", major, minor, GetInfo());
857 APIDescriptor_t *pAPI = new APIDescriptor_t;
858 memset(pAPI, 0, sizeof(APIDescriptor_t));
859 strncpy(pAPI->major_name, major, MAX_APINAME);
861 strncpy(pAPI->minor_name, minor, MAX_APINAME);
863 pAPI->mpTable = pTable;
864 // store the interface size
865 if (type == SYN_PROVIDE)
869 Syn_Printf("ERROR: size of the interface required for a SYN_PROVIDE ('%s' '%s' from '%s')\n", major, minor, GetInfo());
875 else if (type == SYN_REQUIRE)
879 // if a non-zero value is given in function call, use this instead of the val in table
880 *((int *)pAPI->mpTable) = size;
885 pAPI->mSize = *((int *)pAPI->mpTable);
886 if (pAPI->mSize == 0)
888 Syn_Printf("ERROR: didn't get an interface size ('%s' '%s' from '%s')\n", major, minor, GetInfo());
896 Syn_Printf("ERROR: AddAPI type '%d' not supported\n", type);
899 mAPIDescriptors.push_back(pAPI);
900 #ifdef SYNAPSE_VERBOSE
901 Syn_Printf("AddAPI: %s %p '%s' '%s' from '%s', size %d\n", APITypeName[pAPI->mType], pAPI, major, minor, GetInfo(), pAPI->mSize);
908 const char* CSynapseClient::GetInfo()
910 return "CSynapseClient built " __DATE__ " " RADIANT_VERSION;
913 bool CSynapseClient::CheckSetActive()
917 int i,max=GetAPICount();
920 APIDescriptor_t *pAPI = GetAPIDescriptor(i);
921 if (pAPI->mType == SYN_REQUIRE && !pAPI->mbTableInitDone)
924 // if we have managers with fixed list, those need to be completely filled in too
925 vector<CSynapseAPIManager *>::iterator iManager;
926 for(iManager=mManagersList.begin(); iManager!=mManagersList.end(); iManager++)
928 if (!(*iManager)->CheckSetActive())
929 return false; // one of the managers doesn't have all it needs yet
931 // call OnActivate to let the client perform last minute checks
932 // NOTE: this should be fatal instead of letting the engine try other combinations
936 // yes, all required interfaces have been initialized
937 Syn_Printf("'%s' activated\n", GetInfo());
942 bool CSynapseClient::ConfigXML( CSynapseServer *pServer, const char *client_name, const XMLConfigEntry_t entries[] ) {
944 if ( !client_name ) {
945 client_name = GetName();
948 Syn_Printf("Dynamic APIs for client '%s'\n", GetInfo());
949 if ( !pServer->SelectClientConfig( client_name ) )
951 Syn_Printf( "Failed to select synapse client config '%s'\n", client_name );
956 while ( entries[i].type != SYN_UNKNOWN ) { // don't test pTable, for a SYN_PROVIDE it will be empty
958 if ( !pServer->GetConfigForAPI( entries[i].api, &minor ) ) {
959 Syn_Printf( "GetConfigForAPI '%s' failed - invalid XML config file?\n", entries[i].api );
962 AddAPI( entries[i].api, minor, entries[i].size, entries[i].type, entries[i].pTable );
965 Syn_Printf( "%d dynamic interfaces parsed for '%s'\n", i, client_name );
969 void CSynapseClient::AddManager(CSynapseAPIManager *pManager)
972 if (pManager->GetType() == API_LIST)
973 mManagersList.push_back(pManager);
975 mManagersMatch.push_back(pManager);
978 CSynapseAPIManager::~CSynapseAPIManager()
980 vector<APIDescriptor_t *>::iterator iAPI;
981 for(iAPI=mAPIs.begin(); iAPI!=mAPIs.end(); iAPI++)
983 APIDescriptor_t *pAPI = *iAPI;
984 if (pAPI->mRefCount != 0)
986 Syn_Printf("WARNING: ~CSynapseAPIManager has non-zero ref count for interface '%s' '%s'\n", pAPI->major_name, pAPI->minor_name);
993 APIDescriptor_t* CSynapseAPIManager::PrepareRequireAPI(APIDescriptor_t *pAPI)
996 if (pAPI->mType != SYN_PROVIDE)
998 Syn_Printf("ERROR: unexpected pAPI->mType != SYN_PROVIDE in CSynapseAPIManager::PrepareRequireAPI\n");
1002 APIDescriptor_t *pRequireAPI = new APIDescriptor_t;
1003 memcpy(pRequireAPI, pAPI, sizeof(APIDescriptor_t));
1004 pRequireAPI->mType = SYN_REQUIRE_ANY;
1005 pRequireAPI->mpTable = NULL;
1006 pRequireAPI->mbTableInitDone = false;
1007 pRequireAPI->mSize = 0; // this will have to be set correctly by the child for version checking
1008 pRequireAPI->mRefCount = 0;
1012 void CSynapseAPIManager::SetMatchAPI(const char *major, const char *minor)
1014 if (strlen(minor)>MAX_PATTERN_STRING)
1016 Syn_Printf("ERROR: MAX_TOKEN_STRING exceeded in CSynapseAPIManager::SetMatchAPI: '%s'\n", minor);
1019 strcpy(major_pattern, major);
1020 strcpy(minor_pattern, minor);
1021 if (strcmp(minor, "*"))
1027 bool CSynapseAPIManager::MatchAPI(const char *major, const char *minor)
1029 assert(mType == API_MATCH);
1032 if this interface has been allocated already, avoid requesting it again..
1034 vector<APIDescriptor_t *>::iterator iAPI;
1035 for(iAPI=mAPIs.begin(); iAPI!=mAPIs.end(); iAPI++)
1037 if (CSynapseServer::MatchAPI((*iAPI)->major_name, (*iAPI)->minor_name, major, minor))
1041 if (!strcmp(major, major_pattern))
1046 bool CSynapseAPIManager::CheckSetActive()
1048 if (mType == API_MATCH)
1050 // mType == API_LIST
1051 int i,max = GetAPICount();
1052 for(i=0; i<max; i++)
1054 if (!GetAPI(i)->mbTableInitDone)
1060 void CSynapseAPIManager::InitializeAPIList()
1062 char minor_tok[MAX_PATTERN_STRING];
1067 Syn_Printf("WARNING: CSynapseAPIManager::InitializeAPIList on an already initialized APIManager\n");
1071 strncpy(minor_tok, minor_pattern, MAX_PATTERN_STRING);
1072 token = strtok(minor_tok, " ");
1075 /* ask the child to build from scratch */
1076 APIDescriptor_t *pAPI = new APIDescriptor_t;
1077 memset(pAPI, 0, sizeof(APIDescriptor_t));
1078 strncpy(pAPI->major_name, major_pattern, MAX_APINAME);
1079 strncpy(pAPI->minor_name, token, MAX_APINAME);
1080 pAPI->mType = SYN_REQUIRE_ANY;
1082 mAPIs.push_back(pAPI);
1083 token = strtok(NULL, " ");
1087 int CSynapseAPIManager::GetAPICount()
1089 return mAPIs.size();
1092 APIDescriptor_t* CSynapseAPIManager::GetAPI(int i)
1097 // http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=879
1099 printf( "fini_stub\n" );