Author: mladen.turk(a)jboss.com
Date: 2008-06-11 01:44:05 -0400 (Wed, 11 Jun 2008)
New Revision: 1687
Modified:
sandbox/aloha/java/org/jboss/aloha/advertise/AdvertiseListener.java
sandbox/aloha/java/org/jboss/aloha/advertise/AdvertisedServer.java
sandbox/aloha/java/org/jboss/aloha/advertise/IAdvertiseEvent.java
Log:
Use sequence and security
Modified: sandbox/aloha/java/org/jboss/aloha/advertise/AdvertiseListener.java
===================================================================
--- sandbox/aloha/java/org/jboss/aloha/advertise/AdvertiseListener.java 2008-06-10
16:16:44 UTC (rev 1686)
+++ sandbox/aloha/java/org/jboss/aloha/advertise/AdvertiseListener.java 2008-06-11
05:44:05 UTC (rev 1687)
@@ -295,13 +295,15 @@
}
}
- private boolean verifyDigest(String digest, String server, String date)
+ private boolean verifyDigest(String digest, String server, String date,
+ String sequence)
{
if (md == null)
return true;
md.reset();
digestString(md, securityKey);
digestString(md, date);
+ digestString(md, sequence);
digestString(md, server);
byte [] our = md.digest();
byte [] dst = new byte[digest.length() * 2];
@@ -369,10 +371,12 @@
if (!s.startsWith("HTTP/1."))
continue;
- String [] headers = s.split("\r\n");
+ String [] headers = s.split("\r\n");
String date_str = null;
- Date date = null;
- int status = 0;
+ String seq_str = null;
+ Date date = null;
+ int status = 0;
+ long sequence = 0;
String status_desc = null;
String digest = null;
String server_name = null;
@@ -411,6 +415,10 @@
added = true;
}
}
+ else if (hdrv[0].equals("Sequence")) {
+ seq_str = hdrv[1];
+ sequence = Long.parseLong(seq_str);
+ }
else if (server != null) {
server.setParameter(hdrv[0], hdrv[1]);
}
@@ -418,21 +426,36 @@
}
if (server != null && status > 0) {
if (md != null) {
- /* We need a digest to match */
- if (!verifyDigest(digest, server_name, date_str)) {
- System.out.println("Digest mismatch");
+ /* Shared security key was set.
+ * We need a matching digest
+ */
+ if (!verifyDigest(digest, server_name, date_str, seq_str)) {
+ // XXX: We could add event handler here so that
+ // mismatched digest can be logged.
continue;
}
}
server.setDate(date);
- boolean rc = server.setStatus(status, status_desc);
+ if (!added) {
+ long prev_sequence = server.getSequence();
+ if (++prev_sequence < 1)
+ prev_sequence = 1;
+ if (prev_sequence != sequence) {
+ /* Sequence mismatch.
+ * Seems we didn't receive one of the
+ * advertise messages from that server
+ */
+ }
+ }
+ server.setSequence(sequence);
+ boolean sc = server.setStatus(status, status_desc);
if (added) {
servers.put(server_name, server);
- // Call the new server callback
+ // Notify that we have a new server
eventHandler.onEvent(AdvertiseEventType.ON_NEW_SERVER,
server);
}
- else if (rc) {
- // Call the status change callback
+ else if (sc) {
+ // Notify that status has been changed
eventHandler.onEvent(AdvertiseEventType.ON_STATUS_CHANGE,
server);
}
}
Modified: sandbox/aloha/java/org/jboss/aloha/advertise/AdvertisedServer.java
===================================================================
--- sandbox/aloha/java/org/jboss/aloha/advertise/AdvertisedServer.java 2008-06-10 16:16:44
UTC (rev 1686)
+++ sandbox/aloha/java/org/jboss/aloha/advertise/AdvertisedServer.java 2008-06-11 05:44:05
UTC (rev 1687)
@@ -26,7 +26,7 @@
import java.util.Date;
import java.util.HashMap;
-import java.util.Collection;
+import java.util.Collection;
/**
* Advertised server instance
@@ -39,6 +39,7 @@
private String server;
private Date date;
private int status;
+ private long sequence;
private String status_desc;
private HashMap<String, String> headers;
@@ -62,7 +63,7 @@
{
this.server = server;
headers = new HashMap<String, String>();
- }
+ }
protected boolean setStatus(int status, String desc)
{
@@ -70,7 +71,7 @@
status_desc = desc;
if (this.status == 0 ) {
// First time
- this.status = status;
+ this.status = status;
}
else if (this.status != status) {
this.status = status;
@@ -78,40 +79,40 @@
}
return rv;
}
-
- /** Set the Date of the last Advertise message
+
+ /** Get Status code of the last Advertise message
*/
- protected void setDate(Date date)
+ public int getStatusCode()
{
- this.date = date;
+ return status;
}
- /** Set the Header
+ /** Get Status description of the last Advertise message
*/
- protected void setParameter(String name, String value)
+ public String getStatusDescription()
{
- headers.put(name, value);
+ return status_desc;
}
- /** Get Date of the last Advertise message
+ /** Set the Date of the last Advertise message
*/
- public Date getDate()
+ protected void setDate(Date date)
{
- return date;
+ this.date = date;
}
- /** Get Status code of the last Advertise message
+ /** Get Date of the last Advertise message
*/
- public int getStatusCode()
+ public Date getDate()
{
- return status;
+ return date;
}
- /** Get Status description of the last Advertise message
+ /** Set the Header
*/
- public String getStatusDescription()
+ protected void setParameter(String name, String value)
{
- return status_desc;
+ headers.put(name, value);
}
/** Get Advertise parameter
@@ -121,8 +122,22 @@
return headers.get(name);
}
+ /** Set the Sequence number
+ */
+ protected void setSequence(long sequence)
+ {
+ this.sequence = sequence;
+ }
+
+ /** Get Sequence number of the last Advertise message
+ */
+ public long getSequence()
+ {
+ return sequence;
+ }
+
public String toString()
{
return server;
- }
+ }
}
Modified: sandbox/aloha/java/org/jboss/aloha/advertise/IAdvertiseEvent.java
===================================================================
--- sandbox/aloha/java/org/jboss/aloha/advertise/IAdvertiseEvent.java 2008-06-10 16:16:44
UTC (rev 1686)
+++ sandbox/aloha/java/org/jboss/aloha/advertise/IAdvertiseEvent.java 2008-06-11 05:44:05
UTC (rev 1687)
@@ -35,8 +35,8 @@
/** Callback for AdvertiseServer.
* @param type AdvertiseEventType caused by this event.
- * @param server AdvertisedServer causing this event.
+ * @param server AdvertisedServer causing this event.
*/
public void onEvent(AdvertiseEventType type, AdvertisedServer server);
-
+
}