add drill03 exercise 2 & 3
This commit is contained in:
parent
a1bc8b5fde
commit
d450f2096e
18
src/main/kotlin/drills/drill03/exercise2/Agent.kt
Normal file
18
src/main/kotlin/drills/drill03/exercise2/Agent.kt
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
16
src/main/kotlin/drills/drill03/exercise2/AgentContainer.kt
Normal file
16
src/main/kotlin/drills/drill03/exercise2/AgentContainer.kt
Normal 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()
|
||||||
|
}
|
||||||
|
}
|
26
src/main/kotlin/drills/drill03/exercise2/ContainerServer.kt
Normal file
26
src/main/kotlin/drills/drill03/exercise2/ContainerServer.kt
Normal 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")
|
||||||
|
}
|
||||||
|
}
|
36
src/main/kotlin/drills/drill03/exercise2/Node.kt
Normal file
36
src/main/kotlin/drills/drill03/exercise2/Node.kt
Normal 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
|
||||||
|
}
|
||||||
|
}
|
16
src/main/kotlin/drills/drill03/exercise2/PrintAgent.kt
Normal file
16
src/main/kotlin/drills/drill03/exercise2/PrintAgent.kt
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
7
src/main/kotlin/drills/drill03/exercise2/PrintApp.kt
Normal file
7
src/main/kotlin/drills/drill03/exercise2/PrintApp.kt
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package drills.drill03.exercise2
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
val nodes = Array<Node>(3) { NodeImpl(it) }
|
||||||
|
|
||||||
|
PrintAgent(nodes).start()
|
||||||
|
}
|
7
src/main/kotlin/drills/drill03/exercise3/AddTask.kt
Normal file
7
src/main/kotlin/drills/drill03/exercise3/AddTask.kt
Normal 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
|
||||||
|
}
|
26
src/main/kotlin/drills/drill03/exercise3/Client.kt
Normal file
26
src/main/kotlin/drills/drill03/exercise3/Client.kt
Normal 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)
|
9
src/main/kotlin/drills/drill03/exercise3/FactTask.kt
Normal file
9
src/main/kotlin/drills/drill03/exercise3/FactTask.kt
Normal 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)
|
||||||
|
}
|
15
src/main/kotlin/drills/drill03/exercise3/Rev.kt
Normal file
15
src/main/kotlin/drills/drill03/exercise3/Rev.kt
Normal 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()
|
||||||
|
}
|
11
src/main/kotlin/drills/drill03/exercise3/Server.kt
Normal file
11
src/main/kotlin/drills/drill03/exercise3/Server.kt
Normal 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()
|
||||||
|
}
|
7
src/main/kotlin/drills/drill03/exercise3/Task.kt
Normal file
7
src/main/kotlin/drills/drill03/exercise3/Task.kt
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package drills.drill03.exercise3
|
||||||
|
|
||||||
|
import java.io.Serializable
|
||||||
|
|
||||||
|
interface Task : Serializable {
|
||||||
|
fun execute(): Serializable
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user