Created
March 7, 2013 19:11
-
-
Save volgar1x/5110847 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.mambo.core | |
import akka.actor._ | |
import akka.util.ByteString | |
import scala.collection.mutable | |
/** | |
* @author Blackrush | |
*/ | |
class LoginActor(port: Int) extends Actor with ActorLogging { | |
val handler = context.actorOf(Props[LoginHandler]) | |
val clients = mutable.Map.empty[IO.Handle, Login.Client] | |
override def preStart() { | |
IOManager(context.system).listen("0.0.0.0", port) | |
} | |
def receive = { | |
case IO.Listening(server, addr) => log.debug("listening on {}", port) | |
case IO.NewClient(server) => { | |
val socket = server.accept() | |
val client = new Login.Client(socket) | |
clients(socket) = client | |
log.debug("new incoming client {}", socket.uuid) | |
handler ! Login.Connected(client) | |
} | |
case IO.Read(socket, bytes) => { | |
val client = clients(socket) | |
val message = Login.asLoginMessage(bytes) | |
log.debug("new incoming data {} from {}", message, socket.uuid) | |
handler ! Login.InputMessage(client, message) | |
} | |
case IO.Closed(socket: IO.SocketHandle, cause) => { | |
val client = clients(socket) | |
client.active = false | |
log.debug("client {} is disconnected", socket.uuid) | |
handler ! Login.Disconnected(client) | |
} | |
case IO.Closed(server: IO.ServerHandle, cause) => { | |
log.debug("closing") | |
context.system.shutdown() | |
} | |
} | |
} | |
object Login { | |
class Client(socket: IO.SocketHandle) { | |
var active: Boolean = false | |
def write(o: Any) { | |
if (!active) return | |
val bytes = asByteString(o) | |
socket.write(bytes) | |
} | |
def !(o: Any) = write(o) | |
def close() = if (active) socket.close() | |
} | |
trait Message | |
case class Connected(client: Client) extends Message | |
case class Disconnected(client: Client) extends Message | |
case class InputMessage(client: Client, msg: Any) extends Message | |
def asByteString(o: Any): ByteString = ??? | |
def asLoginMessage(bytes: ByteString): Any = ??? | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment