2016-07-11 3 views
2

내가 TCP 서버 예를 here스칼라의 akka ByteString에서 패턴 일치가 가능합니까?

class SimplisticHandler extends Actor { 
    import Tcp._ 
    def receive = { 
    case Received(data) => sender() ! Write(data) 
    case PeerClosed  => context stop self 
    } 
} 

Received(data)에서의 SimplisticHandler을 채택 akka's tcp extention

와 자체 정의 된 TCP 프로토콜을 구현하고있어,이 바이트 순서에 패턴 일치 할 수 있습니까? 필자가 이해 하듯이 패턴 일치가 작동하도록 추출기가 정의되어 있어야합니다. documentation에 따라 정의 된 unapply이없는 것으로 보입니다. ByteStringcommand을 유지하면서

private[this] def commandA(cmd: ByteString) = println(cmd) 
def receive = { 
    // [warn] foo.scala:55: abstract type pattern Coll is unchecked since it is eliminated by erasure 
    // [error] foo.scala:55: type mismatch; 
    // [error] found : scala.collection.SeqLike[T,Coll] 
    // [error] required: akka.util.ByteString 
    case 0x01 +: command => commandA(command) 
    case 0x02 +: 0x03 +: command => commandB(command) 
} 

: 어린이 노동자 그런

def receive = { 
    case Received(data) => 
     val worker = getSomeWorkerActor 
     worker ! data 
    } 

:

내가 좋아하는 뭔가를 원한다. 그렇다면 어떻게? 또는 권장되는 대안은 무엇입니까? String으로 디코딩하지 않도록하고 가능한 경우 문자열과 비교하고 싶습니다.

스칼라 2.11.8, 패턴의 종류가 케이스 절의 오른쪽에서 infered 아니며, 구성과 일치하는 패턴으로 akka 2.4.7

+0

첫 번째 바이트를 찾으려고합니까? –

+0

@ChrisMartin 임의로 바이트 수인 – user2829759

+2

과 일치하는 것이 이상적입니다.이 옵션을 사용하면 즉시 사용할 수 있습니다 :'case 0x01 + : command => ...'. – devkat

답변

3

Pattern Matching Expressions 참조. 따라서 commandAcommandB의 매개 변수 유형은 고려되지 않습니다. 먼저 ByteString에 일치시켜야합니다.

def receive = { 
    case b: ByteString => b match { 
    case 0x01 +: command => commandA(command) 
    case 0x02 +: 0x03 +: command => commandB(command) 
    } 
} 
관련 문제