source: launchers/browserbundle/src/main/scala/net/i2p/RouterLauncherApp.scala @ 8c10ec9

Last change on this file since 8c10ec9 was 8c10ec9, checked in by meeh <meeh@…>, 2 years ago

Realised a better way to handle the build process of both the
Browser Bundle i2p launcher, as well as the upcoming Mac OS X
i2p launcher. They share some few properties and how code has
to be managed for both system's update managers and so on.
More details will be documentated in README.md files as well as
in those commit messages I now write :)

  • Property mode set to 100644
File size: 3.4 KB
Line 
1package net.i2p
2
3import java.io.{File, InputStream}
4
5import net.i2p.router.Router
6import org.json4s._
7import org.json4s.native.JsonMethods._
8
9/**
10  *
11  * For java developers:
12  * A scala object is like an instance of a class.
13  * If you define a method inside an object, it's equals to
14  * java's static methods.
15  *
16  * Also, in scala, the body of a class/object is executed as it's
17  * constructor.
18  *
19  * Also noteworthy;
20  * val is immutable
21  * var is mutable
22  *
23  *
24  * @author Meeh
25  * @version 0.0.1
26  * @since 0.9.35
27  */
28object RouterLauncherApp extends App {
29
30  def toBytes(xs: Int*) = xs.map(_.toByte).toArray
31
32  def getInt(bytes: Array[Byte]): Int = (bytes(3) << 24) & 0xff000000 | (bytes(2) << 16) & 0x00ff0000 | (bytes(1) << 8) & 0x0000ff00 | (bytes(0) << 0) & 0x000000ff
33
34  /**
35    * Encodes bytes in "binary form" as mentioned in Native Messaging in the WebExtension API.
36    * @param length
37    * @return
38    */
39  def getBytes(length: Int): Array[Byte] = {
40    val bytes = new Array[Byte](4)
41    bytes(0) = (length & 0xFF).toByte
42    bytes(1) = ((length >> 8) & 0xFF).toByte
43    bytes(2) = ((length >> 16) & 0xFF).toByte
44    bytes(3) = ((length >> 24) & 0xFF).toByte
45    bytes
46  }
47
48  def readMessage(in: InputStream): String = {
49    val arr = new Array[Byte](4)
50    in.read(arr)
51    val bytes = new Array[Byte](getInt(arr))
52    in.read(bytes)
53    new String(bytes, "UTF-8")
54  }
55
56  def sendMessage(message: String): Unit = {
57    System.out.write(getBytes(message.length))
58    System.out.write(message.getBytes("UTF-8"))
59  }
60
61  // Executed at launch
62  val basePath = Option(System.getProperty("i2p.dir.base")).getOrElse(System.getenv("I2PBASE"))
63  val configPath = Option(System.getProperty("i2p.dir.config")).getOrElse(System.getenv("I2PCONFIG"))
64
65  println(s"basePath => ${basePath}\nconfigPath => ${configPath}")
66
67  object ErrorUtils {
68    def errorMessageInJson(message: String, solution: String) : JObject = JObject(
69      List(
70        ("error",
71          JObject(
72            ("message", JString(message)),
73            ("solution", JString(solution))
74          )
75        )
76      )
77    )
78
79    def printError(message: String, solution: String): Unit = {
80      println(compact(render( errorMessageInJson(message,solution) )))
81    }
82
83    def printErrorAndExit(message: String, solution: String, exitCode: Int = 1): Unit = {
84      printError(message, solution)
85      System.exit(exitCode)
86    }
87  }
88
89  // Path related error checking
90  if (basePath == null || basePath.isEmpty) ErrorUtils.printErrorAndExit("I2P Base path is missing", "set property i2p.dir.base or environment variable I2PBASE")
91  if (configPath  == null || configPath.isEmpty) ErrorUtils.printErrorAndExit("I2P Config path is missing", "set property i2p.dir.config or environment variable I2PCONFIG")
92  if (!new File(basePath).exists()) ErrorUtils.printErrorAndExit("I2P Base path don't exist", "Reinstall the Browser Bundle")
93  if (!new File(configPath).exists()) ErrorUtils.printErrorAndExit("I2P Config path don't exist", "Delete your config directory for the Browser Bundle")
94
95
96  val deployer = new DeployProfile(configPath,basePath)
97  deployer.verifyExistenceOfConfig()
98
99  // Required/mocked properties
100  System.setProperty("wrapper.version", "portable-1")
101  System.setProperty("i2p.dir.portableMode", "true")
102  System.setProperty("loggerFilenameOverride", "router-@.log")
103
104  //ErrorUtils.printError(s"Starting up with arguments ${(args mkString ", ")}",":)")
105
106  Router.main(args)
107}
108
109
Note: See TracBrowser for help on using the repository browser.