1 package base.jdbs.network.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 import java.io.IOException;
60 import java.util.ArrayList;
61 import java.util.Enumeration;
62 import java.util.List;
63
64 import net.jxta.discovery.DiscoveryListener;
65 import net.jxta.discovery.DiscoveryService;
66 import net.jxta.document.AdvertisementFactory;
67 import net.jxta.id.IDFactory;
68 import net.jxta.peergroup.PeerGroup;
69 import net.jxta.pipe.PipeID;
70 import net.jxta.protocol.PipeAdvertisement;
71
72 /***
73 *
74 * @version $Id: PipeUtil.java,v 1.1 2006/05/22 18:29:41 junior-skunk Exp $
75 *
76 * @author james todd [gonzo at jxta dot org]
77 */
78
79 public class PipeUtil {
80
81
82
83 public static final byte[] GROUP_CHAT_PIPE_ID = {
84 (byte)0xD1, (byte)0xD1, (byte)0xD1, (byte)0xD1,
85 (byte)0xD1, (byte)0xD1, (byte)0xD1, (byte)0xD1,
86 (byte)0xD1, (byte)0xD1, (byte)0xD1, (byte)0xD1,
87 (byte)0xD1, (byte)0xD1, (byte)0xD1, (byte)0xD1
88 };
89
90 public static PipeAdvertisement getAdv(PeerGroup pg, String name,
91 String type, byte[] pipeId) {
92 return getAdv(pg, name, type, pipeId, false);
93 }
94
95 public static PipeAdvertisement getAdv(PeerGroup pg, String name,
96 String type, byte[] pipeId, boolean remote) {
97 PipeAdvertisement pa = searchLocal(pg, name);
98
99 if (pa == null) {
100 pa = createAdv(pg, name, type, pipeId);
101
102 publish(pg, pa, remote);
103 }
104
105 return pa;
106 }
107
108 public static List getAdvs(PeerGroup pg, String name) {
109 List p = new ArrayList();
110
111 try {
112 for (Enumeration pas = pg.getDiscoveryService().
113 getLocalAdvertisements(DiscoveryService.ADV,
114 PipeAdvertisement.NameTag, name);
115 pas.hasMoreElements(); ) {
116 Object o = pas.nextElement();
117
118 if (o instanceof PipeAdvertisement) {
119 p.add((PipeAdvertisement)o);
120 }
121 }
122 } catch (IOException ioe) {}
123
124 return p;
125 }
126
127 public static PipeAdvertisement searchLocal(PeerGroup pg, String name) {
128 PipeAdvertisement pa = null;
129
130 try {
131 for (Enumeration pas = pg.getDiscoveryService().
132 getLocalAdvertisements(DiscoveryService.ADV,
133 PipeAdvertisement.NameTag, name);
134 pas.hasMoreElements(); ) {
135 pa = (PipeAdvertisement)pas.nextElement();
136
137 if (pa.getName().equals(name)) {
138 break;
139 } else {
140 pa = null;
141 }
142 }
143 } catch (IOException ioe) {}
144
145 return pa;
146 }
147
148 public static void discoverAdvs(PeerGroup pg, String name,
149 DiscoveryListener listener) {
150 DiscoveryService s = pg.getDiscoveryService();
151
152 s.getRemoteAdvertisements(null, DiscoveryService.ADV,
153 name != null ? PipeAdvertisement.NameTag : null,
154 name, 10, listener);
155 }
156
157 public static PipeAdvertisement createAdv(PeerGroup pg, String name,
158 String type) {
159 return createAdv(pg, name, type, null);
160 }
161
162 public static PipeAdvertisement createAdv(PeerGroup pg, String name,
163 String type, byte[] id) {
164 PipeAdvertisement pa = (PipeAdvertisement)AdvertisementFactory.
165 newAdvertisement(PipeAdvertisement.getAdvertisementType());
166
167 pa.setPipeID(id != null ?
168 (PipeID)IDFactory.newPipeID(pg.getPeerGroupID(), id) :
169 (PipeID)IDFactory.newPipeID(pg.getPeerGroupID()));
170 pa.setName(name);
171 pa.setType(type);
172
173 return pa;
174 }
175
176 public static void publish(PeerGroup pg, PipeAdvertisement pa) {
177 publish(pg, pa, false);
178 }
179
180 public static void publish(PeerGroup pg, PipeAdvertisement pa,
181 boolean remote) {
182 DiscoveryService ds = pg.getDiscoveryService();
183
184 try {
185 ds.publish(pa);
186 } catch (IOException ioe) {}
187
188 if (remote) {
189
190
191 }
192 }
193 }