blob: dcd540845bd6f44da24ed12cee2097c432ead233 [file] [log] [blame]
Francesco Furfariec7e1752006-10-02 13:37:04 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
Francesco Furfari91af5da2006-04-04 23:44:23 +00009 *
Francesco Furfariec7e1752006-10-02 13:37:04 +000010 * http://www.apache.org/licenses/LICENSE-2.0
Francesco Furfari91af5da2006-04-04 23:44:23 +000011 *
Francesco Furfariec7e1752006-10-02 13:37:04 +000012 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
Francesco Furfari91af5da2006-04-04 23:44:23 +000018 */
19
20package org.apache.felix.upnp.tester;
21
22import java.util.*;
23
24import org.osgi.framework.*;
25import org.osgi.service.upnp.*;
26
Francesco Furfarif2a67912006-07-17 17:08:02 +000027/*
Karl Paulsd312acc2007-06-18 20:38:33 +000028* @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
Francesco Furfarif2a67912006-07-17 17:08:02 +000029*/
Francesco Furfari91af5da2006-04-04 23:44:23 +000030public class UPnPSubscriber {
31 private BundleContext context;
32 private UPnPEventListener listener;
33 private HashMap hash;
34
35 private class Subscription implements UPnPEventListener{
36 ServiceRegistration registration;
37
38 Subscription(String keys){
39 try {
40 Filter filter = context.createFilter(keys);
41 Properties props = new Properties();
42 props.put(UPnPEventListener.UPNP_FILTER, filter);
43 registration = context.registerService(UPnPEventListener.class.getName(), this, props);
44 }catch (Exception ex){
45 System.out.println(ex);
46 }
47 }
48
49 public void unsubscribe(){
50 registration.unregister();
51 }
52
53 public void notifyUPnPEvent(String arg0, String arg1, Dictionary arg2) {
54 listener.notifyUPnPEvent( arg0, arg1, arg2);
55 }
56 }
57
58
59 public UPnPSubscriber(BundleContext context,UPnPEventListener listener){
60 this.context = context;
61 this.listener = listener;
62 hash = new HashMap();
63 }
64
65 public void subscribe(String deviceId, String serviceId){
66 String keys = "(&(" + UPnPDevice.ID + "="+ deviceId + ")(" + UPnPService.ID + "=" + serviceId + "))";
67 if (hash.get(keys) == null){
68 hash.put(keys, new Subscription(keys));
69 }
70 }
71
72 public void unsubscribe(String deviceId, String serviceId){
73 String keys = "(&(" + UPnPDevice.ID + "="+ deviceId + ")(" + UPnPService.ID + "=" + serviceId + "))";
74 if (hash.containsKey(keys)) {
75 Subscription subscription = (Subscription) hash.get(keys);
76 subscription.unsubscribe();
77 hash.remove(keys);
78 }
79 }
80
81
82
83
84}