Wiki

Clone wiki

aiengine / DatabaseIntegrationRuby

Database integration

One of the main functions of the engine is the easy integration with databases. Lets see some examples of how works the database interface.

On the first example we will use Redis(http://redis.io/) So we create a class call redisAdaptor that implements the methods update,insert and remove. So for every new flow that the system receives the method insert will be called.

require "../src/ruaiengine"
require "redis"

class RedisAdaptor < DatabaseAdaptor
  attr_reader :total_inserts
  attr_reader :total_updates
  attr_reader :total_removes
  attr_reader :ftype

def initialize(ftype)
  @ftype = ftype
  @total_inserts = 0
  @total_updates = 0
  @total_removes = 0
  @conn = Redis.new
end

def insert(key)
  @conn.hset(@ftype,key,"{}")
  @total_inserts += 1
end

def remove(key)
  @conn.hdel(@ftype,key)
  @total_removes += 1
end

def update(key, data)
  @conn.hset(@ftype,key,data)
  @total_updates += 1
end

def show
  printf "Statistics of adaptor %s\n", @ftype
  printf "\tInserts %d\n", @total_inserts
  printf "\tUpdates %d\n", @total_updates
  printf "\tRemoves %d\n", @total_removes
end

end

Create a new instance of a LAN network for example, and allocate the number of flows to process

s = StackLan.new
pd = PacketDispatcher.new
pd.stack = s

s.total_tcp_flows = 327680
s.total_udp_flows = 163840

Create a new instance of the DatabaseAdaptor and plug it to the TCP and UDP part of the engine.

r_udp = RedisAdaptor.new("udpflows")
r_tcp = RedisAdaptor.new("tcpflows")

s.set_tcp_database_adaptor(r_tcp,1024)
s.set_udp_database_adaptor(r_udp,512)

Open the network device and let the engine run

pd.open("ens7")
begin
  pd.run()
rescue => e
  print "Stop capturing packets"
  print e.inspect
  print e.backtrace
end

r_udp.show()
r_tcp.show()

pd.close()

Now you can check the results on the redis database.

Updated