Whenever an actor receives a message in scala, we can access the sender of the actor by using a keyword 'sender' which is an an object of trait AbstractActor.
My question how is this 'sender' becoming accessible whenever a message is received.?
and also, can we override this implementation where along with sender some other data is also accessible such as ipaddress, port from where the data came .
As far as i know, there is no way you can get ipaddress and port from where the message has come .. Is there any way by which we can obtain ipaddress of sender and port number from this 'sender' object ?
Thanks for the help.
(You didn’t really say which actors, so I’m assuming an Akka answer is okay as well)
The
sender
method gives you theActorRef
which was implicitly or explicitly picked up at the sending site: if you use!
within an actor, itsimplicit val self: ActorRef
is found and used. If that sender lives in a differentActorSystem
than the recipient, i.e. it is a remote message send, then thesender
ref will contain all information necessary for replying, just look at its path:So, in short,
sender.path.address.host
(and analog for port) should give you what you need.In AKKA actor system, ! is overloaded as def !(message : scala.Any)(implicit sender : akka.actor.ActorRef) where sender is the actor that sent the message. After that you can call path method on the ActorRef, but I don't think you will be able to get real IP address from there.