| 21 | import java.util.function.Function |
| 22 | |
| 23 | class RemoteServer { |
| 24 | var initFile = "init_remote.html" // can swap this to go 'headless' |
| 25 | val BOOT = "/boot" |
| 26 | val RESOURCE = "/resource-" |
| 27 | val MESSAGE = "message" |
| 28 | val LOG = "log" |
| 29 | val ERROR = "error" |
| 30 | |
| 31 | var s: NewNanoHTTPD |
| 32 | |
| 33 | var id = 0; |
| 34 | |
| 35 | var hostname: String |
| 36 | |
| 37 | var responseMap = mutableMapOf<String, (JSONObject) -> Boolean>(); |
| 38 | var errorRoot: Box? = null |
| 39 | |
| 40 | val rpc = RPC() |
| 41 | |
| 42 | init { |
| 43 | this.s = NewNanoHTTPD(8090) |
| 44 | |
| 45 | |
| 46 | s.addDocumentRoot(Main.app + "/modules/fieldbox/resources/") |
| 47 | s.addDocumentRoot(Main.app + "/modules/fielded/resources/") |
| 48 | s.addDocumentRoot(Main.app + "/modules/fieldcore/resources/") |
| 49 | s.addDocumentRoot(Main.app + "/lib/web/") |
| 50 | s.addDocumentRoot(Main.app + "/win/lib/web/") |
| 51 | |
| 52 | val addr = InetAddress.getLocalHost().address |
| 53 | val addrs = "${addr[0].toInt() and 255}.${addr[1].toInt() and 255}.${addr[2].toInt() and 255}.${addr[3].toInt() and 255}" |
| 54 | hostname = "http://" + addrs + ":8090/boot" |
| 55 | |
| 56 | s.handlers.add { uri, method, headers, params, files -> |
| 57 | if (uri.startsWith(BOOT)) { |
| 58 | |
| 59 | println(" booting up... ") |
| 60 | |
| 61 | var text = Files.readString(File(Main.app + "lib/web/$initFile").toPath(), Charset.defaultCharset()) |
| 62 | |
| 63 | System.out.println(" canonical host name is :" + InetAddress.getLocalHost().getCanonicalHostName()) |
| 64 | |
| 65 | |
| 66 | text = text.replace("///IP///", addrs) //!! |
| 67 | text = text.replace("///ID///", "" + (id++)) |
| 68 | text = text.replace("///WSPORT///", "8090") |
| 69 | |
| 70 | return@add newFixedLengthResponse(Status.OK, "text/html", text) |
| 71 | } else if (uri.startsWith(RESOURCE)) { |
| 72 | var found = resourceMap.get(uri) |
| 73 | |
| 74 | if (found == null) { |
| 75 | System.out.println("\n\n couldn't find " + uri + " in " + resourceMap + "\n\n"); |
| 76 | return@add newFixedLengthResponse(Status.NOT_FOUND, "text/html", "not found") |
| 77 | } |
| 78 | |
| 79 | return@add s.serveFile(File(found!!)) |
| 80 | } |
no test coverage detected