Skip to content

Commit f45139d

Browse files
committed
Callback using a random port done :D
"-features" in build.sbt and scalacOptions Thread now is launched ans runs the callback RMI server
1 parent 138458d commit f45139d

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

build.sbt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ lazy val root = (project in file(".")).
1515
settings(
1616
// set the name of the project
1717
name := "Client",
18+
scalacOptions += "-feature",
1819
scalaSource in Compile := baseDirectory.value / "src",
1920

2021
mainClass in (Compile, run) := Some("client.Client")

src/client/Client.scala

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import java.util.Calendar
66
import scala.annotation.tailrec
77
import scala.language.postfixOps
88
import java.rmi.server.UnicastRemoteObject
9+
import scala.util.Random
910

1011
sealed abstract class State
1112
object State {
@@ -34,13 +35,8 @@ object Client {
3435
val ret = stub login(user,pass)
3536
if (ret) {
3637
println("Welcome back! :D")
37-
// val callback: ClientTrait = new ClientTraitImpl(user)
38-
// stub registerForCallback(user,callback)
39-
new Thread(new Runnable {
40-
def run() {
41-
ClientTraitImpl.main(user)
42-
}
43-
})
38+
val port = getRandomPort
39+
createCallback(user,port)
4440
stub justRegisterMe(user)
4541
(MainState,user)
4642
}
@@ -51,6 +47,20 @@ object Client {
5147
}
5248
}
5349

50+
def getRandomPort: Int = {
51+
val r = new Random
52+
r.nextInt(65535-6969) + 6969
53+
}
54+
55+
def createCallback(user: String, port: Int): Unit = {
56+
val cb = new Thread(new Runnable {
57+
def run() {
58+
ClientTraitImpl.main(user,port)
59+
}
60+
})
61+
cb.start
62+
}
63+
5464
def createUserCli(stub: server.ServerTrait): (State,String) = {
5565
val user = readLine("User: ")
5666
val pass = readLine("Password: ")

src/client/ClientTraitImpl.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package client
33
import java.rmi.server.UnicastRemoteObject
44
import java.rmi.registry.Registry
55
import java.rmi.registry.LocateRegistry
6+
import scala.language.postfixOps
67

78
class ClientTraitImpl extends ClientTrait {
89

@@ -12,11 +13,12 @@ class ClientTraitImpl extends ClientTrait {
1213
}
1314

1415
object ClientTraitImpl {
15-
def main(name: String): Unit = {
16+
def main(name: String, port: Int): Unit = {
1617
try {
1718
val cb: ClientTrait = new ClientTraitImpl
1819
val stub = UnicastRemoteObject.exportObject(cb,0).asInstanceOf[ClientTrait]
19-
val registry = LocateRegistry.createRegistry(1099)
20+
val port = getRandomPort
21+
val registry = LocateRegistry.createRegistry(port)
2022
registry.rebind(name, stub)
2123
} catch {
2224
case e: Exception => e printStackTrace

0 commit comments

Comments
 (0)