add drill03 exercise 2 & 3

This commit is contained in:
Raffaele Mignone 2020-03-25 20:13:14 +01:00
parent a1bc8b5fde
commit d450f2096e
Signed by: norangebit
GPG Key ID: F5255658CB220573
12 changed files with 194 additions and 0 deletions

View File

@ -0,0 +1,18 @@
package drills.drill03.exercise2
import java.io.Serializable
interface Agent : Serializable, Runnable {
fun start()
fun migrateTo(node: Node)
}
abstract class AbstractAgent : Agent {
override fun start() {
Thread(this).start()
}
override fun migrateTo(node: Node) {
node.migrate(this)
}
}

View File

@ -0,0 +1,16 @@
package drills.drill03.exercise2
import java.rmi.Remote
import java.rmi.RemoteException
import java.rmi.server.UnicastRemoteObject
interface AgentContainer : Remote {
@Throws(RemoteException::class)
fun migrate(agent: Agent)
}
class AgentContainerImpl : AgentContainer, UnicastRemoteObject() {
override fun migrate(agent: Agent) {
agent.start()
}
}

View File

@ -0,0 +1,26 @@
package drills.drill03.exercise2
import java.net.MalformedURLException
import java.rmi.AccessException
import java.rmi.AlreadyBoundException
import java.rmi.Naming
import java.rmi.RemoteException
import java.rmi.registry.LocateRegistry
fun main(args: Array<String>) {
try {
if (args[0] == "0")
LocateRegistry.createRegistry(4242)
val container: AgentContainer = AgentContainerImpl()
Naming.rebind("rmi://localhost:4242/container-${args[0]}", container)
} 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")
}
}

View File

@ -0,0 +1,36 @@
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
}
}

View File

@ -0,0 +1,16 @@
package drills.drill03.exercise2
class PrintAgent(private val nodes: Array<Node>): AbstractAgent(){
private var index = 0
private val names = mutableListOf<String>()
override fun run() {
if (index < nodes.size) {
println("Insert your name:")
val name = readLine() ?: "no name"
names.add(name)
migrateTo(nodes[index++])
} else
println(names)
}
}

View File

@ -0,0 +1,7 @@
package drills.drill03.exercise2
fun main() {
val nodes = Array<Node>(3) { NodeImpl(it) }
PrintAgent(nodes).start()
}

View File

@ -0,0 +1,7 @@
package drills.drill03.exercise3
import java.io.Serializable
class AddTask(private val a: Int, private val b: Int) : Task {
override fun execute(): Serializable = a + b
}

View File

@ -0,0 +1,26 @@
package drills.drill03.exercise3
import util.rmi.Client
fun main() {
Client(clientHandler).start()
}
val clientHandler = {
val rev = Client.lookup("rev") as Rev
println("Enter two numbers:")
val a = readLine()?.toInt() ?: 0
val b = readLine()?.toInt() ?: 0
val sum = rev.executeTask(AddTask(a, b))
println("The sum of $a and $b is $sum")
println("Enter the number of which you want to calculate the factorial:")
val n = readLine()?.toInt() ?: 0
val fact = rev.executeTask(FactTask(n))
println("The factorial of $n is $fact")
}
fun fact(n: Int): Long = if (n <= 1) 1 else n * fact(n - 1)

View File

@ -0,0 +1,9 @@
package drills.drill03.exercise3
import java.io.Serializable
class FactTask(private val n: Int) : Task {
override fun execute(): Serializable = fact(n)
private fun fact(n: Int): Long = if (n <= 1) 1 else n * fact(n - 1)
}

View File

@ -0,0 +1,15 @@
package drills.drill03.exercise3
import java.io.Serializable
import java.rmi.Remote
import java.rmi.RemoteException
import java.rmi.server.UnicastRemoteObject
interface Rev : Remote {
@Throws(RemoteException::class)
fun executeTask(task: Task): Serializable
}
class RevImpl : Rev, UnicastRemoteObject() {
override fun executeTask(task: Task): Serializable = task.execute()
}

View File

@ -0,0 +1,11 @@
package drills.drill03.exercise3
import util.rmi.Server
fun main() {
Server {
System.setSecurityManager(SecurityManager())
val rev: Rev = RevImpl()
Server.bind(rev, "rev")
}.start()
}

View File

@ -0,0 +1,7 @@
package drills.drill03.exercise3
import java.io.Serializable
interface Task : Serializable {
fun execute(): Serializable
}