blob: 094b719e28cb8673705ef36b42afdbdbcd559373 [file] [log] [blame]
Brian Silverman6ae77dd2013-03-29 22:28:08 -07001#include "vision/BinaryServer.h"
2#include <stdio.h>
3#include <unistd.h>
4#include <stdlib.h>
5#include <sys/mman.h>
6#include <errno.h>
7#include <string.h>
8#include <vector>
9#include <netinet/in.h>
10#include <netinet/tcp.h>
11#include <arpa/inet.h>
12#include "aos/externals/libjpeg/include/jpeglib.h"
13
14#include "aos/atom_code/camera/Buffers.h"
15#include "aos/common/time.h"
16#include "opencv2/opencv.hpp"
17
18
19
20
21namespace frc971 {
22namespace vision {
23
24
25
26static void echo_read_cb(struct bufferevent *bev, void * /*ctx*/){
27 struct evbuffer *input = bufferevent_get_input(bev);
28 struct evbuffer *output = bufferevent_get_output(bev);
29
30 size_t len = evbuffer_get_length(input);
31 char *data;
32 data = (char *)malloc(len);
33 evbuffer_copyout(input, data, len);
34
35 printf("we got some data: %s\n", data);
36
37 evbuffer_add_buffer(output, input);
38}
39
40void BinaryServer::ErrorEvent(struct bufferevent *bev, short events){
41 if (events & BEV_EVENT_ERROR)
42 perror("Error from bufferevent");
43 if (events & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) {
44 have_id = false;
45 bufferevent_free(bev);
46 }
47}
48
49void BinaryServer::Accept(struct evconnlistener *listener,
50 evutil_socket_t fd, struct sockaddr * /*address*/, int /*socklen*/){
51 struct event_base *base = evconnlistener_get_base(listener);
52 if(!have_id){
53 struct bufferevent *bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
54 _output = bufferevent_get_output(bev);
55 _bufev = bev;
56 have_id = true;
57 int no_delay_flag = 1;
58 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &no_delay_flag, sizeof(no_delay_flag));
59
60 bufferevent_setcb(bev, echo_read_cb, NULL, StaticErrorEvent, this);
61
62 bufferevent_enable(bev, EV_READ | EV_WRITE);
63 }
64}
65static void accept_error_cb(struct evconnlistener *listener, void * /*ctx*/)
66{
67 struct event_base *base = evconnlistener_get_base(listener);
68 int err = EVUTIL_SOCKET_ERROR();
69 fprintf(stderr, "Got an error %d (%s) on the listener. "
70 "Shutting down.\n", err, evutil_socket_error_to_string(err));
71
72 event_base_loopexit(base, NULL);
73}
74
75void BinaryServer::StartServer(uint16_t port){
76 _fd = socket(AF_INET, SOCK_STREAM, 0);
77 struct sockaddr_in sin;
78 memset(&sin, 0, sizeof(sin));
79 sin.sin_family = AF_INET;
80 sin.sin_port = htons( port );
81 sin.sin_addr.s_addr = inet_addr("0.0.0.0");
82
83 listener = evconnlistener_new_bind(_base, StaticAccept, this,
84 LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE, -1,
85 (struct sockaddr *) (void *)&sin, sizeof(sin));
86
87 if (!listener) {
88 fprintf(stderr,"%s:%d: Couldn't create listener\n", __FILE__, __LINE__);
89 exit(-1);
90 }
91
92 evconnlistener_set_error_cb(listener, accept_error_cb);
93}
94
95void BinaryServer::Notify(int fd,short /*what*/){
96 char notes[4096];
97 int count = read(fd,notes,4096);
98 if(count == 0){
99 close(fd);
100 fprintf(stderr,"%s:%d: Error No cheeze from OpenCV task!!!\n",__FILE__,__LINE__);
101 exit(-1);
102 }
103 printf("notified!: %d\n",count);
104 if(have_id){
105 printf("got someone to read my stuff!\n");
106 char *binary_data;
107 size_t len;
108 if(_notify->GetData(&binary_data,&len)){
109 printf("here is for sending\n");
110 evbuffer_add_reference(_output, binary_data, len, PacketNotifier::StaticDataSent,_notify);
111 printf("this is how sending went %d\n",bufferevent_flush(_bufev,EV_WRITE, BEV_FLUSH));
112 }
113 }
114}
115
116//Constructor
117BinaryServer::BinaryServer(uint16_t port,
118 frc971::vision::PacketNotifier *notify) :
119 _base(event_base_new()) {
120 have_id = false;
121 StartServer(port);
122 _notify = notify;
123 frame_notify = event_new(_base, notify->RecieverFD(),
124 EV_READ|EV_PERSIST, StaticNotify, this);
125 event_add(frame_notify,NULL);
126 event_base_dispatch(_base);
127 }
128
129} // namespace vision
130} // namespace frc971