Hello everybody!
I am a n00b to Akka. I am trying to add some instrumentation code to an
application. I use Soot for doing the code transformation, and Akka for the
communications.
I have to instrument both a client and a server that communicate with each
other. When running tests, I found that only the server's messages reach
the actor. Note that client and server run in separate VMs.
Here is my helper and actor code
/**
* Functions for using the actor
*/
object InstrumentationServer {
/**
* Akka actor address
*/
private lazy val akkaAddress = AddressFromURIString(
"akka.tcp://instrumentationServerSystem@localhost:2552")
/**
* The Akka actor system
*/
private lazy val system = ActorSystem("instrumentationServerSystem")
/**
* The actor. We don't create many references to it, which means that none
* will work after a call to `shutdown`
*/
val instrumentationServerActor = system.actorOf(Props[
InstrumentationServer].
withDeploy(Deploy(scope = RemoteScope(akkaAddress))))
/**
* Shutdown the actor system.
* There is NO guarantee that the processing are over by the actors, as
they are fully asynchronous by design.
*
*/
def shutdown() = {
system.stop(instrumentationServerActor)
system.shutdown()
system.awaitTermination(Duration(5, TimeUnit.SECONDS))
}
/**
* File where the detected event paths are stored
*/
val outputPath = "ws-paths.txt"
val actorRunningTmpFile = "darwini-instrument-actor-running"
/**
* The above, as a Path object
*/
private val target = Paths.get(outputPath).toAbsolutePath
/**
* Writes the events to the file. This has been moved to the companion
object
* because Akka would crash with a java.lang.NoClassDefFoundError if the
actor was referencting `outputPath`. This is not
* an ideal design, but its internal this file, so we can manage.
* @param events the sequence of events
*/
private[InstrumentationServer] def writeEventsToFile(events: Seq[Message
]): Unit = {
val eventsPlain = events.map(ev => (ev.clazz, ev.method, ev.line))
val formatted = eventsPlain.mkString("->")
//Yes, this is wasteful compared to keeping a writer in memory, but the
number of writes
//should be small anyway, so that's not a big deal
Files.write(target, Seq(formatted).asJava, Codec.UTF8.charSet,
StandardOpenOption.CREATE, StandardOpenOption.APPEND,
StandardOpenOption.WRITE, StandardOpenOption.SYNC)
}
}
import InstrumentationServer._
/**
* This actor receives messages that the instrumented code will generate.
* It essentially stores the messages according to order of arrival and
writes them
* to file `InstrumentationServer.outputPath` at the end of each sequence.
*/
class InstrumentationServer extends Actor {
val tmpFile = Files.createTempFile(actorRunningTmpFile, ".tmp")
override def aroundReceive(receive: Actor.Receive, msg: Any): Unit = {
Files.write(tmpFile, Seq(s"Message received: $msg").asJava,
Codec.UTF8.charSet, StandardOpenOption.WRITE, StandardOpenOption.
APPEND, StandardOpenOption.SYNC)
super.aroundReceive(receive, msg)
}
//Ideally, I should use an Iteratee, instead
private val events = ArrayBuffer[Message]()
//Currently, we assume ordered input order
override def receive: Receive = {
case m: StartInteractionMessage => events.clear(); events += m
case m: ProcessingModuleExecuteMessage => events += m
case m: StopInteractionMessage => events += m; writeEventsToFile(events)
}
}
In the code I instrument, I essentially inject
InstrumentationServer,instrumentationServerActor.tell(msg, null).
Note that the code in aroundReceive is just for debugging.
Now, what am I doing wrong? I would like the actor to be created
automagically, receive messages from both client and server, dump to file
and be done with it. I'm sure there is a way to do that :)
--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ:
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.