Lock SP instance for incoming calls.
[shibboleth/cpp-sp.git] / shibsp / remoting / impl / ListenerService.cpp
1 /*
2  *  Copyright 2001-2007 Internet2
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * ListenerService.cpp
19  * 
20  * Interprocess remoting engine.
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "ServiceProvider.h"
26 #include "remoting/ListenerService.h"
27
28 #include <log4cpp/Category.hh>
29 #include <xercesc/dom/DOM.hpp>
30
31 using namespace shibsp;
32 using namespace xmltooling;
33 using namespace log4cpp;
34 using namespace xercesc;
35 using namespace std;
36
37 namespace shibsp {
38     //SHIBSP_DLLLOCAL PluginManager<ListenerService,const DOMElement*>::Factory MemoryListenerServiceFactory;
39     SHIBSP_DLLLOCAL PluginManager<ListenerService,const DOMElement*>::Factory TCPListenerServiceFactory;
40 #ifndef WIN32
41     SHIBSP_DLLLOCAL PluginManager<ListenerService,const DOMElement*>::Factory UnixListenerServiceFactory;
42 #endif
43 };
44
45 void SHIBSP_API shibsp::registerListenerServices()
46 {
47     SPConfig& conf=SPConfig::getConfig();
48     //conf.ListenerServiceManager.registerFactory(MEMORY_LISTENER_SERVICE, MemoryListenerServiceFactory);
49     conf.ListenerServiceManager.registerFactory(TCP_LISTENER_SERVICE, TCPListenerServiceFactory);
50 #ifndef WIN32
51     conf.ListenerServiceManager.registerFactory(UNIX_LISTENER_SERVICE, UnixListenerServiceFactory);
52 #endif
53 }
54
55 Remoted* ListenerService::regListener(const char* address, Remoted* listener)
56 {
57     Remoted* ret=NULL;
58     map<string,Remoted*>::const_iterator i=m_listenerMap.find(address);
59     if (i!=m_listenerMap.end())
60         ret=i->second;
61     m_listenerMap[address]=listener;
62     Category::getInstance(SHIBSP_LOGCAT".Listener").info("registered remoted message endpoint (%s)",address);
63     return ret;
64 }
65
66 bool ListenerService::unregListener(const char* address, Remoted* current, Remoted* restore)
67 {
68     map<string,Remoted*>::const_iterator i=m_listenerMap.find(address);
69     if (i!=m_listenerMap.end() && i->second==current) {
70         if (restore)
71             m_listenerMap[address]=restore;
72         else
73             m_listenerMap.erase(address);
74         Category::getInstance(SHIBSP_LOGCAT".Listener").info("unregistered remoted message endpoint (%s)",address);
75         return true;
76     }
77     return false;
78 }
79
80 Remoted* ListenerService::lookup(const char *address) const
81 {
82     map<string,Remoted*>::const_iterator i=m_listenerMap.find(address);
83     return (i==m_listenerMap.end()) ? NULL : i->second;
84 }
85
86 void ListenerService::receive(DDF &in, ostream& out)
87 {
88     if (!in.name())
89         throw ListenerException("Incoming message with no destination address rejected.");
90     else if (!strcmp("ping",in.name())) {
91         DDF outmsg=DDF(NULL).integer(in.integer() + 1);
92         DDFJanitor jan(outmsg);
93         out << outmsg;
94     }
95
96     Remoted* dest=lookup(in.name());
97     if (!dest)
98         throw ListenerException("No destination registered for incoming message addressed to ($1).",params(1,in.name()));
99     
100     Locker locker(SPConfig::getConfig().getServiceProvider());\r
101     dest->receive(in, out);
102 }