Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added configurable Client and Server Port and IP #243

Merged
merged 5 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions portals-distributed/src/main/scala/portals/distributed/Client.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ object Client extends App:
//////////////////////////////////////////////////////////////////////////////

/** Post the Launch `event` to the server. */
def postToServer(event: Launch): Unit =
def postToServer(event: Launch, ip: String, port: Int): Unit =
val bytes = write(event).getBytes()
val response = requests.post("http://localhost:8080/launch", data = bytes)
val response = requests.post("http://" + ip + ":" + port.toString + "/launch", data = bytes)
response match
case r if r.statusCode == 200 => println("success")
case r => println(s"error: ${r.statusCode}")

/** Post the SubmitClassFiles `event` to the server. */
def postToServer(event: SubmitClassFiles): Unit =
def postToServer(event: SubmitClassFiles, ip: String, port: Int): Unit =
val bytes = write(event).getBytes()
val response = requests.post("http://localhost:8080/submitClassFiles", data = bytes)
val response = requests.post("http://" + ip + ":" + port.toString + "/submitClassFiles", data = bytes)
response match
case r if r.statusCode == 200 => println("success")
case r => println(s"error: ${r.statusCode}")
Expand All @@ -41,13 +41,13 @@ object Client extends App:
//////////////////////////////////////////////////////////////////////////////

/** Submit a classfile at `path` within `directory` to the server. */
def submitClassFile(path: String, directory: String): Unit =
def submitClassFile(path: String, directory: String, ip: String = "localhost", port: Int = 8080): Unit =
val bytes = Util.getBytes(path, directory)
val event = SubmitClassFiles(Seq(ClassFileInfo(path, bytes)))
postToServer(event)
postToServer(event, ip, port)

/** Submit all classfiles within a `directory` to the server. */
def submitClassFilesFromDir(directory: String): Unit =
def submitClassFilesFromDir(directory: String, ip: String = "localhost", port: Int = 8080): Unit =
val dir = Paths.get(directory)
val files = Files
.walk(dir)
Expand All @@ -62,12 +62,12 @@ object Client extends App:
.asScala
.toSeq
val event = SubmitClassFiles(cfs)
postToServer(event)
postToServer(event, ip, port)

/** Launch an `application` specified by its Java path to the server. */
def launch(application: String): Unit =
def launch(application: String, ip: String = "localhost", port: Int = 8080): Unit =
val event = Launch(application)
postToServer(event)
postToServer(event, ip, port)

//////////////////////////////////////////////////////////////////////////////
// OBJECT API
Expand Down Expand Up @@ -114,6 +114,6 @@ object Client extends App:
this.submitClassFilesFromDir(classFilesDirectory)

/** Launch an `app` to the server. */
def launchObject(app: AnyRef): Unit =
def launchObject(app: AnyRef, ip: String = "localhost", port: Int = 8080): Unit =
val name = ApplicationLoader.getClassName(app)
launch(name)
launch(name, ip, port)
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import mainargs.ParserForMethods

/** Command line interface for the client.
*
* Note: to run it from SBT use (don't forget to end with a "):
* ```
* sbt "distributed/runMain portals.distributed.ClientCLI ..."
* ```
* @example
* Display the help message to see all available commands:
* {{{
* sbt "distributed/runMain portals.distributed.ClientCLI --help"
* }}}
*/
object ClientCLI:

Expand All @@ -22,39 +23,88 @@ object ClientCLI:
* {{{
* ClientCLI submit --path portals/distributed/examples/HelloWorld$.class --directory portals-distributed/target/scala-3.3.0/classes
* }}}
*
* @example
* {{{
* ClientCLI submit --path portals/distributed/examples/HelloWorld$.class --ip localhost --port 8080
* }}}
*
* @example
* {{{
* ClientCLI submit --path portals/distributed/examples/HelloWorld$.class --directory portals-distributed/target/scala-3.3.0/classes --ip localhost --port 8080
* }}}
*
* @param path
* the path to the classfile to submit
* @param directory
* (optional) the directory containing the classfile to submit
* @param ip
* (optional) the IP address of the server
* @param port
* (optional) the port of the server
*/
@mainargs.main
def submit(
path: String,
directory: String = "."
directory: String = ".",
ip: String = "localhost",
port: Int = 8080
): Unit =
Client.submitClassFile(path, directory)
Client.submitClassFile(path, directory, ip, port)

/** Submit all classfiles within a `directory` to the server.
*
* @example
* {{{
* ClientCLI submitDir --directory portals-distributed/target/scala-3.3.0/classes
* }}}
*
* @example
* {{{
* ClientCLI submitDir --directory portals-distributed/target/scala-3.3.0/classes --ip localhost --port 8080
* }}}
*
* @param directory
* the directory containing the classfile to submit
* @param ip
* (optional) the IP address of the server
* @param port
* (optional) the port of the server
*/
@mainargs.main
def submitDir(
directory: String
directory: String,
ip: String = "localhost",
port: Int = 8080
): Unit =
Client.submitClassFilesFromDir(directory)
Client.submitClassFilesFromDir(directory, ip, port)

/** Launch an application on the server.
*
* @example
* {{{
* ClientCLI launch --application portals.distributed.examples.HelloWorld$
* }}}
*
* @example
* {{{
* ClientCLI launch --application portals.distributed.examples.HelloWorld$ --ip localhost --port 8080
* }}}
*
* @param application
* the Java path to the application to launch
* @param ip
* (optional) the IP address of the server
* @param port
* (optional) the port of the server
*/
@mainargs.main
def launch(
application: String,
ip: String = "localhost",
port: Int = 8080
): Unit =
Client.launch(application)
Client.launch(application, ip, port)

// using the mainargs library
def main(args: Array[String]): Unit =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,40 @@
package portals.distributed

import cask.main.Main
import io.undertow.Undertow

/** An alternative server that can run with `sbt run` without stopping. */
object SBTRunServer extends cask.Main:
// define the routes which this server handles
val allRoutes = Seq(Server)
/** An alternative server that can run with `sbt run` without stopping.
*
* @example
* {{{
* sbt "distributed/runMain portals.distributed.SBTRunServer 8080"
* }}}
*/
object SBTRunServer:
object InternalSBTRunServer extends cask.Main:
// define the routes which this server handles
override val allRoutes = Seq(Server)

// execute the main method of this server
this.main(args = Array.empty)
// override the default main method to handle the port argument
override def main(args: Array[String]): Unit = {
println(args)
val port = if args.length > 0 then Some(args(0).toInt) else Some(8080)
if (!verbose) Main.silenceJboss()
val server = Undertow.builder
.addHttpListener(port.get, host)
.setHandler(defaultHandler)
.build
server.start()
}

// sleep so that we don't exit prematurely
Thread.sleep(Long.MaxValue)
// using main method here instead of extending App, see:
// https://users.scala-lang.org/t/args-is-null-while-extending-app-even-when-runtime-args-are-provided/8564
def main(args: Array[String]): Unit =
// execute the main method of the server, starting it
InternalSBTRunServer.main(args)

// exit before running this servers main method (again)
System.exit(0)
// sleep so we don't exit prematurely
Thread.sleep(Long.MaxValue)

// exit the application (not sure if necessary here)
System.exit(0)