Class: NATS::Connection

Inherits:
Object
  • Object
show all
Defined in:
ext/nats/nats.c

Instance Method Summary collapse

Instance Method Details

#closeObject



31
32
33
34
35
36
# File 'ext/nats/nats.c', line 31

static VALUE ruby_nats_close(VALUE self) {
    Connection *conn = NULL;
    Data_Get_Struct(self, Connection, conn);
    natsConnection_Destroy(conn->conn);
    return self;
}

#connect(params) ⇒ Object



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
# File 'ext/nats/nats.c', line 38

static VALUE ruby_nats_connect(VALUE self, VALUE params) {
    Connection *conn = NULL;
    Data_Get_Struct(self, Connection, conn);

    natsOptions *opts = NULL;
    natsOptions_Create(&opts);
    natsOptions_SetErrorHandler(opts, ruby_nats_on_error, NULL);

    if (RTEST(params)) {
        VALUE val;

        val = rb_hash_aref(params, ID2SYM(rb_intern("allow_reconnect")));
        RTEST(val) && natsOptions_SetAllowReconnect(opts, RTEST(val));

        val = rb_hash_aref(params, ID2SYM(rb_intern("dont_randomize_servers")));
        RTEST(val) && natsOptions_SetNoRandomize(opts, RTEST(val));

        val = rb_hash_aref(params, ID2SYM(rb_intern("max_reconnect")));
        RTEST(val) && natsOptions_SetMaxReconnect(opts, NUM2INT(val));

        val = rb_hash_aref(params, ID2SYM(rb_intern("reconnect_time_wait")));
        RTEST(val) && natsOptions_SetReconnectWait(opts, NUM2INT(val));

        val = rb_hash_aref(params, ID2SYM(rb_intern("servers")));

        if (RTEST(val)) {
            int array_len = RARRAY_LEN(val);
            VALUE *val_arr = RARRAY_PTR(val);
            char **servers = malloc(sizeof(char*) * array_len);

            if (servers == NULL) {
                rb_raise(rb_eException, "Unable to allocate memory for server uri array");
            }

            for(int i = 0; i < array_len; i++) {
                char* uri = StringValuePtr(val_arr[i]);
                servers[i] = uri;
            }
            
            natsOptions_SetServers(opts, servers, array_len);
        }


        val = rb_hash_aref(params, ID2SYM(rb_intern("timeout")));
        RTEST(val) && natsOptions_SetTimeout(opts, NUM2INT(val));
    }

    natsStatus s = natsConnection_Connect(&(conn->conn), opts);

    if (s != NATS_OK) {
        ruby_nats_raise(s);
    }

    return self;
}

#publish(subject, data) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'ext/nats/nats.c', line 94

static VALUE ruby_nats_publish(VALUE self, VALUE subject, VALUE data) {
    Connection *conn = NULL;
    Data_Get_Struct(self, Connection, conn);
    natsStatus s = natsConnection_PublishString(
        conn->conn,
        StringValuePtr(subject),
        StringValuePtr(data));

    if (s == NATS_OK) {
        s = natsConnection_Flush(conn->conn);
    }

    if (s != NATS_OK) {
        ruby_nats_raise(s);
    }

    return self;
}

#subscribe(subject, callback) ⇒ Object



113
114
115
116
117
118
# File 'ext/nats/nats.c', line 113

static VALUE ruby_nats_subscribe(VALUE self, VALUE subject, VALUE callback) {
    Connection *conn = NULL;
    Data_Get_Struct(self, Connection, conn);
    rb_raise(rb_eException, "Not Implemented!");
    return self;
}

#subscribe_qg(identifier, subject, callback) ⇒ Object



120
121
122
123
124
125
126
127
128
129
# File 'ext/nats/nats.c', line 120

static VALUE ruby_nats_subscribe_queuegroup(
        VALUE self,
        VALUE identifier,
        VALUE subject,
        VALUE callback) {
    Connection *conn = NULL;
    Data_Get_Struct(self, Connection, conn);
    rb_raise(rb_eException, "Not Implemented!");
    return self;
}