Created
December 23, 2016 08:51
-
-
Save fortitudepub/2baf1bb8f06c5956e701e837d276928c to your computer and use it in GitHub Desktop.
Scala actor with pcap thread send packet to itself.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.netease.cns.proton.agent.dhcp | |
import akka.actor.{Actor, ActorLogging} | |
import com.netease.cns.proton.agent.dhcp.DHCPPktRxActor.PcapRxPacket | |
import org.pcap4j.core.PcapNetworkInterface.PromiscuousMode | |
import org.pcap4j.core.Pcaps | |
import org.pcap4j.packet.Packet | |
object DHCPPktRxActor { | |
// TODO: define a packet which will be processed by each NetworkDHCPActor. | |
case class PcapRxPacket(pkt: Packet) | |
} | |
class DHCPPktRxActor(dummyInterface: String) extends Actor with ActorLogging { | |
// Start a thread which capture packet on dummy interface and turn to raw event | |
// to us. | |
val snapLen = 65536; | |
val mode = PromiscuousMode.PROMISCUOUS | |
val timeout = 10; | |
val intf = Pcaps.getDevByName(dummyInterface) | |
val handle = intf.openLive(snapLen, mode, timeout) | |
val thread = new Thread(new Runnable { | |
override def run(): Unit = { | |
println(s"handle blocking mode is ${handle.getBlockingMode().toString}") | |
while (true) { | |
val packet = handle.getNextPacketEx() | |
self ! new PcapRxPacket(packet) | |
} | |
} | |
}) | |
thread.start() | |
override def receive: Receive = { | |
case PcapRxPacket(pkt) => | |
log.debug(s"received dhcp packet ${pkt.toString}") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment