View Javadoc

1   /*
2   Copyright 2011-2014 AGH-UST, http://www.agh.edu.pl
3   Faculty of Computer Science, Electronics and Telecommunications
4   Department of Computer Science 
5   
6   See the NOTICE file distributed with this work for additional
7   information regarding copyright ownership
8   
9   Licensed under the Apache License, Version 2.0 (the "License");
10  you may not use this file except in compliance with the License.
11  You may obtain a copy of the License at
12  
13    http://www.apache.org/licenses/LICENSE-2.0
14  
15  Unless required by applicable law or agreed to in writing, software
16  distributed under the License is distributed on an "AS IS" BASIS,
17  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  See the License for the specific language governing permissions and
19  limitations under the License.
20  */
21  package org.universAAL.ri.gateway.eimanager.impl.importing;
22  
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  
29  import org.universAAL.middleware.context.ContextEvent;
30  import org.universAAL.middleware.context.ContextSubscriber;
31  import org.universAAL.middleware.service.ServiceCall;
32  import org.universAAL.middleware.service.ServiceResponse;
33  import org.universAAL.middleware.service.owls.profile.ServiceProfile;
34  import org.universAAL.middleware.ui.UIHandlerProfile;
35  import org.universAAL.middleware.ui.UIRequest;
36  import org.universAAL.middleware.ui.UIResponse;
37  import org.universAAL.ri.gateway.communicator.Activator;
38  import org.universAAL.ri.gateway.communicator.service.GatewayCommunicator;
39  import org.universAAL.ri.gateway.communicator.service.Message;
40  import org.universAAL.ri.gateway.communicator.service.impl.Serializer;
41  import org.universAAL.ri.gateway.eimanager.impl.AbstractProxyManager;
42  import org.universAAL.ri.gateway.eimanager.impl.ProxyBusMember;
43  import org.universAAL.ri.gateway.eimanager.impl.exporting.ProxyRegistration;
44  
45  public class ImportedProxyManager extends AbstractProxyManager {
46  
47  	private Map<String, List<ProxyBusMember>> generatedProxies;
48  	private Map<String, List<ServiceProfile>> remoteBusMembersImportedProfiles;
49  	private GatewayCommunicator communicator;
50  
51  	public ImportedProxyManager(final GatewayCommunicator communicator) {
52  		super();
53  		this.generatedProxies = new HashMap<String, List<ProxyBusMember>>();
54  		this.remoteBusMembersImportedProfiles = new HashMap<String, List<ServiceProfile>>();
55  		this.communicator = communicator;
56  	}
57  
58  	public void registerProxies(final InternalImportOperation op) {
59  		ProxyBusMember proxy = null;
60  		if (!generatedProxies.containsKey(op.getRemoteRegisteredProxyId())) {
61  			generatedProxies.put(op.getRemoteRegisteredProxyId(),
62  					new ArrayList<ProxyBusMember>());
63  		}
64  		switch (op.getType()) {
65  		case ServiceCaller:
66  			Map<String, List<ServiceProfile>> profilesMap = op
67  					.getRealizedServices();
68  			for (String key : profilesMap.keySet()) {
69  				for (ServiceProfile p : profilesMap.get(key)) {
70  					if (remoteBusMembersImportedProfiles.containsKey(key)) {
71  						if (remoteBusMembersImportedProfiles.get(key).contains(
72  								p)) {
73  							continue;
74  						}
75  					}else{
76  						remoteBusMembersImportedProfiles.put(key, new ArrayList<ServiceProfile>());
77  					}
78  
79  					proxy = new ProxyServiceCallee(new ServiceProfile[] { p },
80  							this, op.getRemoteRegisteredProxyId(), key, Activator.mc);
81  
82  					generatedProxies.get(op.getRemoteRegisteredProxyId()).add(
83  							proxy);
84  					remoteBusMembersImportedProfiles.get(key).add(p);
85  				}
86  			}
87  
88  			break;
89  		case ContextSubscriber:
90  			proxy = new ProxyContextPublisher(
91  					(ContextSubscriber) op.getBusMember(),
92  					op.getContextProvider(), this,
93  					op.getRemoteRegisteredProxyId(), Activator.mc);
94  			generatedProxies.get(op.getRemoteRegisteredProxyId()).add(proxy);
95  			break;
96  		case UICaller:
97  			UIHandlerProfile[] profiles = op.getUiHandlerProfiles();
98  			proxy = new ProxyUIHandler(profiles, this,
99  					op.getRemoteRegisteredProxyId(), Activator.mc);
100 			generatedProxies.get(op.getRemoteRegisteredProxyId()).add(proxy);
101 			break;
102 		}
103 
104 	}
105 
106 	public void unregisterProxies(final InternalImportOperation op) {
107 		String id = op.getRemoteRegisteredProxyId();
108 		if (generatedProxies.containsKey(id)) {
109 			for (ProxyBusMember pr : generatedProxies.get(id)) {
110 				pr.removeProxy();
111 				if (pr instanceof ProxyServiceCallee){
112 					if (remoteBusMembersImportedProfiles.containsKey(pr.getRemoteBusMemberId())){
113 						for(ServiceProfile p : ((ProxyServiceCallee)pr).getProfilesSet()){
114 							remoteBusMembersImportedProfiles.get(pr.getRemoteBusMemberId()).remove(p);
115 						}
116 					}
117 				}
118 			}
119 			generatedProxies.remove(id);
120 		}
121 	}
122 
123 	public void realizeLocalContextEventPublishment(final String sourceId,
124 			final ContextEvent event) {
125 		List<ProxyBusMember> members = generatedProxies.get(sourceId);
126 		for(ProxyBusMember member : members){
127 			((ProxyContextPublisher) member).publishContextEvent(event);
128 		}
129 	}
130 
131 	public ServiceResponse realizeRemoteServiceRequest(final String targetId,
132 			final ServiceCall request, final String remoteBusMemberId) throws IOException,
133 			ClassNotFoundException {
134 		Message toSend = Serializer.Instance.marshallObject(request);
135 		toSend.setRemoteProxyRegistrationId(targetId);
136 		toSend.setRemoteMemberId(remoteBusMemberId);
137 		
138 		Message[] returnMessage = communicator.sendServiceRequest(toSend);
139 		// TODO reimplement mechanisms for choosing proper response from
140 		// multiple spaces
141 
142 		ServiceResponse response = Serializer.Instance.unmarshallObject(
143 				ServiceResponse.class, returnMessage[0]);
144 		System.out.println("Received response: " + response);
145 		return response;
146 	}
147 
148 	public void realizeLocalUIResponsePublishment(final String sourceId,
149 			final UIResponse response) {
150 		// TODO implement
151 		// ((ProxyUIHandler)generatedProxies.get(sourceId)).
152 	}
153 
154 	public void realizeRemoteUIRequest(final String targetId,
155 			final UIRequest uiRequest) throws IOException {
156 		Message toSend = Serializer.Instance.marshallObject(uiRequest);
157 		toSend.setRemoteProxyRegistrationId(targetId);
158 		communicator.sendUIRequest(toSend);
159 	}
160 
161 	public void refreshProxy(final ProxyRegistration proxyRegistration) throws IOException, ClassNotFoundException {
162 
163 		ProxyBusMember proxy = null;
164 		List<ProxyBusMember> members = generatedProxies.get(proxyRegistration
165 				.getId());
166 		if (members != null) {
167 			for (ProxyBusMember m : members) {
168 				m.removeProxy();
169 			}
170 		}
171 		
172 		generatedProxies.remove(proxyRegistration.getId());
173 
174 		Map<String, List<String>> serializedProfilesMap = (Map<String, List<String>>) proxyRegistration
175 				.getReturnedValues();
176 
177 		Map<String, List<ServiceProfile>> profilesMap = new HashMap<String, List<ServiceProfile>>();
178 
179 		for (String key : serializedProfilesMap.keySet()) {
180 			if (profilesMap.get(key) == null) {
181 				profilesMap.put(key, new ArrayList<ServiceProfile>());
182 			}
183 			for (String serializedP : serializedProfilesMap.get(key)) {
184 				profilesMap.get(key).add(
185 						Serializer.Instance.unmarshallObject(
186 								ServiceProfile.class, serializedP,
187 								Activator.class.getClassLoader()));
188 			}
189 		}
190 
191 		for (String key : profilesMap.keySet()) {
192 			for (ServiceProfile p : profilesMap.get(key)) {
193 				if (remoteBusMembersImportedProfiles.containsKey(key)) {
194 					if (remoteBusMembersImportedProfiles.get(key).contains(p)) {
195 						continue;
196 					}
197 				}
198 
199 				proxy = new ProxyServiceCallee(new ServiceProfile[] { p },
200 						this, proxyRegistration.getId(),key, Activator.mc);
201 				if (generatedProxies.get(proxyRegistration.getId()) == null){
202 					generatedProxies.put(proxyRegistration.getId(), new ArrayList<ProxyBusMember>());
203 				}
204 				generatedProxies.get(proxyRegistration.getId())
205 						.add(proxy);
206 			}
207 		}
208 
209 	}
210 }