📄 rendezvousserviceimpl.java
字号:
* @exception IOException Description of Exception
*/
private void disconnectFromRendezVous(EndpointMessenger messenger)
throws IOException {
if (messenger == null) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug(" messenger is null");
throw new IOException(" messenger is null");
}
Message msg = endpoint.newMessage();
// The request simply includes the local peer advertisement.
try {
InputStream ip = null;
Document doc = null;
doc = localPeerAdv.getDocument(new MimeMediaType("text/xml"));
ip = doc.getStream();
msg.addElement (msg.newMessageElement (DisconnectRequest,
new MimeMediaType("text/xml"),
ip));
messenger.sendMessage(msg);
} catch (Exception e) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("disconnectFromRendezVous failed", e);
throw new IOException("disconnectFromRendezVous failed");
}
}
/**
*Description of the Method
*
* @param msg Description of Parameter
*/
private void processConnectRequest(Message msg) {
if (LOG.isDebugEnabled()) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("processConnectRequest");
}
if ((!isRendezVous) || (manager == null)) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug(" not a rendezvous");
return;
}
PeerAdvertisement adv = null;
try {
MessageElement elem = msg.getElement(ConnectRequest);
msg.removeElement (ConnectRequest);
adv = (PeerAdvertisement)
AdvertisementFactory.newAdvertisement(new MimeMediaType("text/xml"),
elem.getStream());
} catch (Exception e) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug(" cannot retrieve advertisment from request");
return;
}
if (adv == null) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug(" advertisement is null");
return;
}
long lease = manager.requestConnection(adv);
// Send the reply
if (!sendReply(adv.getPeerID().toString(), adv, lease)) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("processConnectRequest. Cannot reach client "
+ adv.getPeerID());
// The client is not reachable. Just drop it
return;
}
if (lease != 0) {
if (LOG.isDebugEnabled()) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug(" addClient");
}
addClient(adv.getPeerID().toString(), lease);
}
return;
}
/**
*Description of the Method
*
* @param msg Description of Parameter
*/
private void processDisconnectRequest(Message msg) {
if ((!isRendezVous) || (manager == null)) {
return;
}
PeerAdvertisement adv = null;
try {
adv = (PeerAdvertisement)
AdvertisementFactory.newAdvertisement(
new MimeMediaType("text/xml"),
msg.getElement(DisconnectRequest).getStream());
} catch (Exception e) {
return;
}
if (adv == null) {
return;
}
removeClient(adv.getPeerID().toString());
return;
}
/**
*Description of the Method
*
* @param msg Description of Parameter
*/
private void processConnectedReply(Message msg) {
if (LOG.isDebugEnabled()) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("processConnectedReply");
}
int size = 0;
InputStream is = msg.getElement(ConnectedRdvAdvReply).getStream();
Advertisement adv = null;
if (is != null) {
try {
adv = AdvertisementFactory.newAdvertisement(new MimeMediaType("text/xml"), is);
} catch (Exception e) {
}
}
if (adv == null) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug(" no advertisement");
} else {
// Make sure that EndpointService has also the document
publishInParentGroup (adv);
try {
// This is not our own peer adv so we must not keep it
// longer than its expiration time.
group.getDiscoveryService().publish(adv,
DiscoveryService.PEER,
DiscoveryService.DEFAULT_EXPIRATION,
DiscoveryService.DEFAULT_EXPIRATION);
} catch (Exception e) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug(" publish failed with " + e);
}
}
long lease = 0;
try {
MessageElement el = msg.getElement(ConnectedLeaseReply);
if (el == null) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("missing lease");
return;
}
byte[] buffer = el.getBytesOffset();
int offset = el.getOffset();
int len = el.getLength();
lease = Long.parseLong(new String(buffer, offset, len));
if (LOG.isDebugEnabled()) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("lease=" + lease);
}
} catch (Exception e) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug(" leased failed with " + e);
return;
}
String peer = null;
try {
MessageElement el = msg.getElement(ConnectedPeerReply);
if (el == null) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("missing rdv peer");
return;
}
byte[] buffer = el.getBytesOffset();
peer = new String(el.getBytesOffset(), el.getOffset(), el.getLength());
if (LOG.isDebugEnabled()) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("peer=" + peer);
}
} catch (Exception e) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug(" getting peer failed with " + e);
return;
}
PeerID pId = null;
try {
pId = (PeerID) IDFactory.fromURL(new URL(peer));
} catch (Exception e) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug(" getting peer advertisement failed with " + e);
return;
}
if (lease == 0) {
if (monitor != null) {
monitor.disconnected(pId);
}
} else {
addRdv(peer, lease);
if (monitor != null) {
monitor.connected(pId, lease);
}
}
}
/**
*Description of the Method
*
* @param msg Description of Parameter
*/
private void processRdvAdvReply(Message msg) {
if (monitor == null) {
return;
}
Advertisement adv = null;
try {
adv = AdvertisementFactory.newAdvertisement(
new MimeMediaType("text/xml"),
msg.getElement(RdvAdvReply).getStream());
} catch (Exception e) {
return;
}
if (adv == null) {
return;
}
monitor.discovered(adv);
return;
}
/**
*Description of the Method
*
* @param peer Description of Parameter
* @param adv Description of Parameter
* @param lease Description of Parameter
* @return Description of the Returned Value
*/
private boolean sendReply(String peer, Advertisement adv, long lease) {
localPeerAdv = group.getPeerAdvertisement();
if (LOG.isDebugEnabled()) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("sendReply");
}
// Try to get an EndpointMessenger to that peer.
// First publish the advertisement
try {
// This is not our own peer adv so we must not keep it
// longer than its expiration time.
group.getDiscoveryService().publish(adv,
DiscoveryService.PEER,
DiscoveryService.DEFAULT_EXPIRATION,
DiscoveryService.DEFAULT_EXPIRATION);
} catch (Exception e) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug(" publish failed with " + e);
}
// Make sure that EndpointService has also the document
publishInParentGroup (adv);
ID asID;
EndpointAddress addr;
try {
addr = mkAddress(peer,
pName,
pParam);
} catch (Exception caught) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug(" malformed peer id " + peer);
return false;
}
// Check that the EndpointService can talk to that peer
if (!endpoint.ping(addr)) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug(" cannot get route to peer " + addr);
return false;
}
EndpointMessenger messenger = null;
try {
messenger = endpoint.getMessenger(addr);
if (messenger == null) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug(" no messenger");
return false;
}
} catch (Exception e) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug(" getting messenger failed with " + e);
return false;
}
Message msg = endpoint.newMessage();
ByteArrayInputStream ipPeer =
new ByteArrayInputStream(localPeerId.getBytes());
ByteArrayInputStream ipLease =
new ByteArrayInputStream(Long.toString(lease).getBytes());
try {
InputStream ip = null;
Document doc = null;
doc = localPeerAdv.getDocument(new MimeMediaType("text/xml"));
ip = doc.getStream();
msg.addElement (msg.newMessageElement (ConnectedRdvAdvReply,
new MimeMediaType("text/xml"),
ip));
msg.setString (ConnectedPeerReply, localPeerId);
msg.setString (ConnectedLeaseReply, Long.toString(lease));
messenger.sendMessage(msg);
if (LOG.isDebugEnabled()) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug(" message sent");
}
} catch (Exception e) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug(" failed with " + e);
return false;
}
return true;
}
/**
* Propagates on all endpoint protocols.
* This is the internal version. See also the public one that adds
* ttl/loop control.
*
* Note: The original msg is not modified and may be reused upon return.
*
* @param msg is the message to propagate.
* @param serviceName is the name of the service
* @param queueName Description of Parameter
*/
private void sendToNetwork(Message msg,
String serviceName,
String queueName)
{
try {
endpoint.propagate(msg,
serviceName,
queueName);
} catch (Exception e) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug(e.toString());
}
}
/**
* Sends to all rdv clients.
* This is the internal version. See also the public one that adds
* ttl/loop control.
*
* Note: The original msg is not modified and may be reused upon return.
*
* @param msg is the message to propagate.
* @param serviceName is the name of the service
* @param serviceParam is the parameter of the service
*/
private void sendToEachClient(Message msg,
String serviceName,
String serviceParam)
{
if (LOG.isDebugEnabled()) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("sendToEachClient");
}
if ((!isRendezVous) || (clients == null) || (clients.size() == 0)) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug(" no clients");
return;
}
// Do it backwards so that we get the size only once and
// do not have to make adjustments when we remove en
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -