ã¡ãã£ã¨ãã®ããããè¤éã«æããã®ã§ãä¸åº¦æ´çãã¦ãããã¾ãå ¨é¨ãå ¬å¼èªãã°æ¸ãã¦ãããã㧠OK ãªãã ãã©ãããã§ã¯ãã¾ãã«ãããã§ãããããããããã¨ããåç §ããããã¿ãããªæãã
IOManager ã¨ã¯ãªã«ã
Akka ã§IOãæ±ãã¨ãã«ã¯ã IOManager
ãéãã¦è¡ãã¨è¯ããå®éã® IO ã®ç
©ããããã¨ãé è½ãã¦ããããIOManager
㯠IOManagerActor
ã¨ããã¢ã¯ã¿ã¼ã¸ã®ãªãã¡ã¬ã³ã¹ã§ãããå®éã«Actãã¦ãããã®ã¯ IOManagerActor
ã®ããã ãIOManager
ã¯æåã§æ¢ãããããå¿
è¦ã¯ãªãã管çãã IO ãç¡ããªãã°åæã«æ¢ã¾ã£ã¦ãããããããè³¢ãã
IOManager ãéãã¦ã½ã±ãããä½ãã«ã¯ãIOManager#connect
ã IOManager#listen
ãå¼ã³åºãããããã SocketHandle
ã ServerHandle
ãè¿ããã¾ãããã®ã¨ãåæã«ãä½ããã socket ã«å¯¾ãã IO ã®çµæãã©ã®ã¢ã¯ã¿ã¼ã«ã¡ãã»ã¼ã¸ãéããããæå®ãã¦ãããã¨ãã§ãããä¾ã¨ãã¦ã¯ãããªæã
val serverActor = context.sysetm.actorOf[Props[ServerActor]] val serversocket:ServerHanlde = IOManager(context.system).listen("localhost", port)(serverActor)
ãããªæãã§ãµã¼ãã¼ã½ã±ãããä½ãã¨ãserverActor
ã«å¯¾ãã¦å種ã¤ãã³ã(æ°ããã¯ã©ã¤ã¢ã³ããæ¥ç¶ãã¦ããããã¨ã)ãã¡ãã»ã¼ã¸ã§éããã¦ããããã«ãªãã
ãããªæãã
class ServerActor extends Actor { def receive = { // æ°ããã¯ã©ã¤ã¢ã³ããæ¥ç¶ãã¦ããããã®ã¡ãã»ã¼ã¸ãåä¿¡ããã case IO.NewClient(serverSocket: ServerHandle) => // ããã§serverSocketã«å¯¾ãã¦ãªãããããå ¸åçãªå¦çã¯acceptã ã¨æã } }
ããããã«ã¾ã¨ããã¨ãIOManager
ãéãã¦ã½ã±ãããä½ãã¨ããã®ã½ã±ããã«å¯¾ããç´æ¥ã®æä½ã¯ IOManagerActor
ããã£ã¦ããã¦ãããããã¡ã¯èªåã§ä½ã£ãã¢ã¯ã¿ã¼ã§ãã®çµæãåä¿¡ãã¦ç¨®ã
ã®å¦çãè¡ãã°ãããã¨ãããã¨ã«ãªããIOManagerActor
ããã®ã¡ãã»ã¼ã¸ã«ã¯ ServerHandle
ã SocketHandle
ããããã¯ãã½ã±ãããããã¼ã¿ãèªã¿è¾¼ã¾ããããã¨ããã¤ãã³ãã®å ´åã¯ãã®ãã¼ã¿ã®å
容ãªã©ãå
¥ã£ã¦ããã®ã§ãããã«å¯¾ã㦠accept
ãã write
ããã®å¦çãè¡ãã°è¯ãã
詳ããã¯ å ¬å¼ã®ãªãã¡ã¬ã³ã¹ ãè¦ãã¨è¯ãã¨æãã
ã§ã¯ ServerHanlde/SocketHandle ã¨ã¯ãªã«ã
æ®éã®ãããã¯ã¼ã¯ããã°ã©ãã³ã°ã«æ
£ãã¦ããã¨ããã® ServerHandle/SocketHandle
ã£ã¦ã®ã¯ã½ã±ãããã®ãã®ãããããã¯ããã wrap ãããã®ãªã®ããªãã¨æã£ã¦ãã¾ãã®ã ããã©ãå®ã¯ããã¯ããã§ã¯ãªãã¦ãã»ãã©ãã«ãã ã®ããã³ãã«ãã§ããããã¨ãã°ãSocketHandle
ãéãã¦ã½ã±ããã«ä½ããæ¸ãè¾¼ãä¾ãè¦ã¦ã¿ããã
val byteString = ByteString("some string") socketHandle.asWritable.write(byteString)
ããã§ã½ã±ããã«å¯¾ã㦠byteString
ã®å
容ãæ¸ããããã§ã¯ socketHandle.asWritable.write()
ããã¨ãã«ãªã«ãèµ·ãã£ã¦ããã®ã ãããããã¤ã¯ããã®ã¡ã½ããããã£ã¦ãããã¨ã¯ãã½ã±ããã«byteStringãæ¸ãè¾¼ããã¨ããæä½ã§ã¯ãªãã¦ããIOManager
ã«å¯¾ãã¦ããã® socketHandle
ã«ç´ã¥ãããããã£ã³ãã«ã«byteStringãæ¸ãè¾¼ãã§ããã¨é ¼ãããã¨ã§ãããã¤ã¾ããsocket ã¸ã®å®éã®æ¸ãè¾¼ã¿ã¯ IOManager
ã«ããè¡ããããã¨ã«ãªãããã ã
ãªãã§ãããªãã¨ã«ãªã£ã¦ãã®ãã¨ããã¨ããããããããã§ã¢ããªã±ã¼ã·ã§ã³ã®éçºè ã¯ã½ã±ããã®ç®¡ç(ã½ã±ãããéãã¦ãã¨ãéãã¦ãã¨ã)ã IOManager ã«ä»»ãããã¨ãã§ããã¨ãã®ãä¸ç¹ãããã¨ããããã¯ã¼ã¯è¶ãã«ã¢ã¯ã¿ã¼å士ã§å調ä½æ¥ãè¡ãã¨ãã«ãsocket ãã®ãã®ã¯ãããã¯ã¼ã¯è¶ãã«æ¸¡ããªãã®ã§ããããã¯ã¼ã¯è¶ãã«ãããã¤ã«ããæ¸ãè¾¼ãã§ããã¨ããã¡ãã»ã¼ã¸ãéããå®éã® IO ã¯å®éã« socket ãæã£ã¦ãããã·ã³ãè¡ãã¨ãããã¨ãå®ç¾ããããã«ãã®ããã«ãªã£ã¦ãããã¨ããã®ãä¸ç¹ããªã®ã§ã¯ãªããã¨æã(ã¾ã ããã¯ã¡ããã¨èª¿ã¹ã¦ãªãã®ã§çå½ä¸æãèè ã«ããããã³ããæå¾ )ã
ã¾ã¨ããã¨ãServerHandle ã SocketHandle ã¯ã½ã±ãããã®ãã®ã§ã¯ãªãããã®ã½ã±ããã管çãã¦ããã¢ã¯ã¿ã¼ã«å¯¾ãã¦ãããæ¸ãè¾¼ãã§ãã¨ããacceptãã¦ãã¨ãããããã¡ãã»ã¼ã¸ãéã£ã¦ããããã¤ã§ããã
åããè©³ç´°ã¯ å ¬å¼ã®ãªãã¡ã¬ã³ã¹ ãåç §ã®ãã¨ã
ããã IO.Iteratee ã£ã¦ãªããªã®
IO.Iteratee
ãåºã¦ãã¦ããªãããã©ãããããã®ã¯ãããã¤ã¯ IO.Iteratee
㯠IOManager
ãã¡ã¨ä¸ç·ã«ä½¿ãã¨ä¾¿å©ã§ã¯ããããã©ãç´æ¥ã¯ãªãã®é¢ä¿ããªããIO.Itereatee
ã¨ããã®ã¯ããIOã®çµæã«å¯¾ãã¦ã©ãããæä½ãããããå
ã«å®ç¾©ãã¦ããã¦ãIO.Iteratee
ã«å¯¾ãã¦å®éã«ãã¼ã¿ãæµãè¾¼ã¾ããã¾ã§ãã®æä½ãé
延ããä»çµã¿ãã§ããã
REPLã§å®é¨ãã¦ã¿ããã
scala> import akka.actor.IO import akka.actor.IO scala > import akka.util.ByteString scala> val take4 = IO take(4) res0: akka.actor.IO.Iteratee[akka.util.ByteString] = Next(<function1>) scala>
ã¾ã㯠import ãã¦ããã(ã¨ãããakkaãã¤ã³ã¹ãã¼ã«ããã¦ãããã¨ãåæã§ãã)ããã¦ããã®ãã¨ã« IO take(4) ã§ç°¡å㪠Iteratee ãä½ã£ã¦ããããã®æä½ã¯ãIOãã4ãã¤ãèªã¿è¾¼ããã§ã¯ãªãããIOãã4ãã¤ãèªã¿è¾¼ãã¨ããæä½ãå®ç¾©ããIO.Iterateeãä½ããã¨ããæä½ã§ãããã¨ã«æ³¨æãã¦ã»ãããå®éã«ãã®æ®µéã§ã¯èªã¿è¾¼ã¿ã¯ãªããã¦ããªã(å®éãIOå¾ ã¡ã§ãããã¯ããã«ãããã«ããã³ãããè¿ã£ã¦ãã¦ãã)ã
ã§ã¯ä»åº¦ã¯ãããã®4ãã¤ãèªã¿è¾¼ãã¨ããæä½ã®ãã¨ããããæååã«ãã¦ããªã³ããããã¨ããæä½ãå®ç¾©ãã¦ã¿ããã
ã¾ã㯠byteString ãåãåãããã®å 容ã String ã«ã㦠println ããé¢æ°ãå®ç¾©ããã
val printBytes = {bytes:ByteString => println(bytes.decodeString("US-ASCII"))} printBytes: akka.util.ByteString => Unit = <function1>
ãããããtake4 ã«å¯¾ãã map æä½ãè¡ããIO.Iteratee#map(f: A => B)
ã¯ãã"ã¬ã·ã¼ãã® IO.Iteratee
ã®çµæå¾ãããå¤ã«å¯¾ã㦠f ãå¼ã³åºã"ã¨ããæä½ãå®ç¾©ããIO.Iteratee
ããè¿ããã¡ããã©ãªã¹ãã«å¯¾ãã map æä½ããããªã¹ãã«æ ¼ç´ããã¦ããã«å¯¾ã㦠f ãå¼ã³åºããçµæããªã¹ãã«æ ¼ç´ãã¦è¿ããã®ã¨åããããªæãã ã
scala> val take4AndPrintThat = take4.map(printBytes) take4AndPrintThat: akka.actor.IO.Iteratee[Unit] = Next(<function1>)
ããªããã¨ãä¸ã®ããã«è¡¨ç¾ãããã¨ãã§ããã
scala> val take4andPrintThat = for { | bytes <- IO.take(4) | string = bytes.decodeString("US-ASCII") | } yield println(string) take4andPrintThat: akka.actor.IO.Iteratee[Unit] = Next(<function1>)
Scala ã® for ã¯ã¡ãã£ã¨ç¹æ®ãªã®ã§æ £ããªãã¨èªã¿ã«ãããããããªããããã£ã¦ããã¨ã¯ä¸ã§ãã£ã map ã¨ä¸ç·ã§ããã
ãã¦ãããã§ãã4ãã¤ãèªã¿è¾¼ãã§ããã®çµæãprintãããã¨ããæä½ãå®ç¾©ãããIO.Iteratee
ãä½ãåºããã
ã§ã¯ããã®IO.Iterateeã«å¯¾ãã¦ãå®éã«ãã¼ã¿ãæ¸ãè¾¼ãã§ãã£ã¦ã¿ãããæ¸ãè¾¼ã¿ã¯ apply
ã§ã§ããã
scala> take4andPrintThat(IO.Chunk(ByteString("abcdefg"))) abcd res0: (akka.actor.IO.Iteratee[Unit], akka.actor.IO.Input) = (Done(()),Chunk(ByteString(101, 102, 103)))
"abcdefg"
ãæ¸ãè¾¼ãã çµæã4ãã¤ããåãåºããã¦è¡¨ç¤ºããããIO.Iteratee
㯠immutable ãªãªãã¸ã§ã¯ãã§ããããããæ®ã£ã3ãã¤ããå
é¨ã«ä¿æãããã¿ãããªãã¨ã¯ããªããããã ã¨ã¡ãã£ã¨ä½¿ãåæãããããã®ã§ãæ®ã£ããã¤ãã®ç®¡çãªã©ããã£ã¦ããããã®ã§ãã® IO.Iteratee
ãä¸æ wrap ãããããã IO.IterateeRef
ã ã
IO.IterateeRef ã§ããä½ãç ©ããããã¨ã¯ãªããªã
ããã§ã¯ IO.IterateeRefAsync
ã¨ããã®ã使ãããã©ãããã®çºã«ã¯å®è¡ã³ã³ããã¹ããå¿
è¦ã«ãªãã®ã§ãã¾ãã¯ä¸æºåã¨ãã¦ãä»å㯠import scala.concurrent.ExecutionContext.Implicits.global
ã§æé»ã®å®è¡ã³ã³ããã¹ããä½ã£ã¦ãããAkkaã¨ä¸ç·ã«ä½¿ãå ´åã«ã¯æ示çã«akkaã®system.contextã使ãã¹ãã ããã
scala> import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.ExecutionContext.Implicits.global
ãã¦ãããã§æºåãæ´ã£ãã®ã§ãã¾ãã¯IterateeRefãåå¾ããã
scala> val irefAsync = IO.IterateeRef.async(take4andPrintThat) irefAsync: akka.actor.IO.IterateeRefAsync[Unit] = akka.actor.IO$IterateeRefAsync@f57f347
æ示çã«å®è¡ã³ã³ããã¹ããæå®ãããã¨ãã«ã¯ IO.IterateeRef.async(take4andPrintThat)(context)
ã¨ããã°è¯ãã
ã§ã¯ãã®irefAsyncã«å¯¾ãã¦ãã¼ã¿ãæ¸ãè¾¼ãã§è¡ã£ã¦ã¿ããã
scala> irefAsync(IO.Chunk(ByteString("abcde"))) scala> abcd
take4andPrint
ã§å®ç¾©ãããæä½ãéåæã§å®è¡ããããããå
ã«ããã³ããã表示ãããã®ãè¦ã¦åãããã¨æãããã¦ãä»ãtake4ã®æ®ã1ãã¤ã(e)ã¯ã©ãã¸è¡ã£ãã ããããirefAsyncã®ä¸ã«æ®ã£ã¦ããã¯ãã§ããã試ãã¦ã¿ãã
scala> irefAsync(IO.Chunk(ByteString("fgh"))) scala> irefAsync(IO.Chunk(ByteString("fghg"))) scala> irefAsync(IO.Chunk(ByteString("fgha")))
ï¼ï¼ï¼ï¼ä½ã表示ãããªãï¼ï¼ãããããã®ã¯ããirefAsync
ã¯å
é¨ã« take4andPrintThat
ãæã£ã¦ãããããã®æä½ã¯ãã§ã«è¡ããã¦ãã¾ã£ããã次ã«ã©ããããããå®ç¾©ããã¦ããªãã®ã§ããã®ãã¨ã«ã©ãã©ããã¼ã¿ãæ¸ãè¾¼ã¾ãã¦ãããªã«ãèµ·ãããªããããã§ã¯ä½¿ãç©ã«ãªããªãã
ãã®ãããªäºæ ãé¿ããããã«ãä»åº¦ã¯ããæ°¸é ã«åãæä½ãç¹°ãè¿ãã¨ããæä½ãå®ç¾©ããIO.Iterateeããä½ãåºãã¦ã¿ããã
scala> val loopIteratee = IO.repeat(take4andPrintThat) loopIteratee: akka.actor.IO.Iteratee[Unit] = Next(<function1>)
ããã¦ãããåç
§ãã IO.IterateeRef
ãä½ãã
scala> val loopIrefAsync = IO.IterateeRef.async(loopIteratee) loopIrefAsync: akka.actor.IO.IterateeRefAsync[Unit] = akka.actor.IO$IterateeRefAsync@4f758781
ã§ã¯ããã«å¯¾ãã¦æ¸ãè¾¼ã¿ãè¡ã£ã¦ã¿ããã
scala> loopIrefAsync(IO.Chunk(ByteString("abcdef"))) scala> abcd scala> loopIrefAsync(IO.Chunk(ByteString("ghij"))) scala> efgh scala> loopIrefAsync(IO.Chunk(ByteString("k"))) scala> loopIrefAsync(IO.Chunk(ByteString("l"))) scala> ijkl
ããã¼ã¼ã¼ã¼ä¾¿å©ï¼
ã¨ããããã§ãã¾ã¨ããã¨
IO.Iteratee
ã¯ãIOã«å¯¾ãã¦ã©ãããæä½ãè¡ããããå®ç¾©ããããã®IO.Iteratee
ã«å¯¾ãã¦ãã¼ã¿ãæ¸ãè¾¼ãã¨ããããããå®ç¾©ãããéãã«ãã®ãã¼ã¿ã«å¯¾ãã¦æä½ãè¡ãIO.Iteratee
㯠immutable 㪠object ã§ããããããã®ãã£ããã¼ã¿ãã¨ãã足ããªãã£ããã¼ã¿ããå é¨ã«ä¿æãã¦ããããã¯ããªãã- ããã ã¨ãç´°ãããã£ã³ã¯ãã©ãã©ãæ¸ãè¾¼ãã§ããããã¿ãããªã¨ãã«ãã£ã¡ãä¸ä¾¿ãªã®ã§ãæ®éã¯
IO.IterateeRef
ã«å ãã§ä½¿ã IO.IterateeRef
ã¯æ¸ãè¾¼ã¾ãããã¼ã¿ãä¿æãã¦ããã¦ããã®ãã¼ã¿ã«å¯¾ãã¦é 次å é¨ã®IO.Iteratee
ãé©ç¨ãã¦ããIO.IterateeRef
ã¯ãä¸åº¦ä½¿ã£ãIO.Iteratee
ãä½åº¦ãåå©ç¨ãã¿ãããªãã¨ã¯ããªãã®ã§ããããããã¨ãããããã°ãããã®æä½ãä½åº¦ãç¹°ãè¿ãããã¨ãããããå®ç¾©ãã¦ãããIO.Iteratee
ãç¨æãã¦ãããå¿ è¦ããã
ãããªã¨ããã§ããããã
å®éã®ä¾
æåå³åã ããç«åãã£ãããµã¼ãã¼ ãåååã åã®ã¨ã³ããªã¼ããããã®æè¡ãçµã¿åããããããã«ãªä¾ã¨ãªã£ã¦ããã®ã§ãå®ä¾ã¨ãã¦ã¯ãããè¦ãã¨è¯ãã¨æãã
追è¨
@neko_gata_s doc.akka.io/docs/akka/2.2-⦠akka2.2ã®ããã¥ã¡ã³ãã§ã¯æ¢ã«Old IOæ±ãã«ãªã£ã¦ãã¦ãakka.ioããã±ã¼ã¸ãå¥ã«å±éããã¦ãã¾ããã¾ããakka.actor.IOãdeprecatedã«ãªãããã§ãâ¦ã
— mather ɹÇÉ¥ÊÉɯãã (@mather314) 2013å¹´6æ4æ¥
@mather314 ããããã®ãããªãã¨ãæãã¦ããã¾ããããããã¨ããããã¾ãã
æ³£ãã¦ãªããï¼ï¼ï¼ï¼ï¼ ããããããdeprecatedã«ãªããããªã®ã§ãæ°ããå¦ã¶ã²ã¨ã¯ä¸ã®ãã¤è¦ãã»ããããããï¼ï¼ï¼ï¼ï¼ï¼ æ³£ãã¦ãªãï¼ï¼ï¼ï¼ï¼ï¼