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