- Completed (???) librarian searches being propagated over the directory interface.

ma810 [2002-06-17 21:51:46]
- Completed (???) librarian searches being propagated over the directory interface.
Filename
server/data/DataServer.java
server/data/DataServerEventReceiver.java
server/librarian/LibrarianRequest.java
diff --git a/server/data/DataServer.java b/server/data/DataServer.java
index ef7bcef..cfdc192 100644
--- a/server/data/DataServer.java
+++ b/server/data/DataServer.java
@@ -7,7 +7,7 @@

 package psl.chime4.server.data;

-import java.io.StringReader;
+import java.io.*;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.util.*;
@@ -29,7 +29,8 @@ import psl.chime4.server.librarian.*;
  * @author Mark Ayzenshtat
  */
 public class DataServer {
-	public static final DIType kDirectoryType = new DIType("LibrarianSearch");
+	public static final DIType kLibrarianSearchMessageType
+    = new DIType("LibrarianSearch");
 	private static final ResourceDescriptor[] kZeroResDes =
 		new ResourceDescriptor[0];

@@ -59,7 +60,7 @@ public class DataServer {
 		}

 		// subscribe to directory service
-		mDirInterface.subscribe(mDirEventReceiver, kDirectoryType);
+		mDirInterface.subscribe(mDirEventReceiver, kLibrarianSearchMessageType);

 		mInitialized = true;
 	}
@@ -73,7 +74,7 @@ public class DataServer {
 		}

 		// unsubscribe from directory service
-		mDirInterface.unsubscribe(mDirEventReceiver, kDirectoryType);
+		mDirInterface.unsubscribe(mDirEventReceiver, kLibrarianSearchMessageType);

 		mInitialized = false;
 	}
@@ -145,9 +146,24 @@ public class DataServer {
 		notifyLibrarianOfResult(result);
 	}

-	private void librarianSearchNetworkDataStores(LibrarianRequest iReq) {
-		// TODO: Implement directory interface code here
-
+	private void librarianSearchNetworkDataStores(LibrarianRequest iReq) {
+    // convert entire LibrarianRequest object into a byte array
+    ByteArrayOutputStream baos = new ByteArrayOutputStream(100);
+
+    try {
+      ObjectOutputStream oos = new ObjectOutputStream(baos);
+      oos.writeObject(iReq);
+    } catch (IOException ex) {
+      throw new
+        RuntimeException("Couldn't package librarian request for transport.");
+    }
+
+    byte[] libReqAsBytes = baos.toByteArray();
+
+    // publish library request for other data servers to discover
+    mDirInterface.publish(kLibrarianSearchMessageType,
+      new DIMessageBody(libReqAsBytes));
+
 /*
 		// save results to local data store using
 		// some intelligent persistence scheme
@@ -238,9 +254,54 @@ public class DataServer {

 		return iRD;
 	}
+
+  /**
+   * An implementation of the directory service <code>DIEventReceiver</code>
+   * interface, through which this data server communicates with other data
+   * servers across the network.
+   *
+   * @author Mark Ayzenshtat
+   */
+  private static class DataServerEventReceiver implements DIEventReceiver {
+    private DataServer mDataServer;
+
+    DataServerEventReceiver(DataServer iDataServer) {
+      mDataServer = iDataServer;
+    }
+
+    public void receiveMessage(DIMessage iMessage) {
+      LibrarianRequest request;
+      ByteArrayInputStream bais = new ByteArrayInputStream(
+        iMessage.getBody().toBytes());
+
+      try {
+        ObjectInputStream ois = new ObjectInputStream(bais);
+        request = (LibrarianRequest) ois.readObject();
+      } catch (Exception ex) {
+        throw new
+          RuntimeException(
+            "Couldn't unpackage librarian request from transport."
+          );
+      }
+
+      mDataServer.librarianSearchLocalDataStore(request);
+
+      // FIXME: We need to get the result, package it up, and send it back
+    }
+
+    public void receiveEvent(DIEvent iEvent) {
+      // we don't care about events
+    }
+
+    public void receiveResult(DIHost iResult) {
+      // we don't care about results
+    }
+  }

 	public static void main(String[] args) {
 		DataServer ds = new DataServer();
+
+    ds.startup();

 		ResourceDescriptor rd = ResourceDescriptorFactory
 			.getInstance().newRD("text/html");
@@ -261,5 +322,5 @@ public class DataServer {
     for (int i = 0; i < links.length; i++) {
       System.out.println(links[i]);
     }
-  }
+  }
 }
\ No newline at end of file
diff --git a/server/data/DataServerEventReceiver.java b/server/data/DataServerEventReceiver.java
deleted file mode 100644
index d26e702..0000000
--- a/server/data/DataServerEventReceiver.java
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * DataServerEventReceiver.java
- *
- * Copyright (c) 2002: The Trustees of Columbia University
- * in the City of New York.  All Rights Reserved.
- */
-
-package psl.chime4.server.data;
-
-import psl.chime4.server.di.*;
-
-/**
- * An implementation of the directory service <code>DIEventReceiver</code>
- * interface, through which this data server communicates with other data
- * servers across the network.
- *
- * @author Mark Ayzenshtat
- */
-class DataServerEventReceiver implements DIEventReceiver {
-	private DataServer mDataServer;
-
-	DataServerEventReceiver(DataServer iDataServer) {
-		mDataServer = iDataServer;
-	}
-
-	public void receiveMessage(DIMessage iMessage) {
-	}
-
-	public void receiveEvent(DIEvent iEvent) {
-	}
-
-	public void receiveResult(DIHost iResult) {
-	}
-}
\ No newline at end of file
diff --git a/server/librarian/LibrarianRequest.java b/server/librarian/LibrarianRequest.java
index 8e17155..6761234 100644
--- a/server/librarian/LibrarianRequest.java
+++ b/server/librarian/LibrarianRequest.java
@@ -7,6 +7,7 @@

 package psl.chime4.server.librarian;

+import java.io.Serializable;
 import java.net.URI;

 /**
@@ -16,7 +17,7 @@ import java.net.URI;
  *
  * @author Mark Ayzenshtat
  */
-public class LibrarianRequest {
+public class LibrarianRequest implements Serializable {
 	private String mQuery;
 	private URI[] mURIs;