blob: 8816e5d5b7680575bd4c435186cb4bc0ef67eb37 [file] [log] [blame]
Andrew Brown6e002952016-09-12 20:36:45 -07001/*
2 * jndn-utils
3 * Copyright (c) 2016, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU Lesser General Public License,
7 * version 3, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT ANY
10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
12 * more details.
13 */
14
15package com.intel.jndn.utils;
16
17import com.intel.jndn.utils.On;
18import com.intel.jndn.utils.Publisher;
19import com.intel.jndn.utils.Subscriber;
20import com.intel.jndn.utils.pubsub.PubSubFactory;
21import net.named_data.jndn.Face;
22import net.named_data.jndn.Name;
23import net.named_data.jndn.util.Blob;
24
25import java.io.IOException;
26
27/**
28 * A publish-subscrib topic; see the {@code pubsub} package for implementation details
29 *
30 * @author Andrew Brown, andrew.brown@intel.com
31 */
32public final class Topic {
33 private final Name name;
34
35 /**
36 * @param name the NDN name of the topic
37 */
38 public Topic(Name name) {
39 this.name = name;
40 }
41
42 /**
43 * @return the NDN name of the topic; the return is wrapped so that the original value is immutable and the return
44 * type can be modified
45 */
46 public Name name() {
47 return new Name(name);
48 }
49
50 /**
51 * @param face the face to use for network IO; must be driven externally (e.g. {@link Face#processEvents()})
52 * @param onMessage callback fired when a message is received
53 * @param onError callback fired when an error happens after subscription
54 * @return an open subscriber
55 * @throws IOException if the subscription fails
56 */
57 public Subscriber subscribe(Face face, On<Blob> onMessage, On<Exception> onError) throws IOException {
58 return PubSubFactory.newSubscriber(face, name, onMessage, onError);
59 }
60
61 /**
62 * @param face the face to use for network IO; must be driven externally (e.g. {@link Face#processEvents()})
63 * @return a factory-built publisher
64 */
65 public Publisher newPublisher(Face face) {
66 return PubSubFactory.newPublisher(face, name);
67 }
68}