From 78908906a5ebedba5762cb6ac392bfed651c7c7c Mon Sep 17 00:00:00 2001 From: Yves Fischer Date: Sun, 4 Mar 2012 22:22:51 +0100 Subject: ebus-racket: inserter.rkt , overall improvements --- ebus-racket/inserter.rkt | 87 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100755 ebus-racket/inserter.rkt (limited to 'ebus-racket/inserter.rkt') diff --git a/ebus-racket/inserter.rkt b/ebus-racket/inserter.rkt new file mode 100755 index 0000000..c340019 --- /dev/null +++ b/ebus-racket/inserter.rkt @@ -0,0 +1,87 @@ +#! /usr/bin/env racket +#lang racket/base +(require racket/cmdline + racket/tcp + racket/pretty + racket/match + "db.rkt" + "layer7.rkt" + "layer2.rkt") + +(define connect-host? (make-parameter null)) +(define connect-port? (make-parameter null)) +(define loglevel-layer2? (make-parameter 'info)) +(define loglevel-db? (make-parameter 'info)) +(define insert? (make-parameter #f)) + +(define (handle-fields fields) + (for ([field fields]) + (when (insert?) + (apply db-insert-field field)) + (when (not (insert?)) + (display (format "Field: ~a~n" field))))) + +(define (read-ebus-loop7 input-port) + (let ([fields (layer7-read-ebus (current-input-port))]) + (when (not (or (void? fields) (eof-object? fields))) + (handle-fields fields)) + (cond ((not (eof-object? fields)) (read-ebus-loop7 input-port))))) + +;; Start Thread that observe all given log-receivers +(define (start-logger-thread receiver1 . receiverN) + (define receivers (cons receiver1 receiverN)) + (void + (thread + (lambda () + (let loop () + (match (apply sync receivers) + [(vector level msg data) + (printf "[~s] ~a~n" level msg) + (flush-output)]) + (loop)))))) + +(define (main) + ;; Parse commandline + (command-line + #:once-each + [("-c" "--connect") host port "Connect to server " + (connect-host? host) + (connect-port? port)] + ["--debug-layer2" "Log level for Layer 2 Parser" + (loglevel-layer2? 'debug)] + ["--debug-db" "Log level for DB" + (loglevel-db? 'debug)] + ["--insert" "Do Insert into Database" + (insert? #t)] + ["--db-user" user "Datanase User" + (db-con-user? user)] + ["--db-password" password "Database password" + (db-con-password? password)] + ["--db-database" database "Database database-name" + (db-con-database? database)] + ["--db-server" server "Database Address/Server" + (db-con-server? server)]) + + ;; Init Logging + (start-logger-thread (make-log-receiver (current-logger) 'info) + (make-log-receiver db-logger (loglevel-db?)) + (make-log-receiver layer2-logger (loglevel-layer2?)) + (make-log-receiver layer7-logger 'info)) + + ;; Test Database Connection + (when (not (db-pgc-test)) + (display "Failed to connect database") + (exit 1)) + + ;; Connect, replacing input with tcp connection + (if (or (null? (connect-host?)) (null? (connect-port?))) + (log-message (current-logger) 'info "Using stdin" #t) + (let-values ([(cin cout) (tcp-connect (connect-host?) (connect-port?))]) + (display (format "Connected to ~s ~s ~n" (connect-host?) (connect-port?))) + (current-input-port cin))) + + ;; Process Ebus Packets + (read-ebus-loop7 (current-input-port)) + ) + +(exit (main)) -- cgit v1.2.1