/
agoo.c
123 lines (104 loc) · 2.83 KB
/
agoo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (c) 2018, Peter Ohler, All rights reserved.
#include <signal.h>
#include <stdio.h>
#include <ruby.h>
#include "debug.h"
#include "error_stream.h"
#include "graphql.h"
#include "log.h"
#include "pub.h"
#include "rack_logger.h"
#include "request.h"
#include "rresponse.h"
#include "rlog.h"
#include "rserver.h"
#include "rupgraded.h"
#include "server.h"
#include "upgraded.h"
extern void graphql_init(VALUE mod);
void
agoo_shutdown() {
rserver_shutdown(Qnil);
agoo_log_close();
}
/* Document-method: shutdown
*
* call-seq: shutdown()
*
* Shutdown the server and logger.
*/
static VALUE
ragoo_shutdown(VALUE self) {
agoo_shutdown();
gql_destroy();
debug_report();
return Qnil;
}
/* Document-method: publish
*
* call-seq: publish(subject, message)
*
* Publish a message on the given subject. A subject is normally a String but
* Symbols can also be used as can any other object that responds to #to_s.
*/
VALUE
ragoo_publish(VALUE self, VALUE subject, VALUE message) {
int slen;
const char *subj = extract_subject(subject, &slen);
rb_check_type(message, T_STRING);
agoo_server_publish(agoo_pub_publish(subj, slen, StringValuePtr(message), (int)RSTRING_LEN(message)));
return Qnil;
}
/* Document-method: unsubscribe
*
* call-seq: unsubscribe(subject)
*
* Unsubscribes on client listeners on the specified subject. Subjects are
* normally Strings but Symbols can also be used as can any other object that
* responds to #to_s.
*/
static VALUE
ragoo_unsubscribe(VALUE self, VALUE subject) {
rb_check_type(subject, T_STRING);
agoo_server_publish(agoo_pub_unsubscribe(NULL, StringValuePtr(subject), (int)RSTRING_LEN(subject)));
return Qnil;
}
static void
sig_handler(int sig) {
#ifdef MEM_DEBUG
rb_gc();
#endif
agoo_shutdown();
gql_destroy();
debug_report();
// Use exit instead of rb_exit as rb_exit segfaults most of the time.
//rb_exit(0);
exit(0);
}
/* Document-module: Agoo
*
* A High Performance HTTP Server that supports the Ruby rack API. The word
* agoo is a Japanese word for a type of flying fish.
*/
void
Init_agoo() {
VALUE mod = rb_define_module("Agoo");
rlog_init(mod);
error_stream_init(mod);
rack_logger_init(mod);
request_init(mod);
response_init(mod);
server_init(mod);
upgraded_init(mod);
graphql_init(mod);
rb_define_module_function(mod, "shutdown", ragoo_shutdown, 0);
rb_define_module_function(mod, "publish", ragoo_publish, 2);
rb_define_module_function(mod, "unsubscribe", ragoo_unsubscribe, 1);
signal(SIGINT, sig_handler);
signal(SIGTERM, sig_handler);
signal(SIGPIPE, SIG_IGN);
// This causes sleeps and queue pops to return immediately and is can be
// called all the time on mac OS with multiple threads. Something seems to
// get stuck.
signal(SIGVTALRM, SIG_IGN);
}