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