📄 myjxta.java
字号:
public void run() { try { sleep(30000); System.out.println("normal shutdown failed - will exit via System.exit(0) call"); System.exit(0); } catch (InterruptedException e) { //nothing } } }; hardTerminateThread.setDaemon(true); hardTerminateThread.start(); } } }.start(); } /** * Changes the status information that is displayed in the ui. * * @param status the new status information to display */ public void setStatus(String status) { if (status != null) { this.view.setStatus(status); Status.getInstance().statusEvent(status); } } public void groupEvent(Group group, String status, @SuppressWarnings("unused") EventObject p_re) { if (status != null) { setStatus(group.getName() + ":" + status); } if (Group.CONNECT.equals(status)) { // xxx: check to see if group is discoverable discover(group); } if (group.isVisible()) { // do nothing for the invisible npg getPluginContainer().updateGroupState(group); } } /** * Initializes the MyJxta instance */ private void initialize() { configureHTTPProxy(); Group npgWrapper = null; setStatus(STRINGS.getString("status.jxta.startNetPeerGroup")); try { PeerGroup netPeerGroup = new NetPeerGroupFactory().getInterface(); npgWrapper = new Group(this, netPeerGroup, null, false); } catch (PeerGroupException pge) { if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) { LOG.log(Level.SEVERE, "Caught unexpected Exception", pge); } } if (npgWrapper == null) { setStatus(STRINGS.getString("error.jxta.start")); return; } this.peerName = npgWrapper.getPeerGroup().getPeerName(); // this.preferencesManager = new PreferenceManager(this); this.myPreferenceManager = MyPreferenceManager.getInstance(); setStatus(STRINGS.getString("status.jxta.joiningApplicationGroup")); String gid = Constants.getInstance().get(Constants.GROUP_ID, "").trim(); final boolean mainGroupIsNetPeerGroup = gid.length() == 0; // the normal usecase is that we are running in a dedicated myjxta // group... in this usecase the netpeergroup is not important for the user // and therefore should not be visible. // however, if the NPG is our application group we want it to be visible npgWrapper.setVisible(mainGroupIsNetPeerGroup); joinGroup(npgWrapper, false, false); rendezvousNPG = npgWrapper.getPeerGroup().getRendezVousService(); rendezvousNPG.addListener(this); waitForRendezvousConnection(5000); initializeGroup(npgWrapper); seedGroup(this.view.getGroup()); myPreferenceManager.init(); // this.preferencesManager.presetPreferences(); } private void configureHTTPProxy() { Constants c = Constants.getInstance(); URL hp = null; try { hp = c.getURL(Constants.PROXY_HTTP); } catch (ConversionException ce) { if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine("unable to get proxy url: " + c.get(Constants.PROXY_HTTP)); } } System.setProperty(Env.HTTP_PROXY_HOST, hp != null ? hp.getHost() : ""); System.setProperty(Env.HTTP_PROXY_PORT, hp != null ? String.valueOf(hp .getPort()) : ""); } private void initializeGroup(Group parent) { initializeGroup(parent, Constants.GROUP, "/@id"); } private void initializeGroup(Group parent, String queryPrefix, String queryPostfix) { String prefix = queryPrefix != null ? queryPrefix.trim() : null; String postfix = queryPostfix != null ? queryPostfix.trim() : null; if (prefix != null && postfix != null) { Constants c = Constants.getInstance(); for (Object o : c.getAll(prefix + postfix)) { String gid = ((String) o).trim(); if (gid.length() > 0) { String q = prefix + "[@id=\"" + gid + "\"]"; String gn = c.get(q + "/@name", Defaults.GROUP_NAME); String gp = c.get(q + "/@password", null); String gd = c.get(q + "/@description", Defaults.GROUP_DESCRIPTION); String gar = c.get(q + "/@autoRendezVousPeriod", String .valueOf(Constants.AUTO_RENDEZVOUS_JOIN_PERIOD)); setStatus(STRINGS.getString("status.group.create") + " " + gn); Group g = createGroupObject(parent, gn, gd, gp, gid); if (g != null) { joinGroupWithAutoIntervall(gar, g); initializeGroup(g, q, "/group/@id"); // recursive // call (we can // automatically // join inner // groups) } else { setStatus(STRINGS.getString("error.group.create") + " " + gn); } } } } } private void joinGroupWithAutoIntervall(String autoRdvIntervall, Group p_g) { int garp = Constants.AUTO_RENDEZVOUS_JOIN_PERIOD; try { garp = Integer.parseInt(autoRdvIntervall); } catch (NumberFormatException nfe) { if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) LOG.fine("invalid auto rdv: " + autoRdvIntervall); } p_g.setVisible(true); p_g.setAutoRendezVousPeriod(garp); joinGroup(p_g, true, true); // this is the point that can take quite // long (authenticator is started) } private Group createGroupObject(Group parent, String p_gn, String p_gd, String p_gp, String p_gid) { Group g = null; try { PeerGroup ppg = parent.getPeerGroup(); PeerGroupAdvertisement pga = PeerGroupUtil.create(ppg, p_gn, p_gd, p_gp, 0, PeerGroupID.create(new URI(p_gid))); g = new Group(this, ppg.newGroup(pga), parent); } catch (Exception e) { if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) { LOG.log(Level.SEVERE, "Caught unexpected Exception", e); } } return g; } private void seedGroup(Group parent) { String gid = System.getProperty(GROUP_ID, "").trim(); String gn = System.getProperty(GROUP_NAME, "").trim(); String gd = System.getProperty(GROUP_DESCRIPTION, Defaults.GROUP_DESCRIPTION).trim(); String autoRdvIntervall = System.getProperty( GROUP_AUTO_RENDEZVOUS_PERIOD, String.valueOf(Constants.AUTO_RENDEZVOUS_JOIN_PERIOD)).trim(); String gp = null; if (gid.length() > 0) { setStatus(STRINGS.getString("status.group.create") + " " + gn); Group g = createGroupObject(parent, gn, gd, gp, gid); if (g != null) { joinGroupWithAutoIntervall(autoRdvIntervall, g); } else { setStatus(STRINGS.getString("error.group.create") + " " + gn); } } } /** * Restore settings from previous invocation * * @param group */ private void restore(Group group) { // xxx: restore friends, groups, shares PeerGroup pg = group.getPeerGroup(); setStatus(STRINGS.getString("status.group.restore")); for (PeerGroupAdvertisement peerGroupAdvertisement : PeerGroupUtil.getAdvs(pg, null)) { GroupNode parentGroupNode = new GroupNode(group); GroupNode newGroupNode = new GroupNode(new Group(this, peerGroupAdvertisement, group)); newGroupNode.setParent(parentGroupNode); addJxtaNode(newGroupNode); } setStatus(STRINGS.getString("status.peer.restore")); // String term = Dialog.IMFREE_USER_NAME + Dialog.IMFREE_DELIMITER + // SearchModifier.WILDCARD; String term = OneToOneCommandDialog.IMFREE_COMMAND_NAME + Dialog.IMFREE_DELIMITER + SearchModifier.WILDCARD; for (PipeAdvertisement pipeAdvertisement : PipeUtil.getAdvs(pg, term)) { PeerNode node = new PeerNode( new Peer(pipeAdvertisement), group); node.setParent(new GroupNode(group)); addJxtaNode(node); } } private void discover(Group group) { // if (SearchManager.getInstance().search(Searcher.GROUP, group, this)) // { // setStatus(STRINGS.getString("status.group.discover")); // } if (SearchManager.getInstance().search(Searcher.PIPE, group, this)) { setStatus(STRINGS.getString("status.peer.discover")); } } public PluginContainer getPluginContainer() { return m_pluginContainer; } public ArrayList<String> getAdministrationBuffer() { if (administrationBuffer == null) administrationBuffer = new ArrayList<String>(); return administrationBuffer; } /** * rendezvousEvent the rendezvous event * * @param event rendezvousEvent */ public void rendezvousEvent(RendezvousEvent event) { if (event.getType() == RendezvousEvent.RDVCONNECT || event.getType() == RendezvousEvent.RDVRECONNECT || event.getType() == RendezvousEvent.BECAMERDV) { synchronized (networkConnectLock) { networkConnectLock.notify(); } } } /** * Blocks only, if not connected to a rendezvous, or until a connection to rendezvousNPG node occurs. * * @param timeout timeout in milliseconds, a zero timeout of waits forever * @return true if connected to a rendezvous, false otherwise */ public boolean waitForRendezvousConnection(long timeout) { if (0 == timeout) { timeout = Long.MAX_VALUE; } long timeoutAt = System.currentTimeMillis() + timeout; if (timeoutAt <= 0) { // handle overflow. timeoutAt = Long.MAX_VALUE; } while (!rendezvousNPG.isConnectedToRendezVous() && !rendezvousNPG.isRendezVous()) { try { long waitFor = timeoutAt - System.currentTimeMillis(); if (waitFor > 0) { synchronized (networkConnectLock) { networkConnectLock.wait(timeout); } } else { // all done with waiting. break; } } catch (InterruptedException e) { Thread.interrupted(); break; } } return rendezvousNPG.isConnectedToRendezVous() || rendezvousNPG.isRendezVous(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -