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