The Artima Developer Community
Sponsored Link

Akka Concurrency Forum
Ch 13, Telnet server example (Success(...) and Failure(_))

4 replies on 1 page. Most recent reply: Mar 4, 2013 6:32 AM by Derek Wyatt

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Dan Luu

Posts: 27
Nickname: amitra
Registered: Feb, 2013

Ch 13, Telnet server example (Success(...) and Failure(_)) Posted: Mar 1, 2013 5:37 PM
Reply to this message Reply
Advertisement
I'm fairly certain I'm doing something wrong, but I'm at a loss as to what.

I get an error relating to every use of Success or Failure in the code:

[error] /Users/danluu/Dropbox/classes/eclipse/akkaC/src/main/scala/TelnetServer.scala:6 2: not found: value Success
[error] case Success(CurrentHeading(heading)) =>
[error] ^
[error] /Users/danluu/Dropbox/classes/eclipse/akkaC/src/main/scala/TelnetServer.scala:6 3: not found: value heading
[error] socket.write(headStr(heading))
[error] ^
[error] /Users/danluu/Dropbox/classes/eclipse/akkaC/src/main/scala/TelnetServer.scala:6 4: not found: value Failure
[error] case Failure(_) =>
[error] ^
[error] /Users/danluu/Dropbox/classes/eclipse/akkaC/src/main/scala/TelnetServer.scala:7 0: not found: value Success
[error] case Success(CurrentAltitude(altitude)) =>
[error] ^
[error] /Users/danluu/Dropbox/classes/eclipse/akkaC/src/main/scala/TelnetServer.scala:7 1: not found: value altitude
[error] socket.write(altStr(altitude))
[error] ^
[error] /Users/danluu/Dropbox/classes/eclipse/akkaC/src/main/scala/TelnetServer.scala:7 2: not found: value Failure
[error] case Failure(_) =>


From looking at example code, I see code that works that doesn't import anything that I haven't imported. A lot of people import akka.util._ and akka.actor._, but generalizing those two imports doesn't fix the error.

Do I really need to import something to use Success and Failure, or is this a sign that I've done something else wrong?


Derek Wyatt

Posts: 64
Nickname: dwyatt
Registered: Oct, 2012

Re: Ch 13, Telnet server example (Success(...) and Failure(_)) Posted: Mar 2, 2013 5:45 AM
Reply to this message Reply
Ugh, this is something I missed when they moved Futures around in Scala 2.10. "Success" and "Failure" are part of Scala, not Akka now.

import scala.util.{Success, Failure}

and you should be good to go.

Thanks for the bug report.

Dan Luu

Posts: 27
Nickname: amitra
Registered: Feb, 2013

Re: Ch 13, Telnet server example (Success(...) and Failure(_)) Posted: Mar 2, 2013 1:00 PM
Reply to this message Reply
Oh, that would explain why I saw references to akka libraries when I looked this up -- I was looking at outdated docs. Thanks!

Dan Luu

Posts: 27
Nickname: amitra
Registered: Feb, 2013

Re: Ch 13, Telnet server example (Success(...) and Failure(_)) Posted: Mar 3, 2013 10:05 PM
Reply to this message Reply
Related to this are the changes necessary to actually extract the altitude and heading.

To do that, I got a reference for the Altimeter, and then asked it (via '?') for the altitude, got the altitude, and then sent that back to the telnet server. That works fine, but it seems like a pipeTo would be a cleaner solution. Is there anything better than that?

Also, I noticed that sender points to DeadLetters inside the .onComplete block, so I had to assign sender to a val in order to get the correct ActorRef. Why is that? I'm assuming that means a new local sender is defined for that block, but why is that the case?

Derek Wyatt

Posts: 64
Nickname: dwyatt
Registered: Oct, 2012

Re: Ch 13, Telnet server example (Success(...) and Failure(_)) Posted: Mar 4, 2013 6:32 AM
Reply to this message Reply
Before I cover what I did to solve the problem, let's cover the DeadLetters thing. I'm not sure that this is the case but it reads the like you've closed over 'sender' in a Future. True? If so... oops. :) I talk about this earlier in the book but it gets summed up in 18.3.

Now, as for the solution, let's look at the Altimeter. Clearly you need a handler in the receive block that looks like this:

// Altimeter.receive
case GetCurrentAltitude =>
sender ! CurrentAltitude(altitude)

The next part is getting access to this from the plane. He needs a chunk in the receive block like this:

// Plane.receive
case Altimeter.GetCurrentAltitude =>
actorForControls("Altimeter") forward GetCurrentAltitude

And you're done. It reads like you've done what I've called out as an antipattern in section 18.8. I've done this myself... make an ask, get a future, pipe it to the sender... also known as "forward" :)

Flat View: This topic has 4 replies on 1 page
Topic: Ch 15.2, TimeUnit Previous Topic   Next Topic Topic: Minor inconsistency  in Section 7.4


Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us