<commit_before>package main

import (
	"encoding/binary"
	"fmt"
	"flag"
	"log"
	"log/syslog"
	mrand "math/rand"
	"net"
	"net/http"
	"crypto/rand"
	"strings"
	"sync"
	"time"
)

type RtpMsg struct {
	sn uint16
	delay uint32
}

type RtpStats struct {
	unixtime int64
	recv uint16
	ooo uint16 // Out Of Order packets
	delay uint32
	last_sn uint16
}

var rtpproxyAddr string
var rtpproxyPort uint
var listenPort uint
var payloadSize uint
var payloadType uint
var histSize uint
var histTime uint
var window *MovingWindow
var currRtpStats RtpStats

func init() {
	flag.UintVar(&listenPort, "hport", 8080, "Port to run HTTP server at")
	flag.UintVar(&histSize, "hsize", 10, "History backlog size (in steps)")
	flag.UintVar(&histTime, "htime", 60, "History interval (in seconds)")
	flag.StringVar(&rtpproxyAddr, "rhost", "127.0.0.1", "RTPproxy address")
	flag.UintVar(&rtpproxyPort, "rport", 22222, "RTPproxy port")
	flag.UintVar(&payloadSize, "psize", 160, "RTP payload size (in bytes)")
	flag.UintVar(&payloadType, "ptype", 8, "RTP payload type")
}

func viewHandlerRobo(w http.ResponseWriter, r *http.Request) {
	log.Printf("ROBO FROM: %s REQ: %s\n", r.RemoteAddr, r.URL.Path)
	var retStr string
	retStr += "["

	for i := range window.arr {
		retStr += fmt.Sprintf("{\"unixtime\":%d,\"received\":%d,\"ooo\":%d,\"delay\":%d},",
		window.arr[i].unixtime,
		window.arr[i].recv,
		window.arr[i].ooo,
		window.arr[i].delay)
	}

	retStr += "{}]"
	fmt.Fprintf(w, retStr)
}
func viewHandlerHuman(w http.ResponseWriter, r *http.Request) {
	log.Printf("FROM: %s REQ: %s\n", r.RemoteAddr, r.URL.Path)

	var retStr string

	retStr += "<html lang=\"en\"><head></head><body><table>"

	for i := range window.arr {
		retStr += fmt.Sprintf("<tr><td>%d</td><td>%d</td><td>%d</td><td>%d</td></tr>",
		window.arr[i].unixtime,
		window.arr[i].recv,
		window.arr[i].ooo,
		window.arr[i].delay)
	}

	fmt.Fprintf(w, "<html lang=\"en\"><head></head><body><table>%s</table></head></html>", retStr)
}

// Number of seconds ellapsed from 1900 to 1970, see RFC 5905
const ntpEpochOffset = 2208988800

func getNtpStamp() uint32 {
	tm := time.Now().UnixNano()
	seconds := uint32(tm/1e9 + ntpEpochOffset) // Go uses ns, thus divide by 1e9 to get seconds
	msecs := (seconds % 1e6) * 8000 // 8 KHz
	return msecs + uint32((tm % 1e9) / (1e6 / 8)) // 8 KHz
}

func getRandStr(n uint) string {
	const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
	var bytes = make([]byte, n)
	rand.Read(bytes)
	for i, b := range bytes {
		bytes[i] = alphanum[b % byte(len(alphanum))]
	}
	return string(bytes)
}

func makeCon(name string, haddr string, hport string) *net.UDPConn {
	addr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%s", haddr, hport))
	if err != nil {
		log.Printf("ERR1: udp:%s:%s: %v\n", haddr, hport, err)
		return nil
	}

	con, err := net.DialUDP("udp", nil, addr)
	if err != nil {
		log.Printf("ERR2: udp:%s:%s: %v\n", haddr, hport, err)
		return nil
	}
	log.Printf("Connected %s: [udp:%s:%s]\n", name, haddr, hport)
	return con
}

func main() {
	logger, _ := syslog.New(syslog.LOG_INFO, "rtpproxy_monitoring")
	log.SetOutput(logger)

	flag.Parse()

	const delay = 20 * time.Millisecond

	var n int

	window = New(int(histSize), 1)

	ca := make(chan RtpMsg)
	cb := make(chan RtpMsg)

	// Buffer for the control connection (~1 MTU)
	var buffer [1500]byte

	const rtpHeader uint = 0x80
	const rtpHeaderSize uint = 12

	var apayload []byte = make([]byte, rtpHeaderSize + payloadSize)<commit_msg>Allow disabling log to syslog<commit_after>package main

import (
	"encoding/binary"
	"fmt"
	"flag"
	"log"
	"log/syslog"
	mrand "math/rand"
	"net"
	"net/http"
	"crypto/rand"
	"strings"
	"sync"
	"time"
)

type RtpMsg struct {
	sn uint16
	delay uint32
}

type RtpStats struct {
	unixtime int64
	recv uint16
	ooo uint16 // Out Of Order packets
	delay uint32
	last_sn uint16
}

var rtpproxyAddr string
var rtpproxyPort uint
var listenPort uint
var payloadSize uint
var payloadType uint
var histSize uint
var histTime uint
var syslogLog bool

var window *MovingWindow
var currRtpStats RtpStats

func init() {
	flag.UintVar(&listenPort, "hport", 8080, "Port to run HTTP server at")
	flag.UintVar(&histSize, "hsize", 10, "History backlog size (in steps)")
	flag.UintVar(&histTime, "htime", 60, "History interval (in seconds)")
	flag.StringVar(&rtpproxyAddr, "rhost", "127.0.0.1", "RTPproxy address")
	flag.UintVar(&rtpproxyPort, "rport", 22222, "RTPproxy port")
	flag.UintVar(&payloadSize, "psize", 160, "RTP payload size (in bytes)")
	flag.UintVar(&payloadType, "ptype", 8, "RTP payload type")
	flag.BoolVar(&syslogLog, "syslog", false, "Log to syslog")
}

func viewHandlerRobo(w http.ResponseWriter, r *http.Request) {
	log.Printf("ROBO FROM: %s REQ: %s\n", r.RemoteAddr, r.URL.Path)
	var retStr string
	retStr += "["

	for i := range window.arr {
		retStr += fmt.Sprintf("{\"unixtime\":%d,\"received\":%d,\"ooo\":%d,\"delay\":%d},",
		window.arr[i].unixtime,
		window.arr[i].recv,
		window.arr[i].ooo,
		window.arr[i].delay)
	}

	retStr += "{}]"
	fmt.Fprintf(w, retStr)
}
func viewHandlerHuman(w http.ResponseWriter, r *http.Request) {
	log.Printf("FROM: %s REQ: %s\n", r.RemoteAddr, r.URL.Path)

	var retStr string

	retStr += "<html lang=\"en\"><head></head><body><table>"

	for i := range window.arr {
		retStr += fmt.Sprintf("<tr><td>%d</td><td>%d</td><td>%d</td><td>%d</td></tr>",
		window.arr[i].unixtime,
		window.arr[i].recv,
		window.arr[i].ooo,
		window.arr[i].delay)
	}

	fmt.Fprintf(w, "<html lang=\"en\"><head></head><body><table>%s</table></head></html>", retStr)
}

// Number of seconds ellapsed from 1900 to 1970, see RFC 5905
const ntpEpochOffset = 2208988800

func getNtpStamp() uint32 {
	tm := time.Now().UnixNano()
	seconds := uint32(tm/1e9 + ntpEpochOffset) // Go uses ns, thus divide by 1e9 to get seconds
	msecs := (seconds % 1e6) * 8000 // 8 KHz
	return msecs + uint32((tm % 1e9) / (1e6 / 8)) // 8 KHz
}

func getRandStr(n uint) string {
	const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
	var bytes = make([]byte, n)
	rand.Read(bytes)
	for i, b := range bytes {
		bytes[i] = alphanum[b % byte(len(alphanum))]
	}
	return string(bytes)
}

func makeCon(name string, haddr string, hport string) *net.UDPConn {
	addr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%s", haddr, hport))
	if err != nil {
		log.Printf("ERR1: udp:%s:%s: %v\n", haddr, hport, err)
		return nil
	}

	con, err := net.DialUDP("udp", nil, addr)
	if err != nil {
		log.Printf("ERR2: udp:%s:%s: %v\n", haddr, hport, err)
		return nil
	}
	log.Printf("Connected %s: [udp:%s:%s]\n", name, haddr, hport)
	return con
}

func main() {
	flag.Parse()

	if syslogLog {
		logger, _ := syslog.New(syslog.LOG_INFO, "rtpproxy_monitoring")
		log.SetOutput(logger)
	}

	const delay = 20 * time.Millisecond

	var n int

	window = New(int(histSize), 1)

	ca := make(chan RtpMsg)
	cb := make(chan RtpMsg)

	// Buffer for the control connection (~1 MTU)
	var buffer [1500]byte

	const rtpHeader uint = 0x80
	const rtpHeaderSize uint = 12

	var apayload []byte = make([]byte, rtpHeaderSize + payloadSize)
{'fraction_non_alphanumeric': 0.17011980267794222, 'fraction_numerical': 0.030162085976039463, 'mean_word_length': 4.379833206974981, 'pattern_counts': {'":': 8, '<': 49, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5593628', 'n_tokens_mistral': 3055, 'n_tokens_neox': 2848, 'n_words': 696}