Imported Upstream version 2.2.1+dfsg
[shibboleth/sp.git] / shibsp / remoting / impl / ListenerService.cpp
1 /*
2  *  Copyright 2001-2009 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 <xercesc/dom/DOM.hpp>
29
30 using namespace shibsp;
31 using namespace xmltooling;
32 using namespace xercesc;
33 using namespace std;
34
35 namespace shibsp {
36     SHIBSP_DLLLOCAL PluginManager<ListenerService,string,const DOMElement*>::Factory TCPListenerServiceFactory;
37 #ifndef WIN32
38     SHIBSP_DLLLOCAL PluginManager<ListenerService,string,const DOMElement*>::Factory UnixListenerServiceFactory;
39 #endif
40 };
41
42 void SHIBSP_API shibsp::registerListenerServices()
43 {
44     SPConfig& conf=SPConfig::getConfig();
45     conf.ListenerServiceManager.registerFactory(TCP_LISTENER_SERVICE, TCPListenerServiceFactory);
46 #ifndef WIN32
47     conf.ListenerServiceManager.registerFactory(UNIX_LISTENER_SERVICE, UnixListenerServiceFactory);
48 #endif
49 }
50
51 Remoted* ListenerService::regListener(const char* address, Remoted* listener)
52 {
53     Remoted* ret=NULL;
54     map<string,Remoted*>::const_iterator i=m_listenerMap.find(address);
55     if (i!=m_listenerMap.end())
56         ret=i->second;
57     m_listenerMap[address]=listener;
58     Category::getInstance(SHIBSP_LOGCAT".Listener").info("registered remoted message endpoint (%s)",address);
59     return ret;
60 }
61
62 bool ListenerService::unregListener(const char* address, Remoted* current, Remoted* restore)
63 {
64     map<string,Remoted*>::const_iterator i=m_listenerMap.find(address);
65     if (i!=m_listenerMap.end() && i->second==current) {
66         if (restore)
67             m_listenerMap[address]=restore;
68         else
69             m_listenerMap.erase(address);
70         Category::getInstance(SHIBSP_LOGCAT".Listener").info("unregistered remoted message endpoint (%s)",address);
71         return true;
72     }
73     return false;
74 }
75
76 Remoted* ListenerService::lookup(const char *address) const
77 {
78     map<string,Remoted*>::const_iterator i=m_listenerMap.find(address);
79     return (i==m_listenerMap.end()) ? NULL : i->second;
80 }
81
82 void ListenerService::receive(DDF &in, ostream& out)
83 {
84     if (!in.name())
85         throw ListenerException("Incoming message with no destination address rejected.");
86     else if (!strcmp("ping",in.name())) {
87         DDF outmsg=DDF(NULL).integer(in.integer() + 1);
88         DDFJanitor jan(outmsg);
89         out << outmsg;
90     }
91
92     Locker locker(SPConfig::getConfig().getServiceProvider());
93     Remoted* dest=lookup(in.name());
94     if (!dest)
95         throw ListenerException("No destination registered for incoming message addressed to ($1).",params(1,in.name()));
96
97     dest->receive(in, out);
98 }