Skip to content

Instantly share code, notes, and snippets.

@mumoshu
Last active December 22, 2015 12:29
Show Gist options
  • Save mumoshu/6472913 to your computer and use it in GitHub Desktop.
Save mumoshu/6472913 to your computer and use it in GitHub Desktop.
Akka 2.2のバイナリデータのエンコーディング、デコーディング周りの概念 http://doc.akka.io/docs/akka/2.2.1/scala/io-codec.html

Akkaのネットワーキングでは、2者間の通信を最小単位としてプロトコルを組む。

A <-- Protocol --> B

2者間のプロトコルの実装はPipelineと呼ばれる。 PipelineはAからのメッセージをBに通じるように翻訳し、一方でBからのメッセージをAに通じるように翻訳するという働きをする。

A <-- Pipeline --> B

Pipelineは1つ以上のPipeline Stageので構成される。

A <-- [ Pipeline Stage 1 >> Pipeline Stage 2 ] --> B

PipelineにはPipeline Stageはプロトコル・スタックのように上位層、下位層がある。上位側をAbove、下位側をBelowという。 Aboveはよりアプリケーション寄りで、Belowはよりバイトデータ寄りである。

Above <-- [ Pipeline Stage 1 >> Pipeline Stage 2 ] --> Below

Pipeline StageはPipeline ContextとPipe Pairで構成される。 Pipeline ContextはPipe Pairの設定情報のようなものである。 Pipe Pairは2者間の通信の翻訳を行う。

          ------------------------    ------------------------
          | Pipeline Stage 1     |    | Pipeline Stage 2     |
          |                      |    |                      |
          | [Pipeline Context 1] |    | [Pipeline Context 2] |
Above <-- |-[Pipe Pair 1 ]-------| >> |-[Pipe Pair 2 ]-------| --> Below
          ------------------------    ------------------------

Pipe Pairは2つの小Pipelineで構成される。 一つは上位層から下位層方向への通信を翻訳する Command Pipeline、 もう一つは下位層から上位層方向への通信を翻訳する Event Pipelineである。

          --------------------------    --------------------------
          | Pipeline Stage 1       |    | Pipeline Stage 2       |
          |                        |    |                        |
          | [Pipeline Context 1]   |    | [Pipeline Context 2]   |
          | ---------------------- |    | ---------------------- |
          | | Pipe Pair 1        | | >> | | Pipe Pair 2        | |
          | |                    | |    | |                    | |
Above --> | | Command Pipeline 1 | | -> | | Command Pipeline 2 | | --> Below
      <-- | | Event Pipeline 1   | | <- | | Event Pipeline 2   | | <--
          --------------------------    --------------------------

上位層から下位層へのメッセージは Command と呼ばれる。 下位層から上位層へのメッセージは Event と呼ばれる。

                   --------------------------              --------------------------
                   | Pipeline Stage 1       |              | Pipeline Stage 2       |
                   |                        |              |                        |
                   | [Pipeline Context 1]   |              | [Pipeline Context 2]   |
                   | ---------------------- |              | ---------------------- |
                   | | Pipe Pair 1        | | >>           | | Pipe Pair 2        | |
                   | |                    | |              | |                    | |
Above [Command]--> | | Command Pipeline 1 | | [Command]--> | | Command Pipeline 2 | | [Command]--> Below
      <----[Event] | | Event Pipeline 1   | | <----[Event] | | Event Pipeline 2   | | <----[Event]
                    --------------------------              --------------------------

Commandが上位層から下位層へ向けてCommand Pipelineによって翻訳されるとき、翻訳元をCmdAbove、翻訳先をCmdBelowと呼ぶ。 同様に、Eventが下位層から上位層に向けてへ向けてEvent Pipelineによって翻訳されるとき、翻訳元をEvtBelow、翻訳先をEvtAboveと呼ぶ。

                     --------------------------                         --------------------------
                     | Pipeline Stage 1       |                         | Pipeline Stage 2       |
                     |                        |                         |                        |
                     | [Pipeline Context 1]   |                         | [Pipeline Context 2]   |
                     | ---------------------- |                         | ---------------------- |
                     | | Pipe Pair 1        | |           >>            | | Pipe Pair 2        | |
                     | |                    | |                         | |                    | |
Above --> [CmdAbove] | | Command Pipeline 1 | | [CmdBelow]-->[CmdAbove] | | Command Pipeline 2 | | [CmdBelow]--> Below
      <-- [EvtAbove] | | Event Pipeline 1   | | [EvtBelow]<--[EvtAbove] | | Event Pipeline 2   | | [EvtBelow]<--
                     --------------------------                         --------------------------

以上をScalaの型でいうと、

  • PipelineStage[Context <: PipelineContext, CmdAbove, CmdBelow, EvtAbove, EvtBelow]
  • PipePair[CmdAbove, CmdBelow, EvtAbove, EvtBelow]

である。

PipelineStageには上位層のCmdとEvtが同じで、かつ下位層のCmdとEvtが同じであるSymmetricPipelineStageがある。

SymmetricPipelineStage[Context <: PipelineContext, Above, Below] extends PipelineStage[Context, Above, Below, Above, Below]

PipePairには上位層のCmdとEvtが同じで、かつ下位層のCmdとEvtが同じであるSymmetricPipePairがある。

SymmetricPipePair[Above, Below] extends PipePair[Above, Below, Above, Below]

tiny-mmoでの例

Thriftメッセージのオブジェクト
  <--> 型ヒントとThriftメッセージのバイナリデータ: (Byte, Array[Byte])
  <--> 型ヒントとThriftメッセージのバイナリデータ: ByteString
  <--> メッセージ長(4バイト) + メッセージ本文(可変長) : ByteString

(HintFieldFrame)[https://github.com/mumoshu/tiny-mmo/blob/8b4b81aef885a82139c66b0985a53374cfc88860/src/main/scala/com/github/mumoshu/mmo/protocol/HintFieldFrame.scala#L7] (LengthFieldFrame)[https://github.com/mumoshu/tiny-mmo/blob/8b4b81aef885a82139c66b0985a53374cfc88860/src/main/scala/com/github/mumoshu/mmo/protocol/LengthFieldFrame.scala#L8]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment