Skip to content

Instantly share code, notes, and snippets.

@ngerakines
Created March 5, 2010 22:40
Show Gist options
  • Select an option

  • Save ngerakines/323251 to your computer and use it in GitHub Desktop.

Select an option

Save ngerakines/323251 to your computer and use it in GitHub Desktop.
nowheremachine:heman nick$ make
make all-recursive
Making all in src
gcc -DHAVE_CONFIG_H -I. -I.. -Wall --std=c99 -fgnu89-inline -g -O2 -MT heman-heman.o -MD -MP -MF .deps/heman-heman.Tpo -c -o heman-heman.o `test -f 'heman.c' || echo './'`heman.c
heman.c: In function ‘on_read’:
heman.c:82: error: ‘bert_data_t’ has no member named ‘tuple’
make[2]: *** [heman-heman.o] Error 1
make[1]: *** [all-recursive] Error 1
make: *** [all] Error 2
/*
Copyright (c) 2010 Nick Gerakines <nick at gerakines dot net>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <arpa/inet.h>
#include <assert.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <netinet/in.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <event.h>
#include <bert.h>
#include "heman.h"
#include "stats.h"
#include "rules.h"
// TODO: Parse incoming commands here.
void on_read(int fd, short ev, void *arg) {
struct client *client = (struct client *)arg;
unsigned char buffer[1024];
int len = read(fd, buffer, sizeof(buffer));
if (len == 0) {
close(fd);
event_del(&client->ev_read);
free(client);
return;
} else if (len < 0) {
printf("Socket failure, disconnecting client: %s", strerror(errno));
close(fd);
event_del(&client->ev_read);
free(client);
return;
}
printf("incoming data: '%s'\n", buffer);
bert_decoder_t *decoder;
decoder = bert_decoder_create();
bert_decoder_buffer(decoder, buffer, sizeof(buffer));
bert_data_t *data;
int result;
if ((result = bert_decoder_pull(decoder, &data)) != 1) {
printf("bert error: %s", bert_strerror(result));
}
if (data->type != bert_data_tuple) {
printf("Type is not a tuple: %d", data->type);
// test_fail("bert_decoder_next did not decode a tuple");
}
printf("data type is %d.\n", data->type);
printf("receieved a tuple of %d length.\n", data->tuple->length);
bert_decoder_destroy(decoder);
// {request, foo}
reply(fd, "OK");
}
void on_accept(int fd, short ev, void *arg) {
int client_fd;
struct sockaddr_in client_addr;
socklen_t client_len = sizeof(client_addr);
struct client *client;
client_fd = accept(fd, (struct sockaddr *)&client_addr, &client_len);
if (client_fd == -1) {
warn("accept failed");
return;
}
if (setnonblock(client_fd) < 0) {
warn("failed to set client socket non-blocking");
}
client = calloc(1, sizeof(*client));
if (client == NULL) {
err(1, "malloc failed");
}
event_set(&client->ev_read, client_fd, EV_READ|EV_PERSIST, on_read, client);
event_add(&client->ev_read, NULL);
}
int main(int argc, char **argv) {
int listen_fd;
struct sockaddr_in listen_addr;
int reuseaddr_on = 1;
struct event ev_accept;
event_init();
listen_fd = socket(AF_INET, SOCK_STREAM, 0);
if (listen_fd < 0) { err(1, "listen failed"); }
if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr_on, sizeof(reuseaddr_on)) == -1) { err(1, "setsockopt failed"); }
memset(&listen_addr, 0, sizeof(listen_addr));
listen_addr.sin_family = AF_INET;
listen_addr.sin_addr.s_addr = INADDR_ANY;
listen_addr.sin_port = htons(8003);
if (bind(listen_fd, (struct sockaddr *)&listen_addr, sizeof(listen_addr)) < 0) { err(1, "bind failed"); }
if (listen(listen_fd, 5) < 0) { err(1, "listen failed"); }
if (setnonblock(listen_fd) < 0) { err(1, "failed to set server socket to non-blocking"); }
event_set(&ev_accept, listen_fd, EV_READ|EV_PERSIST, on_accept, NULL);
event_add(&ev_accept, NULL);
event_dispatch();
return 0;
}
void reply(int fd, char *buffer) {
int n = write(fd, buffer, strlen(buffer));
if (n < 0 || n < strlen(buffer)) {
printf("ERROR writing to socket");
}
}
int setnonblock(int fd) {
int flags;
flags = fcntl(fd, F_GETFL);
if (flags < 0) {
return flags;
}
flags |= O_NONBLOCK;
if (fcntl(fd, F_SETFL, flags) < 0) {
return -1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment