|
| 1 | +/* __ *\ |
| 2 | +** ________ ___ / / ___ __ ____ PhantomJS support for Scala.js ** |
| 3 | +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013-2017, LAMP/EPFL ** |
| 4 | +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ https://www.scala-js.org/ ** |
| 5 | +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** |
| 6 | +** |/____/ ** |
| 7 | +\* */ |
| 8 | + |
| 9 | +package org.scalajs.jsenv.phantomjs |
| 10 | + |
| 11 | +import javax.servlet.http.HttpServletRequest |
| 12 | + |
| 13 | +import org.eclipse.jetty.server.Server |
| 14 | +import org.eclipse.jetty.server.nio.SelectChannelConnector |
| 15 | +import org.eclipse.jetty.websocket.{WebSocket, WebSocketHandler} |
| 16 | +import org.eclipse.jetty.util.component.{LifeCycle, AbstractLifeCycle} |
| 17 | +import org.eclipse.jetty.util.log |
| 18 | + |
| 19 | +private[phantomjs] final class JettyWebsocketManager( |
| 20 | + wsListener: WebsocketListener) extends WebsocketManager { thisMgr => |
| 21 | + |
| 22 | + private[this] var webSocketConn: WebSocket.Connection = null |
| 23 | + private[this] var closed = false |
| 24 | + |
| 25 | + // We can just set the logger here, since we are supposed to be protected by |
| 26 | + // the private ClassLoader that loads us reflectively. |
| 27 | + log.Log.setLog(new WSLogger("root")) |
| 28 | + |
| 29 | + private[this] val connector = new SelectChannelConnector |
| 30 | + |
| 31 | + connector.setHost("localhost") |
| 32 | + connector.setPort(0) |
| 33 | + |
| 34 | + private[this] val server = new Server() |
| 35 | + |
| 36 | + server.addConnector(connector) |
| 37 | + server.setHandler(new WebSocketHandler { |
| 38 | + // Support Hixie 76 for Phantom.js |
| 39 | + getWebSocketFactory().setMinVersion(-1) |
| 40 | + |
| 41 | + override def doWebSocketConnect( |
| 42 | + request: HttpServletRequest, protocol: String): WebSocket = |
| 43 | + new ComWebSocketListener |
| 44 | + }) |
| 45 | + |
| 46 | + server.addLifeCycleListener(new AbstractLifeCycle.AbstractLifeCycleListener { |
| 47 | + override def lifeCycleStarted(event: LifeCycle): Unit = { |
| 48 | + if (event.isRunning()) |
| 49 | + wsListener.onRunning() |
| 50 | + } |
| 51 | + }) |
| 52 | + |
| 53 | + private class ComWebSocketListener extends WebSocket.OnTextMessage { |
| 54 | + override def onOpen(connection: WebSocket.Connection): Unit = { |
| 55 | + thisMgr.synchronized { |
| 56 | + if (isConnected) |
| 57 | + throw new IllegalStateException("Client connected twice") |
| 58 | + connection.setMaxIdleTime(Int.MaxValue) |
| 59 | + webSocketConn = connection |
| 60 | + } |
| 61 | + wsListener.onOpen() |
| 62 | + } |
| 63 | + |
| 64 | + override def onClose(statusCode: Int, reason: String): Unit = { |
| 65 | + thisMgr.synchronized { |
| 66 | + webSocketConn = null |
| 67 | + closed = true |
| 68 | + } |
| 69 | + wsListener.onClose() |
| 70 | + server.stop() |
| 71 | + |
| 72 | + if (statusCode != 1000) { |
| 73 | + throw new Exception("Abnormal closing of connection. " + |
| 74 | + s"Code: $statusCode, Reason: $reason") |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + override def onMessage(message: String): Unit = |
| 79 | + wsListener.onMessage(message) |
| 80 | + } |
| 81 | + |
| 82 | + private class WSLogger(fullName: String) extends log.AbstractLogger { |
| 83 | + private[this] var debugEnabled = false |
| 84 | + |
| 85 | + def debug(msg: String, args: Object*): Unit = |
| 86 | + if (debugEnabled) log("DEBUG", msg, args) |
| 87 | + |
| 88 | + def debug(msg: String, thrown: Throwable): Unit = |
| 89 | + if (debugEnabled) log("DEBUG", msg, thrown) |
| 90 | + |
| 91 | + def debug(thrown: Throwable): Unit = |
| 92 | + if (debugEnabled) log("DEBUG", thrown) |
| 93 | + |
| 94 | + def getName(): String = fullName |
| 95 | + |
| 96 | + def ignore(ignored: Throwable): Unit = () |
| 97 | + |
| 98 | + def info(msg: String, args: Object*): Unit = log("INFO", msg, args) |
| 99 | + def info(msg: String, thrown: Throwable): Unit = log("INFO", msg, thrown) |
| 100 | + def info(thrown: Throwable): Unit = log("INFO", thrown) |
| 101 | + |
| 102 | + def warn(msg: String, args: Object*): Unit = log("WARN", msg, args) |
| 103 | + def warn(msg: String, thrown: Throwable): Unit = log("WARN", msg, thrown) |
| 104 | + def warn(thrown: Throwable): Unit = log("WARN", thrown) |
| 105 | + |
| 106 | + def isDebugEnabled(): Boolean = debugEnabled |
| 107 | + def setDebugEnabled(enabled: Boolean): Unit = debugEnabled = enabled |
| 108 | + |
| 109 | + private def log(lvl: String, msg: String, args: Object*): Unit = |
| 110 | + wsListener.log(s"$lvl: $msg " + args.mkString(", ")) |
| 111 | + |
| 112 | + private def log(lvl: String, msg: String, thrown: Throwable): Unit = |
| 113 | + wsListener.log(s"$lvl: $msg $thrown\n{$thrown.getStackStrace}") |
| 114 | + |
| 115 | + private def log(lvl: String, thrown: Throwable): Unit = |
| 116 | + wsListener.log(s"$lvl: $thrown\n{$thrown.getStackStrace}") |
| 117 | + |
| 118 | + protected def newLogger(fullName: String) = new WSLogger(fullName) |
| 119 | + } |
| 120 | + |
| 121 | + def start(): Unit = server.start() |
| 122 | + |
| 123 | + def stop(): Unit = server.stop() |
| 124 | + |
| 125 | + def isConnected: Boolean = webSocketConn != null && !closed |
| 126 | + def isClosed: Boolean = closed |
| 127 | + |
| 128 | + def localPort: Int = connector.getLocalPort() |
| 129 | + |
| 130 | + def sendMessage(msg: String): Unit = synchronized { |
| 131 | + if (webSocketConn != null) |
| 132 | + webSocketConn.sendMessage(msg) |
| 133 | + } |
| 134 | + |
| 135 | +} |
0 commit comments