Created
August 13, 2013 17:23
-
-
Save volgar1x/6223477 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.photon.login | |
import akka.actor._ | |
import com.typesafe.config.ConfigFactory | |
import akka.io.{IO, Tcp} | |
import akka.util.ByteString | |
object LoginServer { | |
def main(params: Array[String]) { | |
val system = ActorSystem( | |
name = "PhotonLoginServer", | |
config = ConfigFactory.empty | |
) | |
system.actorOf(Props[NetworkMaster]) | |
} | |
} | |
class SettingsImpl(system: ExtendedActorSystem) extends Extension { | |
val config = system.settings.config | |
val LoginPort = config.getInt("photon.login.port") | |
} | |
object Settings extends ExtensionId[SettingsImpl] with ExtensionIdProvider { | |
def lookup = Settings | |
def createExtension(system: ExtendedActorSystem) = new SettingsImpl(system) | |
} | |
class NetworkMaster extends Actor { | |
import Tcp._ | |
import context.system | |
val settings = Settings(system) | |
val manager = IO(Tcp) | |
manager ! Bind(self, settings.LoginPort) | |
def receive = { | |
case Connected(_, _) => | |
sender ! Register(context.actorOf(Props[NetworkHandler])) | |
} | |
} | |
class ClientState(val value: Int) | |
object ClientState { | |
val None = new ClientState(0) | |
val Authentication = new ClientState(1) | |
} | |
class ClientContext { | |
val state: ClientState | |
} | |
class NetworkHandler extends Actor { | |
import Tcp._ | |
implicit val clientContext = new ClientContext | |
def receive = { | |
case Received(input) => input match { | |
case ConnectionRequest(username, pass) => | |
case QueueStatusRequest() => | |
} | |
} | |
object ConnectionRequest { | |
def unapply(input: ByteString)(implicit context: ClientContext): Option[(String, String)] = | |
context.state match { | |
case ClientState.Authentication => Some(input.splitAt(input.indexOf('\n'))) | |
case _ => None | |
} | |
} | |
object QueueStatusRequest { | |
def unapply(input: ByteString): Boolean = input.indexOf("Af") == 0 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment