package doss.nfs;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.dcache.nfs.ExportFile;
import org.dcache.nfs.v3.MountServer;
import org.dcache.nfs.v3.NfsServerV3;
import org.dcache.nfs.v4.MDSOperationFactory;
import org.dcache.nfs.v4.NFSServerV41;
import org.dcache.nfs.v4.NfsIdMapping;
import org.dcache.nfs.v4.SimpleIdMap;
import org.dcache.nfs.vfs.VirtualFileSystem;
import org.dcache.xdr.OncRpcException;
import org.dcache.xdr.OncRpcProgram;
import org.dcache.xdr.OncRpcSvc;
import org.dcache.xdr.OncRpcSvcBuilder;
import org.dcache.xdr.RpcDispatchable;
import org.dcache.xdr.portmap.OncRpcEmbeddedPortmap;

public class Main {
	public static void main(String[] args) throws IOException, OncRpcException, InterruptedException {
		URL exports = Main.class.getClassLoader().getResource(
				"doss/nfs/exports");
		if (exports == null) {
			throw new RuntimeException(
					"doss/nfs/exports not found on classpath");
		}
		VirtualFileSystem fs = new BlobStoreVFS();
		ExportFile exportFile = new ExportFile(exports);
		
		// NFS 3
		NfsServerV3 nfs3 = new NfsServerV3(exportFile, fs);
		
		// NFS 4
		MDSOperationFactory opfac = new MDSOperationFactory();
		NfsIdMapping idMap = new SimpleIdMap();
		
		NFSServerV41 nfs4 = new NFSServerV41(opfac, null, fs, idMap, exportFile);
		
		MountServer mountd = new MountServer(exportFile, fs);

		OncRpcSvc rpcSvc = new OncRpcSvcBuilder().withPort(1234).withTCP()
				.build();
		Map<OncRpcProgram, RpcDispatchable> services = new HashMap<>();
		
		rpcSvc.register(new OncRpcProgram(100003, 3), nfs3);
		rpcSvc.register(new OncRpcProgram(100003, 4), nfs4);
		rpcSvc.register(new OncRpcProgram(100005, 3), mountd);
		
		rpcSvc.setPrograms(services );
		rpcSvc.start();
		Thread.sleep(100000);
	}
}

@@ -789,16 +789,114 @@
 ption %7B%0A
+%09%09// virtual file system%0A%09%09VirtualFileSystem fs = new BlobStoreVFS();%0A%09%09%0A%09%09// exports config file%0A
 %09%09URL ex
@@ -900,16 +900,19 @@
  exports
+Url
  = Main.
@@ -946,21 +946,16 @@
 esource(
-%0A%09%09%09%09
 %22doss/nf
@@ -980,16 +980,19 @@
 (exports
+Url
  == null
@@ -1029,14 +1029,8 @@
 ion(
-%0A%09%09%09%09%09
 %22dos
@@ -1077,53 +1077,8 @@
 %09%09%7D%0A
-%09%09VirtualFileSystem fs = new BlobStoreVFS();%0A
 %09%09Ex
@@ -1092,20 +1092,17 @@
 e export
-File
+s
  = new E
@@ -1118,16 +1118,19 @@
 (exports
+Url
 );%0A%09%09%0A%09%09
@@ -1140,77 +1140,16 @@
 NFS 
-3%0A%09%09NfsServerV3 nfs3 = new NfsServerV3(exportFile, fs);%0A%09%09%0A%09%09// NFS 4
+4 server
 %0A%09%09M
@@ -1244,19 +1244,16 @@
 dMap();%0A
-%09%09%0A
 %09%09NFSSer
@@ -1317,18 +1317,27 @@
 port
-File
+s
 );%0A%09%09%0A
+%09%09// mountd%0A
 %09%09Mo
@@ -1381,20 +1381,17 @@
 port
-File
+s
 , fs);%0A%0A
 %09%09On
@@ -1386,16 +1386,32 @@
 , fs);%0A%0A
+%09%09// RPC server%0A
 %09%09OncRpc
@@ -1474,13 +1474,8 @@
 CP()
-%0A%09%09%09%09
 .bui
@@ -1550,66 +1550,8 @@
 ();%0A
-%09%09%0A%09%09rpcSvc.register(new OncRpcProgram(100003, 3), nfs3);%0A
 %09%09rp
@@ -1658,19 +1658,16 @@
 ountd);%0A
-%09%09%0A
 %09%09rpcSvc
@@ -1715,29 +1715,107 @@
 ;%0A%09%09
-Thread.sleep(100000);
+%0A%09%09try %7B%0A%09%09%09// hang around for a while%0A%09%09%09Thread.sleep(100000);%0A%09%09%7D finally %7B%0A%09%09%09rpcSvc.stop();%0A%09%09%7D
 %0A%09%7D%0A

Remove NFS3 server as it's a bit more fiddly