/** filename.cpp
 Summary: quick summary
 Usage:   ./filename switches
**/
using namespace std;
#include <iostream>
#include "settings.h"
#define TIMER_RATE 10 // in Hz
void pcap_handler2(u_char *args, const struct pcap_pkthdr *header, const u_char *packet);
void timer_handler(int x);
struct pcap *handle; // handle to the pcap object

int s; struct sockaddr_in si; // my socket

int main(int argc, char *argv[]) {
	heartbeat_open(PROG_ID, "filename"); // listen for heartbeat requests, see settings.h for PROG_ defines

	// set up input socket(s)
	string ports = ""; // put all the ports here, you'll have to put a switch() on the .port in the callback
	ports = ports + "    udp port " + itoa(PORT_HEARTBEAT_REQ); // required
	ports = ports + " or udp port " + itoa(PORT_QUIT); // required
	ports = ports + " or udp port " + itoa(PORT_GPS); // ports you want to listen to
	pcap_init(&handle, DEVICE, ports); // start listening
	
	// set up output socket(s)
	s = socket_open(&si, PORT_NUMBER); // initialize output socket (make multiple of these to send on multiple ports)

	// set up timer
	struct itimerval timer1;  // set up the timers
	signal(SIGALRM, timer_handler); // call this function on rollover
	timer1.it_interval.tv_sec  = 0;     // reset val
	timer1.it_interval.tv_usec = (__suseconds_t)((1.0/(double)TIMER_RATE)*1000000); // reset val (converts UPDATE_RATE from Hz into microseconds
	timer1.it_value.tv_sec     = timer1.it_interval.tv_sec;  // initial val
	timer1.it_value.tv_usec    = timer1.it_interval.tv_usec; // initial val
	setitimer(ITIMER_REAL, &timer1, NULL); // apply new settings

	
	printf("\ntemplate: started\n");

	char buf[5] = "test";
	socket_send(s, buf, si); // send a test packet
	
	// ** pcap_loop will sit here and block *forever*.  Put all your code in the timer callback
	pcap_loop(handle, -1, pcap_handler2, NULL); // callback pcap_handler2 on new packet

	pcap_close(handle);
	printf("\ntemplate: terminated\n");
	return 0;
}




void timer_handler(int x) {
	signal(SIGALRM, timer_handler); // reset, so it timers more than once
	// handle timer event
}



void pcap_handler2(u_char *args, const struct pcap_pkthdr *header, const u_char *packet) {
	s_pcap_packet pcap_packet = pcap_decode(packet);
	if (pcap_packet.port == PORT_HEARTBEAT_REQ) { // required
		heartbeat(pcap_packet.payload); // reply to request
	} else if (pcap_packet.port == PORT_QUIT) { // quit on ::quit:: message
		if (strlen(pcap_packet.payload) == 8) { // ::quit::
			if (pcap_packet.payload[0] == ':' && pcap_packet.payload[1] == ':' && pcap_packet.payload[2] == 'q' && pcap_packet.payload[3] == 'u' && pcap_packet.payload[4] == 'i' && pcap_packet.payload[5] == 't' && pcap_packet.payload[6] == ':' && pcap_packet.payload[7] == ':') {
				pcap_breakloop(handle);
			}
		}
	} else {
		// packet received
		// pcap_packet.port     // the port we received the data on
		// pcap_packet.payload  // the data passed
	}
}
