blob: c21a8b313c2351b019f4f235b20252cf9d1e745a [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 Silverman14fd0fb2014-01-14 21:42:01 -080015#include "aos/linux_code/camera/Buffers.h"
Brian Silverman6ae77dd2013-03-29 22:28:08 -070016#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 Silverman6742a442013-11-03 12:58:42 -080021static void echo_read_cb(struct bufferevent *bev, void * /*ctx*/) {
Brian Silverman6ae77dd2013-03-29 22:28:08 -070022 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
Brian Silverman6742a442013-11-03 12:58:42 -080035void BinaryServer::ErrorEvent(struct bufferevent *bev, short events) {
36 if (events & BEV_EVENT_ERROR) perror("Error from bufferevent");
Brian Silverman6ae77dd2013-03-29 22:28:08 -070037 if (events & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) {
38 have_id = false;
39 bufferevent_free(bev);
40 }
41}
42
Brian Silverman6742a442013-11-03 12:58:42 -080043void BinaryServer::Accept(struct evconnlistener *listener, evutil_socket_t fd,
44 struct sockaddr * /*address*/, int /*socklen*/) {
Brian Silverman6ae77dd2013-03-29 22:28:08 -070045 struct event_base *base = evconnlistener_get_base(listener);
Brian Silverman6742a442013-11-03 12:58:42 -080046 if (!have_id) {
47 struct bufferevent *bev =
48 bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
Brian Silverman6ae77dd2013-03-29 22:28:08 -070049 _output = bufferevent_get_output(bev);
50 _bufev = bev;
51 have_id = true;
52 int no_delay_flag = 1;
Brian Silverman6742a442013-11-03 12:58:42 -080053 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &no_delay_flag,
54 sizeof(no_delay_flag));
Brian Silverman6ae77dd2013-03-29 22:28:08 -070055
56 bufferevent_setcb(bev, echo_read_cb, NULL, StaticErrorEvent, this);
57
58 bufferevent_enable(bev, EV_READ | EV_WRITE);
59 }
60}
Brian Silverman6742a442013-11-03 12:58:42 -080061static void accept_error_cb(struct evconnlistener *listener, void * /*ctx*/) {
Brian Silverman6ae77dd2013-03-29 22:28:08 -070062 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. "
Brian Silverman6742a442013-11-03 12:58:42 -080065 "Shutting down.\n",
66 err, evutil_socket_error_to_string(err));
Brian Silverman6ae77dd2013-03-29 22:28:08 -070067
68 event_base_loopexit(base, NULL);
69}
70
Brian Silverman6742a442013-11-03 12:58:42 -080071void BinaryServer::StartServer(uint16_t port) {
Brian Silverman6ae77dd2013-03-29 22:28:08 -070072 _fd = socket(AF_INET, SOCK_STREAM, 0);
73 struct sockaddr_in sin;
74 memset(&sin, 0, sizeof(sin));
75 sin.sin_family = AF_INET;
Brian Silverman6742a442013-11-03 12:58:42 -080076 sin.sin_port = htons(port);
Brian Silverman6ae77dd2013-03-29 22:28:08 -070077 sin.sin_addr.s_addr = inet_addr("0.0.0.0");
78
Brian Silverman6742a442013-11-03 12:58:42 -080079 listener = evconnlistener_new_bind(
80 _base, StaticAccept, this, LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE, -1,
81 (struct sockaddr *)(void *)&sin, sizeof(sin));
Brian Silverman6ae77dd2013-03-29 22:28:08 -070082
83 if (!listener) {
Brian Silverman6742a442013-11-03 12:58:42 -080084 fprintf(stderr, "%s:%d: Couldn't create listener\n", __FILE__, __LINE__);
Brian Silverman6ae77dd2013-03-29 22:28:08 -070085 exit(-1);
86 }
87
88 evconnlistener_set_error_cb(listener, accept_error_cb);
89}
90
Brian Silverman6742a442013-11-03 12:58:42 -080091void BinaryServer::Notify(int fd, short /*what*/) {
Brian Silverman6ae77dd2013-03-29 22:28:08 -070092 char notes[4096];
Brian Silverman6742a442013-11-03 12:58:42 -080093 int count = read(fd, notes, 4096);
94 if (count == 0) {
Brian Silverman6ae77dd2013-03-29 22:28:08 -070095 close(fd);
Brian Silverman6742a442013-11-03 12:58:42 -080096 fprintf(stderr, "%s:%d: Error No cheeze from OpenCV task!!!\n", __FILE__,
97 __LINE__);
Brian Silverman6ae77dd2013-03-29 22:28:08 -070098 exit(-1);
99 }
Brian Silverman6742a442013-11-03 12:58:42 -0800100 printf("notified!: %d\n", count);
101 if (have_id) {
Brian Silverman6ae77dd2013-03-29 22:28:08 -0700102 printf("got someone to read my stuff!\n");
103 char *binary_data;
104 size_t len;
Brian Silverman6742a442013-11-03 12:58:42 -0800105 if (_notify->GetData(&binary_data, &len)) {
Brian Silverman6ae77dd2013-03-29 22:28:08 -0700106 printf("here is for sending\n");
Brian Silverman6742a442013-11-03 12:58:42 -0800107 evbuffer_add_reference(_output, binary_data, len,
108 PacketNotifier::StaticDataSent, _notify);
109 printf("this is how sending went %d\n",
110 bufferevent_flush(_bufev, EV_WRITE, BEV_FLUSH));
Brian Silverman6ae77dd2013-03-29 22:28:08 -0700111 }
112 }
113}
114
Brian Silverman6742a442013-11-03 12:58:42 -0800115// Constructor
116BinaryServer::BinaryServer(uint16_t port,
117 frc971::vision::PacketNotifier *notify)
118 : _base(event_base_new()) {
119 have_id = false;
120 StartServer(port);
121 _notify = notify;
122 frame_notify = event_new(_base, notify->RecieverFD(), EV_READ | EV_PERSIST,
123 StaticNotify, this);
124 event_add(frame_notify, NULL);
125 event_base_dispatch(_base);
126}
Brian Silverman6ae77dd2013-03-29 22:28:08 -0700127
128} // namespace vision
129} // namespace frc971