VS10 solution files, convert from NULL macro to nullptr.
[shibboleth/sp.git] / shibsp / remoting / impl / ListenerService.cpp
1 /*
2  *  Copyright 2001-2010 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::Remoted()
52 {
53 }
54
55 Remoted::~Remoted()
56 {
57 }
58
59 ListenerService::ListenerService()
60 {
61 }
62
63 ListenerService::~ListenerService()
64 {
65 }
66
67 Remoted* ListenerService::regListener(const char* address, Remoted* listener)
68 {
69     Remoted* ret=nullptr;
70     map<string,Remoted*>::const_iterator i=m_listenerMap.find(address);
71     if (i!=m_listenerMap.end())
72         ret=i->second;
73     m_listenerMap[address]=listener;
74     Category::getInstance(SHIBSP_LOGCAT".Listener").info("registered remoted message endpoint (%s)",address);
75     return ret;
76 }
77
78 bool ListenerService::unregListener(const char* address, Remoted* current, Remoted* restore)
79 {
80     map<string,Remoted*>::const_iterator i=m_listenerMap.find(address);
81     if (i!=m_listenerMap.end() && i->second==current) {
82         if (restore)
83             m_listenerMap[address]=restore;
84         else
85             m_listenerMap.erase(address);
86         Category::getInstance(SHIBSP_LOGCAT".Listener").info("unregistered remoted message endpoint (%s)",address);
87         return true;
88     }
89     return false;
90 }
91
92 Remoted* ListenerService::lookup(const char *address) const
93 {
94     map<string,Remoted*>::const_iterator i=m_listenerMap.find(address);
95     return (i==m_listenerMap.end()) ? nullptr : i->second;
96 }
97
98 void ListenerService::receive(DDF &in, ostream& out)
99 {
100     if (!in.name())
101         throw ListenerException("Incoming message with no destination address rejected.");
102     else if (!strcmp("ping",in.name())) {
103         DDF outmsg=DDF(nullptr).integer(in.integer() + 1);
104         DDFJanitor jan(outmsg);
105         out << outmsg;
106     }
107
108     Locker locker(SPConfig::getConfig().getServiceProvider());
109     Remoted* dest=lookup(in.name());
110     if (!dest)
111         throw ListenerException("No destination registered for incoming message addressed to ($1).",params(1,in.name()));
112
113     dest->receive(in, out);
114 }
115
116 bool ListenerService::init(bool force)
117 {
118     return true;
119 }
120
121 void ListenerService::term()
122 {
123 }