source: launchers/browserbundle/src/main/scala/net/i2p/RouterLauncherApp.scala @ 6d0f80f

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

Fixed a load of issues related to my "sync" approach, and Mac OSX's "async" API.

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