Created
November 22, 2011 21:55
-
-
Save gamlerhart/1387130 to your computer and use it in GitHub Desktop.
akka-mobile-first-code-tour
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
| class AkkaDroidActivity extends Activity with ActivityActor { | |
| override def onCreate(savedInstanceState: Bundle) { | |
| super.onCreate(savedInstanceState) | |
| setContentView(R.layout.main) | |
| actorOf[DoSomeWork].start() | |
| } | |
| protected def receive = { | |
| case WorkDone(happend) => { | |
| val inputTextBox = findViewById(R.id.enterMsgBox).asInstanceOf[EditText] | |
| inputTextBox.setText("happend") | |
| } | |
| } | |
| } | |
| case class WorkDone(result:String) | |
| class DoSomeWork extends Actor{ | |
| protected def receive = { | |
| case "Start" =>{ | |
| val result = workForAWhile() | |
| self.reply(result) | |
| } | |
| } | |
| private def workForAWhile() :String ={ | |
| // work work | |
| "work-done" | |
| } | |
| } |
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
| akka { | |
| event-handlers = ["akka.mobile.android.LogcatLogger"] | |
| event-handler-level = "DEBUG" # Options: ERROR, WARNING, INFO, DEBUG | |
| mobile{ | |
| client{ | |
| host = "our.server.host" | |
| port = 2552 | |
| } | |
| } | |
| } |
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
| class ChatClientActor extends Actor{ | |
| protected def receive = { | |
| case SentToServer(msg) =>{ | |
| val chatService = MyApplication.remote.actorFor("chat-service") | |
| chatService ! msg | |
| } | |
| case MessageFromServer(msg) =>{ | |
| // well, show the message or do what is needed | |
| } | |
| } | |
| } |
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
| class TalksToMobileDevices extends Actor with ServiceActor{ | |
| protected def receive = { | |
| case msg =>{ | |
| self.reply("Answer for: "+msg) | |
| val clientId = this.clientId; | |
| if(clientId.isDefined){ | |
| // store it in database. So that you later can talk to this client | |
| } | |
| } | |
| } | |
| } |
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
| remote.register("notifications", Actor.actorOf(new Actor() { | |
| protected def receive = { | |
| case s: String => { | |
| // react to the notification | |
| } | |
| } | |
| }) |
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
| val clientId = // clientId. Like from the database | |
| val serviceOnPhone = chatServer.actorOf(clientId, "notifications") | |
| serviceOnPhone ! "Hi Phone" |
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
| // Registred in the AndroidManifest as our application object | |
| class MyApplication extends android.app.Application { | |
| lazy val remote = MobileRemoteClient.createClient( | |
| AndroidDevice(this)) | |
| } |
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
| val chatServer = NettyRemoteServer.start(2552); | |
| chatServer.register("chat-service", Actor.actorOf[ChatServerActor]) | |
| class ChatServerActor extends Actor{ | |
| protected def receive = { | |
| case msg =>{ | |
| self.reply("Answer for: "+msg) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment