lm-sistemi-software-distrib.../src/main/kotlin/drills/drill03/exercise2/Node.kt

37 lines
1022 B
Kotlin

package drills.drill03.exercise2
import java.io.Serializable
import java.net.MalformedURLException
import java.rmi.Naming
import java.rmi.NotBoundException
import java.rmi.RemoteException
interface Node : Serializable {
fun migrate(agent: Agent)
}
class NodeImpl(index: Int) : Node {
private val container = getContainer(index)
override fun migrate(agent: Agent) {
container?.migrate(agent)
}
private fun getContainer(index: Int): AgentContainer? {
try {
return Naming
.lookup("rmi://localhost:4242/container-$index") as AgentContainer
} catch (e: NotBoundException) {
System.err.println("Request obect not bound $e")
} catch (e: MalformedURLException) {
System.err.println("Wrong URL $e")
} catch (e: RemoteException) {
System.err.println("Network or Server Error $e")
} catch (e: Exception) {
System.err.println("Generic Error $e")
}
return null
}
}