Francesco Furfari | ec7e175 | 2006-10-02 13:37:04 +0000 | [diff] [blame] | 1 | /*
|
| 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 Furfari | 91af5da | 2006-04-04 23:44:23 +0000 | [diff] [blame] | 9 | *
|
Francesco Furfari | ec7e175 | 2006-10-02 13:37:04 +0000 | [diff] [blame] | 10 | * http://www.apache.org/licenses/LICENSE-2.0
|
Francesco Furfari | 91af5da | 2006-04-04 23:44:23 +0000 | [diff] [blame] | 11 | *
|
Francesco Furfari | ec7e175 | 2006-10-02 13:37:04 +0000 | [diff] [blame] | 12 | * 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 Furfari | 91af5da | 2006-04-04 23:44:23 +0000 | [diff] [blame] | 18 | */
|
| 19 |
|
| 20 | package org.apache.felix.upnp.tester;
|
| 21 |
|
| 22 | import java.util.*;
|
| 23 |
|
| 24 | import org.osgi.framework.*;
|
| 25 | import org.osgi.service.upnp.*;
|
| 26 |
|
Francesco Furfari | f2a6791 | 2006-07-17 17:08:02 +0000 | [diff] [blame] | 27 | /*
|
Karl Pauls | d312acc | 2007-06-18 20:38:33 +0000 | [diff] [blame^] | 28 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
|
Francesco Furfari | f2a6791 | 2006-07-17 17:08:02 +0000 | [diff] [blame] | 29 | */
|
Francesco Furfari | 91af5da | 2006-04-04 23:44:23 +0000 | [diff] [blame] | 30 | public 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 | }
|