Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / real-world application / jigsaw / src / org / w3c / www / protocol / http / icp / ICPQuery.java
diff --git a/JMCR-Stable/real-world application/jigsaw/src/org/w3c/www/protocol/http/icp/ICPQuery.java b/JMCR-Stable/real-world application/jigsaw/src/org/w3c/www/protocol/http/icp/ICPQuery.java
new file mode 100644 (file)
index 0000000..0e5a4b7
--- /dev/null
@@ -0,0 +1,90 @@
+// ICPQuery.java\r
+// $Id: ICPQuery.java,v 1.1 2010/06/15 12:27:36 smhuang Exp $\r
+// (c) COPYRIGHT MIT and INRIA, 1996.\r
+// please first read the full copyright statement in file COPYRIGHT.HTML\r
+\r
+package org.w3c.www.protocol.http.icp;\r
+\r
+import java.net.InetAddress;\r
+import java.net.MalformedURLException;\r
+import java.net.URL;\r
+\r
+class ICPQuery extends ICPMessage {\r
+\r
+    protected int getByteArrayLength() {\r
+       return (super.getByteArrayLength()\r
+                + 4 // sender's address\r
+                + 4 // requestor's adress\r
+               + ((url == null) ? 0 : url.toExternalForm().length()) + 1);\r
+    }\r
+\r
+    protected int toByteArray(byte buf[]) {\r
+       int    off    = super.getByteArrayLength();\r
+       String strurl = (url == null) ? null : url.toExternalForm();\r
+       int    urllen = ((strurl == null) ? 0 : strurl.length());\r
+       // Encode generic payload infos:\r
+       super.toByteArray(buf);\r
+       // Encode query specific fields:\r
+       // Skip the sender's address:\r
+       off += 4;\r
+       // Skip the requestor's address\r
+       off += 4;\r
+       // Encode the URL (null terminated string):\r
+       if ( urllen > 0 )\r
+           strurl.getBytes(0, urllen, buf, off);\r
+       buf[off+urllen] = 0;\r
+       return off+urllen+1;\r
+    }\r
+       \r
+    protected int parse(byte buf[], int off, int len)\r
+       throws ICPProtocolException\r
+    {\r
+       off = super.parse(buf, off, len);\r
+       // Skip sender's address:\r
+       off += 4;\r
+       // Skip the requestor's host address:\r
+       off += 4;\r
+       // Read in the URL:\r
+       for (int i = off ; i < len ; i++) {\r
+           if (buf[i] == 0) {\r
+               // Is there a real URL ?\r
+               if ( i - off < 1 )\r
+                   return i;\r
+               // Yep, parse it:\r
+               String strurl = new String(buf, 0, off, i-off);\r
+               try {\r
+                   this.url = new URL(strurl);\r
+               } catch (MalformedURLException ex) {\r
+                   throw new ICPProtocolException("Invalid URL:"+strurl);\r
+               }\r
+               return i;\r
+           }\r
+       }\r
+       throw new ICPProtocolException("Invalid URL encoding");\r
+    }\r
+\r
+    /**\r
+     * Create an ICP query.\r
+     * @param id The identifier of the message.\r
+     * @param url The queried URL.\r
+     */\r
+\r
+    ICPQuery(int id, URL url) {\r
+       this.opcode = ICP_OP_QUERY;\r
+       this.id     = id;\r
+       this.url    = url;\r
+    }\r
+\r
+    ICPQuery(InetAddress addr, int port\r
+            , int opcode, int version\r
+            , byte buf[], int off, int len) \r
+       throws ICPProtocolException\r
+    {\r
+       this.addr    = addr;\r
+       this.port    = port;\r
+       this.opcode  = opcode;\r
+       this.version = version;\r
+       parse(buf, off, len);\r
+    }\r
+\r
+}\r