[P2play-commit] SF.net SVN: p2play: [28] trunk/P2PlayBoot/src/org/p2play/boot
Status: Pre-Alpha
Brought to you by:
tisoft
|
From: <ti...@us...> - 2007-01-22 12:50:57
|
Revision: 28
http://p2play.svn.sourceforge.net/p2play/?rev=28&view=rev
Author: tisoft
Date: 2007-01-22 04:50:57 -0800 (Mon, 22 Jan 2007)
Log Message:
-----------
Initial import.
A PAST based virtual file system
Added Paths:
-----------
trunk/P2PlayBoot/src/org/p2play/boot/vfs/
trunk/P2PlayBoot/src/org/p2play/boot/vfs/FilePastContent.java
trunk/P2PlayBoot/src/org/p2play/boot/vfs/FolderPastContent.java
trunk/P2PlayBoot/src/org/p2play/boot/vfs/ImagineryPastContent.java
trunk/P2PlayBoot/src/org/p2play/boot/vfs/PastDeserializer.java
trunk/P2PlayBoot/src/org/p2play/boot/vfs/PastryFileObject.java
trunk/P2PlayBoot/src/org/p2play/boot/vfs/PastryFileProvider.java
trunk/P2PlayBoot/src/org/p2play/boot/vfs/PastryFileSystem.java
Added: trunk/P2PlayBoot/src/org/p2play/boot/vfs/FilePastContent.java
===================================================================
--- trunk/P2PlayBoot/src/org/p2play/boot/vfs/FilePastContent.java (rev 0)
+++ trunk/P2PlayBoot/src/org/p2play/boot/vfs/FilePastContent.java 2007-01-22 12:50:57 UTC (rev 28)
@@ -0,0 +1,100 @@
+package org.p2play.boot.vfs;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import rice.p2p.commonapi.Endpoint;
+import rice.p2p.commonapi.Id;
+import rice.p2p.commonapi.NodeHandle;
+import rice.p2p.commonapi.rawserialization.InputBuffer;
+import rice.p2p.commonapi.rawserialization.OutputBuffer;
+import rice.p2p.past.Past;
+import rice.p2p.past.PastContent;
+import rice.p2p.past.PastContentHandle;
+import rice.p2p.past.PastException;
+import rice.p2p.past.rawserialization.RawPastContent;
+
+public class FilePastContent implements RawPastContent {
+
+ private Id id;
+
+ private byte[] data;
+
+ private transient ByteArrayOutputStream outputStream;
+
+ public FilePastContent(Id id) {
+ super();
+ this.id = id;
+ this.data=new byte[0];
+ }
+
+ public FilePastContent(InputBuffer buf, Endpoint endpoint) throws IOException {
+ this.id=endpoint.readId(buf, buf.readShort());
+ this.data=new byte[buf.readInt()];
+ buf.read(this.data);
+ }
+
+ public PastContent checkInsert(Id id, PastContent existingContent)
+ throws PastException {
+ System.out.println("Storing file "+id.toString());
+ return this;
+ }
+
+ public PastContentHandle getHandle(final Past local) {
+ final NodeHandle handle=local.getLocalNodeHandle();
+ return new PastContentHandle() {
+ public Id getId() {
+ return FilePastContent.this.getId();
+ }
+ public NodeHandle getNodeHandle() {
+ return handle;
+ }
+ };
+ }
+
+ public short getType() {
+ return PastDeserializer.TYPE_FILE;
+ }
+
+ public Id getId() {
+ return id;
+ }
+
+ public boolean isMutable() {
+ return true;
+ }
+ public long getContentSize(){
+ return data.length;
+ }
+
+ public InputStream getInputStream(){
+ return new ByteArrayInputStream(data);
+ }
+
+ public OutputStream getOutputStream(boolean append) throws IOException {
+ if(outputStream!=null)
+ throw new IOException("Tring to open a second outputstream to the same file.");
+
+ outputStream=new ByteArrayOutputStream();
+ if(append)
+ outputStream.write(data);
+
+ return outputStream;
+ }
+
+ public void closeStream() {
+ if(outputStream!=null)
+ data=outputStream.toByteArray();
+ outputStream=null;
+ }
+
+ public void serialize(OutputBuffer buf) throws IOException {
+ buf.writeShort(id.getType());
+ id.serialize(buf);
+ buf.writeInt(data.length);
+ buf.write(data, 0, data.length);
+ }
+}
Added: trunk/P2PlayBoot/src/org/p2play/boot/vfs/FolderPastContent.java
===================================================================
--- trunk/P2PlayBoot/src/org/p2play/boot/vfs/FolderPastContent.java (rev 0)
+++ trunk/P2PlayBoot/src/org/p2play/boot/vfs/FolderPastContent.java 2007-01-22 12:50:57 UTC (rev 28)
@@ -0,0 +1,89 @@
+package org.p2play.boot.vfs;
+
+import java.io.IOException;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Iterator;
+
+import rice.p2p.commonapi.Endpoint;
+import rice.p2p.commonapi.Id;
+import rice.p2p.commonapi.NodeHandle;
+import rice.p2p.commonapi.rawserialization.InputBuffer;
+import rice.p2p.commonapi.rawserialization.OutputBuffer;
+import rice.p2p.past.Past;
+import rice.p2p.past.PastContent;
+import rice.p2p.past.PastContentHandle;
+import rice.p2p.past.PastException;
+import rice.p2p.past.rawserialization.RawPastContent;
+
+public class FolderPastContent implements RawPastContent {
+
+ private Id id;
+ private Collection<String> children;
+
+ public FolderPastContent(Id id) {
+ super();
+ this.id = id;
+ children=new HashSet<String>();
+ }
+
+ public FolderPastContent(InputBuffer buf, Endpoint endpoint) throws IOException {
+ this(endpoint.readId(buf, buf.readShort()));
+ int size=buf.readInt();
+ for (int i = 0; i < size; i++) {
+ children.add(buf.readUTF());
+ }
+ }
+
+ public PastContent checkInsert(Id id, PastContent existingContent)
+ throws PastException {
+ System.out.println("Storing folder "+id.toString());
+ return this;
+ }
+
+ public PastContentHandle getHandle(Past local) {
+ final NodeHandle handle=local.getLocalNodeHandle();
+ return new PastContentHandle() {
+ public Id getId() {
+ return FolderPastContent.this.getId();
+ }
+ public NodeHandle getNodeHandle() {
+ return handle;
+ }
+ };
+ }
+
+ public Id getId() {
+ return id;
+ }
+
+ public boolean isMutable() {
+ return true;
+ }
+
+ public short getType() {
+ return PastDeserializer.TYPE_FOLDER;
+ }
+
+ public void serialize(OutputBuffer buf) throws IOException {
+ buf.writeShort(id.getType());
+ id.serialize(buf);
+ buf.writeInt(children.size());
+ Iterator<String> iterator=children.iterator();
+ while (iterator.hasNext()) {
+ buf.writeUTF(iterator.next());
+ }
+ }
+
+ public String[] getChildren() {
+ return children.toArray(new String[children.size()]);
+ }
+
+ public void add(String pathDecoded) {
+ children.add(pathDecoded);
+ }
+
+ public void remove(String pathDecoded) {
+ children.remove(pathDecoded);
+ }
+}
Added: trunk/P2PlayBoot/src/org/p2play/boot/vfs/ImagineryPastContent.java
===================================================================
--- trunk/P2PlayBoot/src/org/p2play/boot/vfs/ImagineryPastContent.java (rev 0)
+++ trunk/P2PlayBoot/src/org/p2play/boot/vfs/ImagineryPastContent.java 2007-01-22 12:50:57 UTC (rev 28)
@@ -0,0 +1,49 @@
+package org.p2play.boot.vfs;
+
+import rice.p2p.commonapi.Id;
+import rice.p2p.commonapi.NodeHandle;
+import rice.p2p.past.Past;
+import rice.p2p.past.PastContent;
+import rice.p2p.past.PastContentHandle;
+import rice.p2p.past.PastException;
+
+/**
+ * This class represents a deeted file or folder
+ *
+ * @author markus
+ *
+ */
+public class ImagineryPastContent implements PastContent {
+ private Id id;
+
+ public ImagineryPastContent(Id id) {
+ super();
+ this.id = id;
+ }
+
+ public PastContent checkInsert(Id id, PastContent existingContent)
+ throws PastException {
+ System.out.println("Deleting "+id.toString());
+ return this;
+ }
+
+ public PastContentHandle getHandle(final Past local) {
+ final NodeHandle handle=local.getLocalNodeHandle();
+ return new PastContentHandle() {
+ public Id getId() {
+ return ImagineryPastContent.this.getId();
+ }
+ public NodeHandle getNodeHandle() {
+ return handle;
+ }
+ };
+ }
+
+ public Id getId() {
+ return id;
+ }
+
+ public boolean isMutable() {
+ return true;
+ }
+}
Added: trunk/P2PlayBoot/src/org/p2play/boot/vfs/PastDeserializer.java
===================================================================
--- trunk/P2PlayBoot/src/org/p2play/boot/vfs/PastDeserializer.java (rev 0)
+++ trunk/P2PlayBoot/src/org/p2play/boot/vfs/PastDeserializer.java 2007-01-22 12:50:57 UTC (rev 28)
@@ -0,0 +1,38 @@
+package org.p2play.boot.vfs;
+
+import java.io.IOException;
+
+import rice.p2p.commonapi.Endpoint;
+import rice.p2p.commonapi.rawserialization.InputBuffer;
+import rice.p2p.past.PastContent;
+import rice.p2p.past.rawserialization.JavaPastContentDeserializer;
+import rice.p2p.past.rawserialization.PastContentDeserializer;
+
+public class PastDeserializer implements PastContentDeserializer {
+
+ public static final short TYPE_IMAGINERY = 1;
+
+ public static final short TYPE_FOLDER = 2;
+
+ public static final short TYPE_FILE = 3;
+
+ private JavaPastContentDeserializer deserializer;
+
+ public PastDeserializer() {
+ deserializer=new JavaPastContentDeserializer();
+ }
+
+ public PastContent deserializePastContent(InputBuffer buf,
+ Endpoint endpoint, short contentType) throws IOException {
+ switch (contentType) {
+ case TYPE_FILE:
+ return new FilePastContent(buf,endpoint);
+ case TYPE_FOLDER:
+ return new FolderPastContent(buf,endpoint);
+ default:
+ return deserializer.deserializePastContent(buf, endpoint,
+ contentType);
+ }
+ }
+
+}
Added: trunk/P2PlayBoot/src/org/p2play/boot/vfs/PastryFileObject.java
===================================================================
--- trunk/P2PlayBoot/src/org/p2play/boot/vfs/PastryFileObject.java (rev 0)
+++ trunk/P2PlayBoot/src/org/p2play/boot/vfs/PastryFileObject.java 2007-01-22 12:50:57 UTC (rev 28)
@@ -0,0 +1,176 @@
+package org.p2play.boot.vfs;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Arrays;
+
+import org.apache.commons.vfs.FileName;
+import org.apache.commons.vfs.FileSystemException;
+import org.apache.commons.vfs.FileType;
+import org.apache.commons.vfs.provider.AbstractFileObject;
+import org.apache.commons.vfs.provider.AbstractFileSystem;
+
+import rice.Continuation;
+import rice.p2p.commonapi.Id;
+import rice.p2p.commonapi.Node;
+import rice.p2p.past.Past;
+import rice.p2p.past.PastContent;
+
+public class PastryFileObject extends AbstractFileObject {
+ private Past past;
+
+ private Id id;
+
+ private Exception exception;
+
+ private PastContent content;
+
+ private Object mutex = new Object();
+
+ /**
+ * are we currently in the process of refreshing our state?
+ */
+ private boolean refreshing;
+
+ public PastryFileObject(Node node, Past past, FileName name,
+ AbstractFileSystem fs) throws FileSystemException {
+ super(name, fs);
+ this.past = past;
+ this.id = node.getIdFactory().buildId(name.getPathDecoded());
+ this.refreshing = true;
+ }
+
+ @Override
+ protected void doAttach() throws Exception {
+ this.past.lookup(this.id, new Continuation() {
+ public void receiveException(Exception result) {
+ result.printStackTrace();
+
+ exception = result;
+
+ synchronized (mutex) {
+ refreshing = false;
+ mutex.notifyAll();
+ }
+ }
+
+ public void receiveResult(Object result) {
+ content = (PastContent) result;
+ synchronized (mutex) {
+ refreshing = false;
+ mutex.notifyAll();
+ }
+ }
+ });
+
+ waitForRefreshFinished();
+ }
+
+ @Override
+ protected void doCreateFolder() throws Exception {
+ FolderPastContent folderPastContent = new FolderPastContent(id);
+
+ updateData(folderPastContent);
+
+ refresh();
+ }
+
+ private void updateData(final PastContent pastContent) throws Exception {
+ //close stream of files, so that they can be transmitted
+ if(pastContent instanceof FilePastContent)
+ ((FilePastContent)pastContent).closeStream();
+
+ past.insert(pastContent, new Continuation() {
+
+ public void receiveException(Exception result) {
+ result.printStackTrace();
+
+ exception = result;
+
+ synchronized (mutex) {
+ refreshing = false;
+ mutex.notifyAll();
+ }
+ }
+
+ public void receiveResult(Object result) {
+ System.out.println("Update of "+pastContent.getId()+": "+Arrays.toString((Object[])result));
+ synchronized (mutex) {
+ refreshing = false;
+ mutex.notifyAll();
+ }
+ }
+ });
+
+ waitForRefreshFinished();
+ }
+
+ @Override
+ protected long doGetContentSize() throws Exception {
+ return ((FilePastContent) content).getContentSize();
+ }
+
+ private void waitForRefreshFinished() throws Exception {
+ if (refreshing) {
+ synchronized (mutex) {
+ mutex.wait();
+ }
+ }
+
+ if (exception != null)
+ throw exception;
+ }
+
+ @Override
+ protected InputStream doGetInputStream() throws Exception {
+ return ((FilePastContent) content).getInputStream();
+ }
+
+ @Override
+ protected OutputStream doGetOutputStream(boolean bAppend) throws Exception {
+ if(content==null||content instanceof ImagineryPastContent)
+ content=new FilePastContent(id);
+ return ((FilePastContent)content).getOutputStream(bAppend);
+ }
+
+ @Override
+ protected FileType doGetType() throws Exception {
+ if (content == null)
+ return FileType.IMAGINARY;
+ else if (content instanceof ImagineryPastContent)
+ return FileType.IMAGINARY;
+ else if (content instanceof FilePastContent)
+ return FileType.FILE;
+ else if (content instanceof FolderPastContent)
+ return FileType.FOLDER;
+
+ return FileType.IMAGINARY;
+ }
+
+ @Override
+ protected void doDelete() throws Exception {
+ content=new ImagineryPastContent(id);
+ updateData(content);
+ }
+
+ @Override
+ protected String[] doListChildren() throws Exception {
+ return ((FolderPastContent)content).getChildren();
+ }
+
+ @Override
+ protected void onChildrenChanged(FileName child, FileType newType) throws Exception {
+ if(!isAttached())
+ this.doAttach();
+ if(newType==FileType.IMAGINARY)
+ ((FolderPastContent)content).remove(child.getPathDecoded());
+ else
+ ((FolderPastContent)content).add(child.getPathDecoded());
+ updateData(content);
+ }
+
+ @Override
+ protected void onChange() throws Exception {
+ updateData(content);
+ }
+}
Added: trunk/P2PlayBoot/src/org/p2play/boot/vfs/PastryFileProvider.java
===================================================================
--- trunk/P2PlayBoot/src/org/p2play/boot/vfs/PastryFileProvider.java (rev 0)
+++ trunk/P2PlayBoot/src/org/p2play/boot/vfs/PastryFileProvider.java 2007-01-22 12:50:57 UTC (rev 28)
@@ -0,0 +1,48 @@
+package org.p2play.boot.vfs;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+
+import org.apache.commons.vfs.Capability;
+import org.apache.commons.vfs.FileName;
+import org.apache.commons.vfs.FileSystem;
+import org.apache.commons.vfs.FileSystemException;
+import org.apache.commons.vfs.FileSystemOptions;
+import org.apache.commons.vfs.provider.AbstractOriginatingFileProvider;
+import org.p2play.net.pastry.NodeHandler;
+
+public class PastryFileProvider extends AbstractOriginatingFileProvider {
+
+ private NodeHandler nodeHandler;
+
+ final static Collection capabilities = Collections
+ .unmodifiableCollection(Arrays
+ .asList(new Capability[] {
+ Capability.CREATE,
+ Capability.DELETE,
+ Capability.GET_TYPE,
+ Capability.LIST_CHILDREN,
+ Capability.READ_CONTENT,
+ Capability.WRITE_CONTENT,
+ Capability.GET_LAST_MODIFIED,
+ Capability.URI,
+ Capability.COMPRESS,
+ Capability.SIGNING,
+ }));
+
+ public PastryFileProvider(NodeHandler nodeHandler) {
+ this.nodeHandler=nodeHandler;
+ }
+
+ @Override
+ protected FileSystem doCreateFileSystem(FileName rootName,
+ FileSystemOptions fileSystemOptions) throws FileSystemException {
+ return new PastryFileSystem(this, nodeHandler, rootName, fileSystemOptions);
+ }
+
+ public Collection getCapabilities() {
+ return capabilities;
+ }
+
+}
Added: trunk/P2PlayBoot/src/org/p2play/boot/vfs/PastryFileSystem.java
===================================================================
--- trunk/P2PlayBoot/src/org/p2play/boot/vfs/PastryFileSystem.java (rev 0)
+++ trunk/P2PlayBoot/src/org/p2play/boot/vfs/PastryFileSystem.java 2007-01-22 12:50:57 UTC (rev 28)
@@ -0,0 +1,43 @@
+package org.p2play.boot.vfs;
+
+import java.util.Collection;
+
+import org.apache.commons.vfs.FileName;
+import org.apache.commons.vfs.FileObject;
+import org.apache.commons.vfs.FileSystemOptions;
+import org.apache.commons.vfs.provider.AbstractFileSystem;
+import org.p2play.net.pastry.NodeHandler;
+
+import rice.p2p.commonapi.Node;
+import rice.p2p.past.Past;
+
+public class PastryFileSystem extends AbstractFileSystem {
+
+ private PastryFileProvider pastryFileProvider;
+
+ private Node node;
+
+ private Past past;
+
+ public PastryFileSystem(PastryFileProvider pastryFileProvider, NodeHandler nodeHandler,
+ FileName rootName, FileSystemOptions fileSystemOptions) {
+ super(rootName, null, fileSystemOptions);
+ this.pastryFileProvider = pastryFileProvider;
+ this.node = nodeHandler.getNode();
+ this.past = nodeHandler.getPast("p2play.vfs");
+ this.past.setContentDeserializer(new PastDeserializer());
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ protected void addCapabilities(Collection caps) {
+ caps.addAll(pastryFileProvider.getCapabilities());
+ }
+
+ @Override
+ protected FileObject createFile(FileName name) throws Exception {
+ // TODO Auto-generated method stub
+ return new PastryFileObject(node, past, name, this);
+ }
+
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|