summaryrefslogtreecommitdiff
path: root/java/src
diff options
context:
space:
mode:
authorpjain <pjain@ae88bc3d-4319-0410-8dbf-d08b4c9d3795>1997-04-23 17:32:05 +0000
committerpjain <pjain@ae88bc3d-4319-0410-8dbf-d08b4c9d3795>1997-04-23 17:32:05 +0000
commitc922b100b9bf597e4f4891c4955e8ba113c27e9e (patch)
tree15aefd90c163e148b6304fd307956eefdc55a9e8 /java/src
parentfaa064c0246797490e46cbc9aa1c5952e4ab9cb2 (diff)
downloadATCD-c922b100b9bf597e4f4891c4955e8ba113c27e9e.tar.gz
Added some new files
Diffstat (limited to 'java/src')
-rw-r--r--java/src/Blob.java94
-rw-r--r--java/src/BlobHandler.java35
-rw-r--r--java/src/BlobReader.java104
-rw-r--r--java/src/BlobWriter.java193
-rw-r--r--java/src/Makefile6
5 files changed, 431 insertions, 1 deletions
diff --git a/java/src/Blob.java b/java/src/Blob.java
new file mode 100644
index 00000000000..69feef248b3
--- /dev/null
+++ b/java/src/Blob.java
@@ -0,0 +1,94 @@
+/*************************************************
+ *
+ * = PACKAGE
+ * JACE.Connection
+ *
+ * = FILENAME
+ * Blob.java
+ *
+ *@author Prashant Jain
+ *
+ *************************************************/
+package JACE.Connection;
+
+import java.io.*;
+import java.net.*;
+import JACE.SOCK_SAP.*;
+import JACE.ASX.*;
+import JACE.OS.*;
+
+public class Blob
+{
+ public int open (String filename, String hostname , int port)
+ {
+ this.filename_ = filename;
+ this.hostname_ = hostname;
+ this.port_ = port;
+ return 0;
+ }
+
+ public MessageBlock read (int length, int offset)
+ {
+ // Check if we have a valid length and a valid offset
+ if (length < 0 || offset < 0)
+ {
+ ACE.ERROR ("Blob::read(): Negative length or offset");
+ return null;
+ }
+
+ // Create a Blob Reader
+ BlobReader blobReader = new BlobReader (length, offset, this.filename_, this.hostname_, this.port_);
+
+ // Receive data
+ MessageBlock mb = blobReader.receiveData ();
+ if (blobReader.bytesRead () != length)
+ return null;
+ else
+ return mb;
+ }
+
+ public int write (MessageBlock mb, int length, int offset)
+ {
+ // Check if we have a valid length and a valid offset
+ if (length < 0 || offset < 0)
+ ACE.ERROR ("Blob::write(): Negative length or offset");
+
+ // Create a Blob Writer
+ BlobWriter blobWriter = new BlobWriter (mb, length, offset, this.filename_);
+
+ try
+ {
+ // Connect to the server
+ this.connector_.open (this.hostname_, this.port_);
+ this.connector_.connect (blobWriter);
+ }
+ catch (UnknownHostException e)
+ {
+ ACE.ERROR (e);
+ }
+ catch (InstantiationException e)
+ {
+ ACE.ERROR (e);
+ }
+ catch (IllegalAccessException e)
+ {
+ ACE.ERROR (e);
+ }
+ catch (IOException e)
+ {
+ ACE.ERROR (e);
+ }
+
+ return blobWriter.bytesWritten ();
+ }
+
+ public int close ()
+ {
+ return 0;
+ }
+
+ String filename_;
+ String hostname_;
+ int port_;
+ Connector connector_ = new Connector ();
+}
diff --git a/java/src/BlobHandler.java b/java/src/BlobHandler.java
new file mode 100644
index 00000000000..ec282bdeaa9
--- /dev/null
+++ b/java/src/BlobHandler.java
@@ -0,0 +1,35 @@
+/*************************************************
+ *
+ * = PACKAGE
+ * JACE.Connection
+ *
+ * = FILENAME
+ * BlobHandler.java
+ *
+ *@author Prashant Jain
+ *
+ *************************************************/
+package JACE.Connection;
+
+import java.io.*;
+import java.net.*;
+import JACE.SOCK_SAP.*;
+import JACE.ASX.*;
+import JACE.OS.*;
+
+public abstract class BlobHandler extends SvcHandler
+{
+ public BlobHandler (int length, int offset, String filename)
+ {
+ this.length_ = length;
+ this.offset_ = offset;
+ this.filename_ = filename;
+ }
+
+ public abstract int open (Object obj);
+
+ protected int length_ = 0;
+ protected int offset_ = 0;
+ protected String filename_ = null;
+}
+
diff --git a/java/src/BlobReader.java b/java/src/BlobReader.java
new file mode 100644
index 00000000000..6f37baef028
--- /dev/null
+++ b/java/src/BlobReader.java
@@ -0,0 +1,104 @@
+/*************************************************
+ *
+ * = PACKAGE
+ * JACE.Connection
+ *
+ * = FILENAME
+ * BlobReader.java
+ *
+ *@author Prashant Jain
+ *
+ *************************************************/
+package JACE.Connection;
+
+import java.io.*;
+import java.net.*;
+import JACE.SOCK_SAP.*;
+import JACE.ASX.*;
+import JACE.OS.*;
+
+// Reader **************************************************
+
+public class BlobReader
+{
+ public BlobReader (int length,
+ int offset,
+ String filename,
+ String hostname,
+ int port)
+ {
+ this.length_ = length;
+ this.offset_= offset;
+ this.filename_ = filename;
+ this.hostname_ = hostname;
+ this.port_ = port;
+ }
+
+
+ public MessageBlock receiveData ()
+ {
+ String hostname = this.hostname_;
+ String filename = this.filename_;
+
+ // Check if the filename begins with a "/" and if so, remove it
+ // since we are concatenating a "/" to the hostname.
+ if (this.filename_.startsWith ("/"))
+ filename = this.filename_.substring (1);
+
+ hostname = hostname + ":" + this.port_ + "/";
+ // System.out.println (hostname + filename);
+
+ // Allocate a buffer to hold the offset worth of data
+ byte tempBuf [] = new byte [this.offset_];
+ // Allocate a buffer to hold the actual data
+ byte dataBuf [] = new byte [this.length_];
+
+ try
+ {
+ // Create a URL to fetch the file
+ URL url = new URL (this.protocol_ + hostname + filename);
+
+ // Get the input stream and pipe it to a DataInputStream
+ DataInputStream iStream = new DataInputStream (url.openStream ());
+
+ // Read the offset worth of bytes
+ iStream.readFully (tempBuf, 0, this.offset_);
+
+ // Read length worth of bytes
+ iStream.readFully (dataBuf, 0, this.length_);
+ }
+ catch (MalformedURLException e)
+ {
+ ACE.ERROR (e);
+ }
+ catch (IOException e)
+ {
+ ACE.ERROR (e);
+ }
+ // Cache number of bytes read
+ this.bytesRead_ = this.length_;
+ return new MessageBlock (new String (dataBuf, 0));
+ }
+
+ public int close (long flags)
+ {
+ return 0;
+ }
+
+ public int bytesRead ()
+ {
+ return this.bytesRead_;
+ }
+
+ private String protocol_ = "http://";
+
+ int length_ = 0;
+ int offset_= 0;
+ String filename_ = null;
+ String hostname_ = "localhost";
+ int port_ = 80;
+
+ int bytesRead_ = 0;
+}
+
+
diff --git a/java/src/BlobWriter.java b/java/src/BlobWriter.java
new file mode 100644
index 00000000000..4c4d5d1d31b
--- /dev/null
+++ b/java/src/BlobWriter.java
@@ -0,0 +1,193 @@
+/*************************************************
+ *
+ * = PACKAGE
+ * JACE.Connection
+ *
+ * = FILENAME
+ * BlobWriter.java
+ *
+ *@author Prashant Jain
+ *
+ *************************************************/
+package JACE.Connection;
+
+import java.io.*;
+import java.net.*;
+import JACE.SOCK_SAP.*;
+import JACE.ASX.*;
+import JACE.OS.*;
+
+// Writer **************************************************
+
+public class BlobWriter extends BlobHandler
+{
+ public BlobWriter (MessageBlock mb,
+ int length,
+ int offset,
+ String filename)
+ {
+ super (length, offset, filename);
+ this.mb_ = mb;
+ this.returnCode_ = -1;
+ }
+
+ public int open (Object obj)
+ {
+ if (this.sendRequest () != 0)
+ {
+ ACE.ERROR ("BlobWriter::open():sendRequest failed");
+ return -1;
+ }
+ else if (this.receiveReply () != 0)
+ {
+ ACE.ERROR ("BlobWriter::open():receiveReply failed");
+ return -1;
+ }
+ return 0;
+ }
+
+ public int close (long flags)
+ {
+ return 0;
+ }
+
+ public int bytesWritten ()
+ {
+ return this.bytesWritten_;;
+ }
+
+ protected int sendRequest ()
+ {
+ // Check for sanity -- check if we have any data to send.
+ if (this.offset_+ this.length_ > this.mb_.length ())
+ {
+ ACE.ERROR ("BlobWriter::sendRequest():Invalid offset/length");
+ return -1;
+ }
+
+ if (this.sendHeader () == -1)
+ {
+ ACE.ERROR ("BlobWriter::sendHeader failed.");
+ return -1;
+ }
+ else
+ if (this.sendData () == -1)
+ {
+ ACE.ERROR ("BlobWriter::sendData failed.");
+ return -1;
+ }
+ return 0;
+ }
+
+ // Send the header
+ protected int sendHeader ()
+ {
+ String filename = this.filename_;
+ // Check if the filename begins with a "/" and if it doesn't, add it
+ if (!this.filename_.startsWith ("/"))
+ filename = "/" + this.filename_;
+
+ // Create the header, store the actual length in mesglen
+ String mesg = this.requestPrefix_ + " " + filename + " " + this.requestSuffix_ + " " + this.length_ + "\n\n";
+ System.out.print (mesg);
+
+ try
+ {
+ if (this.peer ().send (mesg) < 0)
+ {
+ ACE.ERROR ("Error sending request");
+ return -1;
+ }
+ }
+ catch (IOException e)
+ {
+ ACE.ERROR (e);
+ return -1;
+ }
+ return 0;
+ }
+
+ // Send the data
+ protected int sendData ()
+ {
+ // Get the actual data to send
+ String data = this.mb_.base ().substring (this.offset_,
+ this.offset_ + this.length_);
+
+ try
+ {
+ // System.out.println (data);
+ // Now send the data
+ if (this.peer ().send (data) != this.length_)
+ {
+ ACE.ERROR ("Error sending file");
+ return -1;
+ }
+ }
+ catch (IOException e)
+ {
+ ACE.ERROR (e);
+ return -1;
+ }
+ this.bytesWritten_ = this.length_;
+ return 0;
+ }
+
+
+ protected int receiveReply ()
+ {
+ // Receive the reply from the server
+ StringBuffer reply = new StringBuffer (1024);
+
+ try
+ {
+ if (this.peer ().recv (reply) < 0)
+ {
+ ACE.ERROR ("Error receiving reply from server");
+ return -1;
+ }
+ }
+ catch (IOException e)
+ {
+ ACE.ERROR (e);
+ }
+
+ String s = reply.toString ();
+
+ int index = -1;
+ // Now parse the reply to see if it was a success or a failure
+ if ((index = s.indexOf (replyPrefix_)) == -1)
+ {
+ ACE.ERROR ("Error receiving reply from server");
+ return -1;
+ }
+
+ int codeIndex = index + replyPrefix_.length () + 1;
+
+ // Assume code is a 3 digit number
+ String codeString = s.substring (codeIndex, codeIndex + 3);
+
+ returnCode_ = (new Integer (codeString)).intValue ();
+ // System.out.println (code);
+
+ if (returnCode_ >= 200 && returnCode_ < 300) // Check if everything went smoothly
+ return 0;
+ else
+ return -1;
+ }
+
+ public int returnCode ()
+ {
+ return this.returnCode_;
+ }
+
+ protected String protocol_ = "http://";
+ protected int bytesWritten_ = 0;
+ protected MessageBlock mb_ = null;
+ protected String requestPrefix_ = "PUT";
+ protected String requestSuffix_ = "HTTP/1.0\nContent-length:";
+ protected String replyPrefix_ = "HTTP/1.0";
+ protected int returnCode_;
+}
+
+
diff --git a/java/src/Makefile b/java/src/Makefile
index 8d2fab2f79b..b9a8ae5efcd 100644
--- a/java/src/Makefile
+++ b/java/src/Makefile
@@ -84,7 +84,11 @@ pkg_connection = \
AcceptStrategy \
ActivateStrategy \
CreationStrategy \
- StrategyAcceptor
+ StrategyAcceptor \
+ Blob \
+ BlobHandler \
+ BlobReader \
+ BlobWriter
connection: os socksap svcconfig $(addsuffix .java,$(pkg_connection))
$(COMPILE.java)