Skip to content

Connect Peers to other Nodes

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

To connect a peer to other, already joined, nodes, a BootstrapLocation has to be provided in the ApplicationConfig, specifying the IP address and the port of the other device:

import org.rmatil.sync.core.Sync;
import org.rmatil.sync.core.init.ApplicationConfigFactory;
import org.rmatil.sync.core.model.ApplicationConfig;
import org.rmatil.sync.core.model.RemoteClientLocation;
import org.rmatil.sync.network.core.model.NodeLocation;
import org.rmatil.sync.persistence.core.tree.ITreeStorageAdapter;
import org.rmatil.sync.persistence.core.tree.local.LocalStorageAdapter;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;

// ...

  Path rootDir = Paths.get("path/to/synchronised/folder");
  ITreeStorageAdapter storageAdapter = new LocalStorageAdapter(rootDir);

  // initialise the synchronised folder:
  // - create folder for ObjectStore (usually .sync)
  // - create folders for shared files:
  //    - sharedWithOthers (read-write)
  //    - sharedWithOthers (read-only)
  Sync.init(storageAdapter);

  KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
  KeyPair keyPair = keyGen.genKeyPair();

  RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
  RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();


  ApplicationConfig appConfig = ApplicationConfigFactory.createDefaultApplicationConfig(
    "Piff Jenkins",
    "ThisIsSafeUseIt",
    "SaltAndPepperMakesTheMealBetter",
    publicKey,
    privateKey,
    new RemoteClientLocation(
      "192.168.1.1",
      4003
    ),
    ApplicationConfigFactory.getDefaultIgnorePatterns()
  );

  // create a new Sync instance pointing to the root of the
  // specified storage adapter, i.e. path/to/synchronised/folder
  Sync sync = new Sync(storageAdapter);

  // start the node and let it connect to 192.168.1.1:4003 as specified in the config,
  // reconcile state of the synchronised folder with the ObjectStore,
  // reconcile local state with other connected clients of the same user
  NodeLocation nodeLocation = sync.connect(appConfig);

Clone this wiki locally