📄 genericpeergroup.java
字号:
if (!(target instanceof PeerGroup)) { return false; } PeerGroup targetAsPeerGroup = (PeerGroup) target; // both null or both non-null. if ((null == parentGroup) && (null != targetAsPeerGroup.getParentGroup())) { return false; } if ((null != parentGroup) && (null == targetAsPeerGroup.getParentGroup())) { return false; } if ((null != parentGroup) && !parentGroup.equals(targetAsPeerGroup.getParentGroup())) { return false; } // and same peer ids. return getPeerGroupID().equals(targetAsPeerGroup.getPeerGroupID()); } /** * {@inheritDoc} */ @Override public int hashCode() { // before init we must fail. if ((null == peerAdvertisement) || (null == getPeerGroupID())) { throw new IllegalStateException("PeerGroup not sufficiently initialized"); } // XXX 20050907 bondolo including parentGroup would improve the hash. return getPeerGroupID().hashCode(); } /** * {@inheritDoc} * <p/> * An implementation suitable for debugging. <b>Don't try to parse * this string!</b> All of the information is available from other sources. */ @Override public String toString() { if (null == getPeerGroupID()) { return super.toString(); } StringBuilder result = new StringBuilder(); result.append(getPeerGroupID().toString()); String peerGroupName = peerGroupAdvertisement.getName(); if (null != peerGroupName) { result.append(" \""); result.append(peerGroupName); result.append('\"'); } result.append('['); result.append(masterRefCount); result.append(']'); if (null != parentGroup) { result.append(" / "); result.append(parentGroup.toString()); } return result.toString(); } /** * {@inheritDoc} */ public ThreadGroup getHomeThreadGroup() { return threadGroup; } /** * Discover advertisements. * * @param discovery The discovery service to use. * @param type the Discovery advertisement type. * @param attr The attribute to search for or {@code null}. * @param value The attribute value to match or {@code null}. * @param seconds The number of seconds to search. * @param thisClass The Advertisement class which the advertisement must * match. * @return a Collection of advertisements */ private Collection<Advertisement> discoverSome(DiscoveryService discovery, int type, String attr, String value, int seconds, Class thisClass) { long discoverUntil = TimeUtils.toAbsoluteTimeMillis(seconds * TimeUtils.ASECOND); long lastRemoteAt = 0; // no previous remote discovery made. List<Advertisement> results = new ArrayList<Advertisement>(); try { do { Enumeration<Advertisement> res = discovery.getLocalAdvertisements(type, attr, value); while (res.hasMoreElements()) { Advertisement a = res.nextElement(); if (thisClass.isInstance(a)) { results.add(a); } } if (!results.isEmpty()) { break; } if (TimeUtils.toRelativeTimeMillis(TimeUtils.timeNow(), lastRemoteAt) > (30 * TimeUtils.ASECOND)) { discovery.getRemoteAdvertisements(null, type, attr, value, 20); lastRemoteAt = TimeUtils.timeNow(); } // snooze waiting for responses to come in. Thread.sleep(1000); } while (TimeUtils.timeNow() < discoverUntil); } catch (Exception whatever) { if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) { LOG.log(Level.WARNING, "Failure during discovery", whatever); } } return results; } /** * Discover an advertisement within the local peer group. * * @param type the Discovery advertisement type. * @param attr The attribute to search for or {@code null}. * @param value The attribute value to match or {@code null}. * @param seconds The number of seconds to search. * @param thisClass The Advertisement class which the advertisement must match. * @return a Collection of advertisements */ private Advertisement discoverOne(int type, String attr, String value, int seconds, Class thisClass) { Iterator<Advertisement> res = discoverSome(discovery, type, attr, value, seconds, thisClass).iterator(); if (!res.hasNext()) { return null; } return res.next(); } /** * Shortcuts to the standard basic services. * * @param mcid The Module Class ID of the service. * @param service The service instance to set as the shortcut or * {@code null} to clear the shortcut. */ private void setShortCut(ModuleClassID mcid, Service service) { if (endpointClassID.equals(mcid)) { endpoint = (EndpointService) service; return; } if (resolverClassID.equals(mcid)) { resolver = (ResolverService) service; return; } if (discoveryClassID.equals(mcid)) { discovery = (DiscoveryService) service; return; } if (pipeClassID.equals(mcid)) { pipe = (PipeService) service; return; } if (membershipClassID.equals(mcid)) { membership = (MembershipService) service; return; } if (peerinfoClassID.equals(mcid)) { peerinfo = (PeerInfoService) service; return; } if (rendezvousClassID.equals(mcid)) { rendezvous = (RendezVousService) service; return; } if (accessClassID.equals(mcid)) { access = (AccessService) service; } } /** * Add a service to the collection of known services. * * @param mcid The Module Class ID of the service. * @param service The service instance to set as the shortcut or */ protected synchronized void addService(ModuleClassID mcid, Service service) { if (stopping) { return; } if (services.containsKey(mcid)) { throw new IllegalStateException("Service" + mcid + " already registered."); } services.put(mcid, service); setShortCut(mcid, service); } /** * {@inheritDoc} */ synchronized public Service lookupService(ID mcid) throws ServiceNotFoundException { Service p = services.get(mcid); if (p == null) { throw new ServiceNotFoundException("Not found: " + mcid.toString()); } return p.getInterface(); } /** * {@inheritDoc} * <p/> * Group implementations do not have to support mapping. * it would be nice to separate better Interfaces, so that * Interface Objects can do things that the real service does * not have to implement. */ public Service lookupService(ID mcid, int roleIndex) throws ServiceNotFoundException { // If the role number is != 0, it can't be honored: we // do not have an explicit map. if (roleIndex != 0) { throw new ServiceNotFoundException("Not found: " + mcid + "[" + roleIndex + "]"); } return lookupService(mcid); } /** * {@inheritDoc} */ public Iterator getRoleMap(ID name) { // No translation; use the given name in a singleton. return Collections.singletonList(name).iterator(); } /** * check that all required core services are registered * * @throws ServiceNotFoundException If a required service was not found. */ protected void checkServices() throws ServiceNotFoundException { Service ignored; ignored = lookupService(endpointClassID); ignored = lookupService(resolverClassID); ignored = lookupService(membershipClassID); ignored = lookupService(accessClassID); /** * ignored = lookupService(discoveryClassID); * ignored = lookupService(pipeClassID); * ignored = lookupService(rendezvousClassID); * ignored = lookupService(peerinfoClassID); */ } /** * Ask a group to unregister and unload a service * * @param mcid The service to be removed. * @throws ServiceNotFoundException if service is not found */ protected synchronized void removeService(ModuleClassID mcid) throws ServiceNotFoundException { setShortCut(mcid, null); Service p = services.remove(mcid); if (p == null) { throw new ServiceNotFoundException("Not found: " + mcid.toString()); } p.stopApp(); // service.terminate(); FIXME. We probably need a terminate() // method. // FIXME: [jice@jxta.org 20011013] to make sure the service is // no-longer referenced, we should always return interfaces, and // have a way to cut the reference to the real service in the // interfaces. One way of doing that would be to have to levels // of indirection: we should keep one and return references to it. // when we want to cut the service loose, we should clear the // reference from the interface that we own before letting it go. // We need to study the consequences of doing that before implementing // it. } /** * {@inheritDoc} */ public Module loadModule(ID assigned, Advertisement impl) throws ProtocolNotSupportedException, PeerGroupException { return loadModule(assigned, (ModuleImplAdvertisement) impl, false); } /** * Load a Module from a ModuleImplAdv. * <p/> * Compatibility is checked and load is attempted. If compatible and * loaded successfully, the resulting Module is initialized and returned. * In most cases the other loadModule() method should be preferred, since * unlike this one, it will seek many compatible implementation * advertisements and try them all until one works. The home group of the new * module (its' parent group if the new Module is a group) will be this group. * * @param assigned Id to be assigned to that module (usually its ClassID). * @param implAdv An implementation advertisement for that module. * @param privileged If {@code true} then the module is provided the true * group obj instead of just an interface to the group object. This is * normally used only for the group's defined services and applications. * @return Module the module loaded and initialized. * @throws ProtocolNotSupportedException The module is incompatible. * @throws PeerGroupException The module could not be loaded or initialized */ protected Module loadModule(ID assigned, ModuleImplAdvertisement implAdv, boolean privileged) throws ProtocolNotSupportedException, PeerGroupException { Element compat = implAdv.getCompat(); if (null == compat) { throw new IllegalArgumentException("No compatibility statement for : " + assigned); } if (!compatible(compat)) { if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) { LOG.warning("Incompatible Module : " + assigned); } throw new ProtocolNotSupportedException("Incompatible Module : " + assigned); } Module newMod = null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -