c# barcode generator library open source ACTORS AND CONCURRENCY in Font

Generation Data Matrix ECC200 in Font ACTORS AND CONCURRENCY

CHAPTER 6 ACTORS AND CONCURRENCY
Data Matrix ECC200 Creation In None
Using Barcode maker for Font Control to generate, create ECC200 image in Font applications.
www.OnBarcode.com
Code128 Creator In None
Using Barcode generator for Font Control to generate, create Code128 image in Font applications.
www.OnBarcode.com
protected def updateMessage: Any } case class Messages(msgs: List[String]) object Chat extends Actor with ListenerMgt with GetMgt { private var msgs: List[String] = Nil def act = loop { react(handler orElse { case s: String => msgs ::= s updateListeners() }) } protected def updateMessage = Messages(msgs) this.start } class Listen extends Actor { def act = loop { react { case Messages(m) => println("Got "+m) } } this.start }
EAN128 Maker In None
Using Barcode generator for Font Control to generate, create GTIN - 128 image in Font applications.
www.OnBarcode.com
UPC Code Creation In None
Using Barcode creator for Font Control to generate, create UPC-A Supplement 5 image in Font applications.
www.OnBarcode.com
That s the whole listing. Let s take it piece by piece. First, we import our Actors:
Make Barcode In None
Using Barcode encoder for Font Control to generate, create Barcode image in Font applications.
www.OnBarcode.com
Quick Response Code Generator In None
Using Barcode printer for Font Control to generate, create QR image in Font applications.
www.OnBarcode.com
import scala.actors.Actor import Actor._
GTIN - 13 Printer In None
Using Barcode maker for Font Control to generate, create GTIN - 13 image in Font applications.
www.OnBarcode.com
Planet Encoder In None
Using Barcode printer for Font Control to generate, create USPS Confirm Service Barcode image in Font applications.
www.OnBarcode.com
We define a generic trait called Buildable that has a single handler method. The handler method has the signature that we need to pass to the react method in an Actor, and the implementation is to return a partial function that will match nothing. This is analogous to a blank method in a class that you expect to be overloaded. Your subclass can implement functionality, and when they call super, there is no additional work done.
DataMatrix Generator In Objective-C
Using Barcode creation for iPhone Control to generate, create DataMatrix image in iPhone applications.
www.OnBarcode.com
Generate Data Matrix In Objective-C
Using Barcode printer for iPad Control to generate, create ECC200 image in iPad applications.
www.OnBarcode.com
trait Buildable { def handler: PartialFunction[Any, Unit] = Map.empty }
Painting UCC-128 In Java
Using Barcode creator for Android Control to generate, create UCC.EAN - 128 image in Android applications.
www.OnBarcode.com
Decoding GTIN - 128 In Visual Basic .NET
Using Barcode recognizer for .NET framework Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
CHAPTER 6 ACTORS AND CONCURRENCY
Draw Barcode In .NET
Using Barcode maker for ASP.NET Control to generate, create Barcode image in ASP.NET applications.
www.OnBarcode.com
QR Code 2d Barcode Creation In .NET
Using Barcode printer for ASP.NET Control to generate, create QR Code image in ASP.NET applications.
www.OnBarcode.com
Next, we define the messages for a generic listener:
PDF-417 2d Barcode Creation In C#.NET
Using Barcode generation for .NET framework Control to generate, create PDF 417 image in .NET framework applications.
www.OnBarcode.com
Creating Barcode In .NET Framework
Using Barcode generator for ASP.NET Control to generate, create Barcode image in ASP.NET applications.
www.OnBarcode.com
case class Add(who: Actor) case class Remove(who: Actor)
Barcode Creator In Visual Studio .NET
Using Barcode printer for VS .NET Control to generate, create Barcode image in .NET applications.
www.OnBarcode.com
Drawing QR Code ISO/IEC18004 In VB.NET
Using Barcode drawer for Visual Studio .NET Control to generate, create QR image in Visual Studio .NET applications.
www.OnBarcode.com
We define the ListenerMgt trait that extends Buildable, has private state and implements the updateListeners method, and requires the updateMessage method to be implemented:
Read Data Matrix 2d Barcode In None
Using Barcode decoder for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
USS-128 Drawer In Java
Using Barcode generator for BIRT reports Control to generate, create UCC.EAN - 128 image in BIRT applications.
www.OnBarcode.com
trait ListenerMgt extends Buildable { private var listeners: List[Actor] = Nil
We implement the handler method by composing the superclass s handler with this class s message handler:
override def handler = super.handler orElse { case Add(who) => listeners = who :: listeners who ! updateMessage case Remove(who) => listeners -= who }
We update all the listeners:
protected def updateListeners() { val m = updateMessage listeners.foreach(a => a ! m) }
Any class that mixes this trait in must implement the updateMessage method:
protected def updateMessage: Any }
We ve implemented ListenerMgt. Now, let s implement a trait, GetMgt, which will respond to the GetInfo message with the result of the updateMessage call:
case object GetInfo trait GetMgt extends Buildable { override def handler = super.handler orElse { case GetInfo => reply(updateMessage) } protected def updateMessage: Any }
CHAPTER 6 ACTORS AND CONCURRENCY
Finally, let s create a Chat server that composes the ListenerMgt and GetMgt traits into an Actor and then adds Chat-specific functionality:
case class Messages(msgs: List[String]) object Chat extends Actor with ListenerMgt with GetMgt { private var msgs: List[String] = Nil def act = loop { react(handler orElse { case s: String => msgs = s :: msgs updateListeners() }) } protected def updateMessage = Messages(msgs) this.start }
The composition of Actor with ListenerMgt with GetMgt builds a class that has all of these traits combined. During the composition, ListenerMgt becomes the superclass of GetMgt, so GetMgt s super.handler call will invoke ListenerMgt s handler method. This allows us to chain the calls to handler and compose the PartialFunction that we pass to react. We add in the Chat-specific PartialFunction, and we ve got a complete Chat server. Let s write some code to check it out.
class Listen extends Actor { def act = loop { react { case Messages(m) => println("Got "+m) } } this.start }
Let s run the code and see what happens:
scala> val listen = new Listen
listen: Listen = Listen@10cc9b4
CHAPTER 6 ACTORS AND CONCURRENCY
scala> Chat ! Add(listen)
Got List()
scala> Chat ! "Hello"
Got List(Hello)
scala> Chat ! "Dude"
Got List(Dude, Hello)
scala> Chat ! GetInfo
res3: Any = Messages(List(Dude, Hello))
This is a simple example of composing an Actor out of generic pieces. The generic pieces can be combined into a single Actor class that provides specific business logic as well as the more generic functionality of listeners and so on.
Summary
In this chapter, we explored Scala Actors. Actors provide an alternative model for concurrency that feels to me a whole lot like coarse-grained OOP. Philipp Haller is a member of Martin Odersky s group and is primarily responsible for Scala s Actor design and implementation. Philipp has written a number of papers on Actors that provide a lot of valuable insight into his design choices as well as Actor implementation.11 There is plenty more
11. Philipp Haller and Martin Odersky, Event Based Programming without Inversion of Control, http:// lampwww.epfl.ch/~odersky/papers/jmlc06.pdf and Philipp Haller and Martin Odersky, Actors that Unify Threads and Events, http://lamp.epfl.ch/~phaller/doc/haller07coord.pdf.
Copyright © OnBarcode.com . All rights reserved.