Skip to content

Sharing

Raphael Matile edited this page Mar 17, 2016 · 2 revisions

To share files and directories among different users participating in the same network, Sync provides a SharingSyncer which informs the sharer as well as the own nodes about a new access gain.

Notes:

  • Before sharing is available, the node must be fully connected
  • Sharing with the own user is prohibited
  • At least one node of the sharer must be connected too, to inform him about the access gain / revocation

Share

Files

To share a file with another user in the network, a ShareEvent must be synchronised:

import org.rmatil.sync.core.api.IShareEvent;
import org.rmatil.sync.core.api.ISharingSyncer;
import org.rmatil.sync.core.syncer.sharing.event.ShareEvent;
import org.rmatil.sync.core.syncer.sharing.event.UnshareEvent;
import org.rmatil.sync.version.api.AccessType;

import java.nio.file.Paths;


// ...
// Connect resp. bootstrap the node before
// The synchronised folder is located at path/to/synchronised/folder
// ...


  // share the file lying at relative/path/to/shared/file within
  // the synchronised directory. The absolute path is also
  // path/to/synchronised/folder/relative/path/to/shared/file
  IShareEvent shareEvent = new ShareEvent(
    Paths.get("relative/path/to/shared/file"),
    AccessType.WRITE,
    "Jason Response"
  );

  // get manager to share files
  ISharingSyncer sharingSyncer = sync.getSharingSyncer();
  // contact all nodes of Piff Jenkins, allowing them to
  // update their ObjectStore,
  // then notify one peer of Jason Response
  sharingSyncer.sync(shareEvent);


  // unshare another file previously shared
  IShareEvent unshareEvent = new UnshareEvent(
    Paths.get("previously/shared/file"),
    AccessType.READ,
    "Jason Response"
  );
  // contact all nodes of Piff Jenkins,
  // then notify one client of Jason Response
  sharingSyncer.sync(unshareEvent);

Directories

To share directories, a bit more work must be done:

import org.rmatil.sync.core.api.ISharingSyncer;
import org.rmatil.sync.core.syncer.sharing.event.ShareEvent;
import org.rmatil.sync.persistence.api.IPathElement;
import org.rmatil.sync.persistence.core.tree.ITreeStorageAdapter;
import org.rmatil.sync.persistence.core.tree.TreePathElement;
import org.rmatil.sync.persistence.core.tree.local.LocalStorageAdapter;
import org.rmatil.sync.version.api.AccessType;

import java.nio.file.Paths;
import java.util.List;


// ...
// Connect resp. bootstrap the node before
// The synchronised folder is located at path/to/synchronised/folder
// ...



  ITreeStorageAdapter storageAdapter = new LocalStorageAdapter(Paths.get("path/to/synchronised/folder"));
  TreePathElement elementToShare = new TreePathElement("path/to/directory");

  if (! storageAdapter.isDir(elementToShare)) {
    System.err.println("Path should be a directory");
    return;
  }

  // get manager to share files
  ISharingSyncer sharingSyncer = sync.getSharingSyncer();

  // get all children of the directory
  List<TreePathElement> children = storageAdapter.getDirectoryContents(elementToShare);
  for (IPathElement child : children) {
    // share each child
    sharingSyncer.sync(
      new ShareEvent(
        Paths.get(child.getPath()),
        AccessType.WRITE,
        "Jason Response"
      )
    );
  }

Unshare

Files

Unsharing files works similar as sharing them: Instead of creating a ShareEvent, an UnshareEvent must be synchronised:

import org.rmatil.sync.core.api.IShareEvent;
import org.rmatil.sync.core.api.ISharingSyncer;
import org.rmatil.sync.core.syncer.sharing.event.ShareEvent;
import org.rmatil.sync.core.syncer.sharing.event.UnshareEvent;
import org.rmatil.sync.version.api.AccessType;

import java.nio.file.Paths;


// ...
// Connect resp. bootstrap the node before
// The synchronised folder is located at path/to/synchronised/folder
// ...



  // unshare a previously shared file
  IShareEvent unshareEvent = new UnshareEvent(
    Paths.get("previously/shared/file"),
    AccessType.READ,
    "Jason Response"
  );
  // contact all nodes of Piff Jenkins,
  // then notify one client of Jason Response
  sharingSyncer.sync(unshareEvent);

Directories

Again, to unshare directories, a bit more work must be done:

import org.rmatil.sync.core.api.ISharingSyncer;
import org.rmatil.sync.core.syncer.sharing.event.UnshareEvent;
import org.rmatil.sync.persistence.api.IPathElement;
import org.rmatil.sync.persistence.core.tree.ITreeStorageAdapter;
import org.rmatil.sync.persistence.core.tree.TreePathElement;
import org.rmatil.sync.persistence.core.tree.local.LocalStorageAdapter;
import org.rmatil.sync.version.api.AccessType;

import java.nio.file.Paths;
import java.util.List;


// ...
// Connect resp. bootstrap the node before
// The synchronised folder is located at path/to/synchronised/folder
// ...



  ITreeStorageAdapter storageAdapter = new LocalStorageAdapter(Paths.get("path/to/synchronised/folder"));
  TreePathElement elementToShare = new TreePathElement("path/to/directory");

  if (! storageAdapter.isDir(elementToShare)) {
    System.err.println("Path should be a directory");
    return;
  }

  // get manager to unshare files
  ISharingSyncer sharingSyncer = sync.getSharingSyncer();

  // get all children of the directory
  List<TreePathElement> children = storageAdapter.getDirectoryContents(elementToShare);
  for (IPathElement child : children) {
    // unshare each child
    sharingSyncer.sync(
      new UnshareEvent(
        Paths.get(child.getPath()),
        AccessType.WRITE,
        "Jason Response"
      )
    );
  }