package util.rmi import java.net.MalformedURLException import java.rmi.AccessException import java.rmi.AlreadyBoundException import java.rmi.Naming import java.rmi.Remote import java.rmi.RemoteException import java.rmi.registry.LocateRegistry class Server(private val handler: () -> Unit) { fun start() = try { handler() } catch (e: AccessException) { System.err.println("Bind operation not permitted $e") } catch (e: RemoteException) { System.err.println("Registry could not be contacted $e") } catch (e: MalformedURLException) { System.err.println("Wrong URL for binding $e") } catch (e: AlreadyBoundException) { System.err.println("Object alreay bound to the registry $e") } catch (e: Exception) { System.err.println("Generic Error $e") } companion object { fun bind(remote: Remote, name: String, port: Int = 4242) { LocateRegistry.createRegistry(port) Naming.bind("rmi://localhost:$port/$name", remote) } } }