-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudi.cpp
2798 lines (2620 loc) · 92.4 KB
/
cloudi.cpp
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//-*-Mode:C++;coding:utf-8;tab-width:4;c-basic-offset:4;indent-tabs-mode:()-*-
// ex: set ft=cpp fenc=utf-8 sts=4 ts=4 sw=4 et nomod:
//
// MIT License
//
// Copyright (c) 2011-2023 Michael Truog <mjtruog at protonmail dot com>
//
// 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 "cloudi.hpp"
#undef CLOUDI_HPP // avoid header warning
#include "cloudi.h"
#define CLOUDI_HPP
#include "config.h"
#include "realloc_ptr.hpp"
#include "copy_ptr.hpp"
#include "timer.hpp"
#include "bind.hpp"
#include <unistd.h>
#include <errno.h>
#include <poll.h>
#include <ei.h>
#include <boost/shared_ptr.hpp>
#include <boost/unordered_map.hpp>
#include <boost/exception/all.hpp>
#define BACKTRACE_FRAMES 32
#define BACKTRACE_FRAME_OFFSET 2
#if defined(BACKTRACE_USE_BACKWARD)
#include <backward.hpp>
#elif defined(BACKTRACE_USE_BOOST)
#include <boost/stacktrace.hpp>
#elif defined(BACKTRACE_USE_BOOSTER)
#include <booster/backtrace.h>
#endif
#include <string>
#include <list>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <iostream>
#include "assert.hpp"
extern "C" {
static std::string backtrace_string()
{
#if defined(BACKTRACE_USE_BACKWARD)
std::ostringstream result;
backward::StackTrace backtrace;
backtrace.load_here(BACKTRACE_FRAMES);
backward::TraceResolver resolver;
result << "trace (most recent call last)";
unsigned int const thread_id = backtrace.thread_id();
if (thread_id)
{
result << " in thread " << thread_id << ":" << std::endl;
}
else
{
result << ":" << std::endl;
}
resolver.load_stacktrace(backtrace);
for (size_t i = BACKTRACE_FRAME_OFFSET; i < backtrace.size(); ++i)
{
backward::ResolvedTrace const & trace = resolver.resolve(backtrace[i]);
bool indented = true;
result << "#" <<
std::setfill(' ') << std::setw(2) << std::left <<
std::dec << (trace.idx - BACKTRACE_FRAME_OFFSET) << " ";
if (trace.source.filename.empty())
{
result <<
std::setfill(' ') << std::setw(18) << std::right <<
std::hex << trace.addr << " in " <<
trace.object_function << std::endl <<
" at " << trace.object_filename << std::endl;
indented = false;
}
for (size_t j = 0; j < trace.inliners.size(); ++j)
{
if (! indented)
result << " ";
backward::ResolvedTrace::SourceLoc const & location =
trace.inliners[j];
result <<
" (inlined) "
"in " << location.function << std::endl <<
" at " << location.filename << ":" <<
std::dec << location.line << std::endl;
indented = false;
}
if (! trace.source.filename.empty())
{
if (! indented)
result << " ";
result <<
std::setfill(' ') << std::setw(18) << std::right <<
std::hex << trace.addr << " in " <<
trace.source.function << std::endl <<
" at " << trace.source.filename << ":" <<
std::dec << trace.source.line << std::endl;
}
}
return result.str();
#elif defined(BACKTRACE_USE_BOOST)
std::ostringstream result;
result << "trace (most recent call last):" << std::endl;
boost::stacktrace::stacktrace const backtrace(BACKTRACE_FRAME_OFFSET,
BACKTRACE_FRAMES);
for (size_t i = 0; i < backtrace.size(); ++i)
{
boost::stacktrace::frame const & frame = backtrace[i];
result << "#" <<
std::setfill(' ') << std::setw(2) << std::left <<
std::dec << i << " " <<
std::setfill(' ') << std::setw(18) << std::right <<
std::hex << frame.address();
if (! frame.name().empty())
{
result << " in " << frame.name();
}
if (! frame.source_file().empty())
{
result << std::endl <<
" at " << frame.source_file() << ":" <<
std::dec << frame.source_line();
}
result << std::endl;
}
return result.str();
#elif defined(BACKTRACE_USE_BOOSTER)
std::ostringstream result;
booster::backtrace backtrace(BACKTRACE_FRAMES);
result << "trace (most recent call last):" << std::endl;
for (unsigned int i = BACKTRACE_FRAME_OFFSET;
i < backtrace.stack_size(); ++i)
{
result << "#" <<
std::setfill(' ') << std::setw(2) << std::left <<
std::dec << (i - BACKTRACE_FRAME_OFFSET) << " ";
backtrace.trace_line(i, result);
}
return result.str();
#else
return std::string("");
#endif
}
}
namespace
{
class callback_function
{
private:
class callback_function_c :
public CloudI::API::callback_function_generic
{
public:
callback_function_c(cloudi_instance_t * api,
cloudi_callback_t f) :
m_api(api), m_f(f) {}
virtual ~callback_function_c() throw() {}
virtual void operator () (int const request_type,
char const * const name,
char const * const pattern,
void const * const request_info,
uint32_t const request_info_size,
void const * const request,
uint32_t const request_size,
uint32_t timeout,
int8_t priority,
char const * const trans_id,
char const * const source,
uint32_t const source_size)
{
m_f(request_type,
name,
pattern,
request_info,
request_info_size,
request,
request_size,
timeout,
priority,
trans_id,
source,
source_size,
m_api->state,
m_api);
}
private:
cloudi_instance_t * m_api;
cloudi_callback_t m_f;
};
class callback_null_response :
public CloudI::API::callback_function_generic
{
public:
callback_null_response() {}
virtual ~callback_null_response() throw() {}
virtual void operator () (int const,
char const * const,
char const * const,
void const * const,
uint32_t const,
void const * const,
uint32_t const,
uint32_t,
int8_t,
char const * const,
char const * const,
uint32_t const)
{
}
};
public:
callback_function(CloudI::API::callback_function_generic * p) :
m_function(p) {}
callback_function(cloudi_instance_t * api,
cloudi_callback_t f) :
m_function(new callback_function_c(api, f)) {}
static callback_function null_response()
{
callback_function null_response(new callback_null_response());
return null_response;
}
void operator () (int const request_type,
char const * const name,
char const * const pattern,
void const * const request_info,
uint32_t const request_info_size,
void const * const request,
uint32_t const request_size,
uint32_t timeout,
int8_t const priority,
char const * const trans_id,
char const * const source,
uint32_t const source_size) const
{
(*m_function)(request_type,
name,
pattern,
request_info,
request_info_size,
request,
request_size,
timeout,
priority,
trans_id,
source,
source_size);
}
private:
boost::shared_ptr<CloudI::API::callback_function_generic>
m_function;
};
class callback_function_lookup
{
private:
class callback_function_queue
{
private:
typedef std::list<callback_function> queue_t;
public:
callback_function_queue(callback_function const & f) :
m_queue(new queue_t()),
m_size(1)
{
m_queue->push_back(f);
}
void push_back(callback_function const & f)
{
m_queue->push_back(f);
m_size++;
}
void pop_front()
{
m_queue->pop_front();
assert(m_size > 0);
m_size--;
}
bool empty() const
{
return (m_size == 0);
}
callback_function const & cycle()
{
queue_t & queue = *m_queue;
if (m_size == 1)
return queue.front();
queue.push_back(queue.front());
queue.pop_front();
return queue.back();
}
private:
boost::shared_ptr<queue_t> m_queue;
size_t m_size;
};
typedef boost::unordered_map<std::string, callback_function_queue>
lookup_queue_t;
typedef std::pair<std::string, callback_function_queue>
lookup_queue_pair_t;
public:
void insert(std::string const & pattern,
callback_function const & f)
{
lookup_queue_t::iterator itr = m_lookup.find(pattern);
if (itr == m_lookup.end())
{
m_lookup.insert(lookup_queue_pair_t(pattern, f));
}
else
{
itr->second.push_back(f);
}
}
bool remove(std::string const & pattern)
{
lookup_queue_t::iterator itr = m_lookup.find(pattern);
if (itr == m_lookup.end())
return false;
itr->second.pop_front();
if (itr->second.empty())
m_lookup.erase(itr);
return true;
}
callback_function find(std::string const & pattern)
{
lookup_queue_t::iterator itr = m_lookup.find(pattern);
if (itr == m_lookup.end())
return m_null_response;
return itr->second.cycle();
}
private:
static callback_function const m_null_response;
lookup_queue_t m_lookup;
};
callback_function const callback_function_lookup::m_null_response =
callback_function::null_response();
typedef callback_function_lookup lookup_t;
typedef realloc_ptr<char> buffer_t;
int errno_read()
{
switch (errno)
{
case EAGAIN:
return cloudi_error_read_EAGAIN;
case EBADF:
return cloudi_error_read_EBADF;
case EFAULT:
return cloudi_error_read_EFAULT;
case EINTR:
return cloudi_error_read_EINTR;
case EINVAL:
return cloudi_error_read_EINVAL;
case EIO:
return cloudi_error_read_EIO;
case EISDIR:
return cloudi_error_read_EISDIR;
default:
return cloudi_error_read_unknown;
}
}
int errno_write()
{
switch (errno)
{
case EAGAIN:
return cloudi_error_write_EAGAIN;
case EBADF:
return cloudi_error_write_EBADF;
case EFAULT:
return cloudi_error_write_EFAULT;
case EFBIG:
return cloudi_error_write_EFBIG;
case EINTR:
return cloudi_error_write_EINTR;
case EINVAL:
return cloudi_error_write_EINVAL;
case EIO:
return cloudi_error_write_EIO;
case ENOSPC:
return cloudi_error_write_ENOSPC;
case EPIPE:
return cloudi_error_write_EPIPE;
default:
return cloudi_error_write_unknown;
}
}
int errno_poll()
{
switch (errno)
{
case EBADF:
return cloudi_error_poll_EBADF;
case EFAULT:
return cloudi_error_poll_EFAULT;
case EINTR:
return cloudi_error_poll_EINTR;
case EINVAL:
return cloudi_error_poll_EINVAL;
case ENOMEM:
return cloudi_error_poll_ENOMEM;
default:
return cloudi_error_poll_unknown;
}
}
int data_ready(int fd, bool & ready)
{
struct pollfd fds[1] = {{fd, POLLIN | POLLPRI, 0}};
int const count = poll(fds, 1, 0);
if (count == -1)
return errno_poll();
ready = (count == 1);
return cloudi_success;
}
int read_exact(int fd,
unsigned char * const buffer,
uint32_t const length)
{
uint32_t total = 0;
while (total < length)
{
ssize_t const i = ::read(fd, buffer + total, length - total);
if (i <= 0)
{
if (i == -1)
return errno_read();
else
return cloudi_error_read_null;
}
total += i;
}
if (total > length)
return cloudi_error_read_overflow;
return cloudi_success;
}
int read_all(int fd, int const use_header,
buffer_t & buffer, uint32_t & total,
uint32_t const buffer_size)
{
total = 0;
if (use_header)
{
unsigned char header[4];
int const status = read_exact(fd, header, 4);
if (status)
return status;
uint32_t const length = (header[0] << 24) |
(header[1] << 16) |
(header[2] << 8) |
header[3];
if (buffer.reserve(length) == false)
return cloudi_out_of_memory;
total = length;
return read_exact(fd, buffer.get<unsigned char>(), length);
}
else
{
bool ready = true;
while (ready)
{
if (buffer.reserve(total + buffer_size) == false)
return cloudi_out_of_memory;
ssize_t i = ::read(fd, &buffer[total], buffer_size);
if (i < 0)
return errno_read();
total += i;
ready = (i == static_cast<signed>(buffer_size)) ||
(i == 0 && total == 0);
if (ready)
{
int const status = data_ready(fd, ready);
if (status)
return status;
}
}
}
return cloudi_success;
}
int write_exact(int fd, int const use_header,
char * const buffer, uint32_t const length)
{
if (use_header)
{
uint32_t const length_body = length - 4;
buffer[0] = (length_body & 0xff000000) >> 24;
buffer[1] = (length_body & 0x00ff0000) >> 16;
buffer[2] = (length_body & 0x0000ff00) >> 8;
buffer[3] = length_body & 0x000000ff;
}
uint32_t total = 0;
while (total < length)
{
ssize_t const i = ::write(fd, buffer + total, length - total);
if (i <= 0)
{
if (i == -1)
return errno_write();
else
return cloudi_error_write_null;
}
total += i;
}
if (total > length)
return cloudi_error_write_overflow;
return cloudi_success;
}
} // anonymous namespace
extern "C" {
static void exit_handler()
{
std::cout.flush();
std::cerr.flush();
std::clog.flush();
::fflush(stdout);
::fflush(stderr);
}
static int poll_request(cloudi_instance_t * api,
int timeout,
int external);
static int getenv_to_uint32(char const * const name,
uint32_t * const value)
{
char const * const value_str = ::getenv(name);
if (value_str == 0)
return cloudi_invalid_input;
int const value_int = ::atoi(value_str);
if (value_int < 0)
return cloudi_invalid_input;
*value = static_cast<uint32_t>(value_int);
return cloudi_success;
}
int cloudi_initialize(cloudi_instance_t * api,
unsigned int const thread_index,
void * state)
{
if (api == 0)
return cloudi_out_of_memory;
char const * const protocol = ::getenv("CLOUDI_API_INIT_PROTOCOL");
if (protocol == 0)
{
std::cerr <<
"CloudI service execution must occur in CloudI" << std::endl;
return cloudi_invalid_input;
}
uint32_t buffer_size;
int result = getenv_to_uint32("CLOUDI_API_INIT_BUFFER_SIZE", &buffer_size);
if (result)
return result;
::memset(api, 0, sizeof(cloudi_instance_t));
api->state = state;
if (::strcmp(protocol, "tcp") == 0)
{
api->fd = thread_index + 3;
api->use_header = 1;
}
else if (::strcmp(protocol, "udp") == 0)
{
api->fd = thread_index + 3;
//api->use_header = 0;
}
else if (::strcmp(protocol, "local") == 0)
{
api->fd = thread_index + 3;
api->use_header = 1;
}
else
{
return cloudi_invalid_input;
}
//api->initialization_complete = 0;
//api->terminate = 0;
//api->cxx_terminate_exception = 0;
//api->free_with_delete = 0;
//api->free_name = 0;
//api->free_pattern = 0;
//api->free_request_info = 0;
//api->free_request = 0;
//api->free_response_info = 0;
//api->free_response = 0;
api->buffer_size = buffer_size;
api->lookup = new lookup_t();
api->buffer_send = new buffer_t(32768, CLOUDI_MAX_BUFFERSIZE);
api->buffer_recv = new buffer_t(32768, CLOUDI_MAX_BUFFERSIZE);
//api->buffer_recv_index = 0;
api->buffer_call = new buffer_t(32768, CLOUDI_MAX_BUFFERSIZE);
api->poll_timer = new timer();
//api->prefix = 0;
api->timeout_terminate = 10; // TIMEOUT_TERMINATE_MIN
// termination handling
::atexit(&exit_handler);
assert_initialize();
// unbuffered stdout/stderr
std::cout.setf(std::ios::unitbuf);
::setvbuf(stdout, NULL, _IONBF, 0);
::setvbuf(stderr, NULL, _IONBF, 0);
// attempt initialization
buffer_t & buffer = *reinterpret_cast<buffer_t *>(api->buffer_send);
int index = 0;
if (api->use_header)
index = 4;
if (ei_encode_version(buffer.get<char>(), &index))
return cloudi_error_ei_encode;
if (ei_encode_atom(buffer.get<char>(), &index, "init"))
return cloudi_error_ei_encode;
result = write_exact(api->fd, api->use_header,
buffer.get<char>(), index);
if (result)
return result;
while (cloudi_timeout == (result = poll_request(api, 1000, 0)))
{
}
return result;
}
void * cloudi_destroy(cloudi_instance_t * api)
{
if (api && api->fd)
{
::close(api->fd);
delete reinterpret_cast<lookup_t *>(api->lookup);
delete reinterpret_cast<buffer_t *>(api->buffer_send);
delete reinterpret_cast<buffer_t *>(api->buffer_recv);
delete reinterpret_cast<buffer_t *>(api->buffer_call);
delete reinterpret_cast<timer *>(api->poll_timer);
if (api->prefix)
delete [] api->prefix;
return api->state;
}
return 0;
}
int cloudi_initialize_thread_count(unsigned int * const thread_count)
{
uint32_t value = 0;
int result = getenv_to_uint32("CLOUDI_API_INIT_THREAD_COUNT", &value);
if (result)
return result;
*thread_count = value;
return cloudi_success;
}
static int cloudi_subscribe_(cloudi_instance_t * api,
char const * const pattern,
callback_function const & f)
{
lookup_t & lookup = *reinterpret_cast<lookup_t *>(api->lookup);
lookup.insert(std::string(api->prefix) + pattern, f);
buffer_t & buffer = *reinterpret_cast<buffer_t *>(api->buffer_send);
int index = 0;
if (api->use_header)
index = 4;
if (ei_encode_version(buffer.get<char>(), &index))
return cloudi_error_ei_encode;
if (ei_encode_tuple_header(buffer.get<char>(), &index, 2))
return cloudi_error_ei_encode;
if (ei_encode_atom(buffer.get<char>(), &index, "subscribe"))
return cloudi_error_ei_encode;
if (buffer.reserve(index + strlen(pattern) + 128) == false)
return cloudi_error_write_overflow;
if (ei_encode_string(buffer.get<char>(), &index, pattern))
return cloudi_error_ei_encode;
int result = write_exact(api->fd, api->use_header,
buffer.get<char>(), index);
if (result)
return result;
return cloudi_success;
}
int cloudi_subscribe(cloudi_instance_t * api,
char const * const pattern,
cloudi_callback_t f)
{
return cloudi_subscribe_(api,
pattern,
callback_function(api, f));
}
int cloudi_subscribe_count(cloudi_instance_t * api,
char const * const pattern)
{
buffer_t & buffer = *reinterpret_cast<buffer_t *>(api->buffer_send);
int index = 0;
if (api->use_header)
index = 4;
if (ei_encode_version(buffer.get<char>(), &index))
return cloudi_error_ei_encode;
if (ei_encode_tuple_header(buffer.get<char>(), &index, 2))
return cloudi_error_ei_encode;
if (ei_encode_atom(buffer.get<char>(), &index, "subscribe_count"))
return cloudi_error_ei_encode;
if (buffer.reserve(index + strlen(pattern) + 128) == false)
return cloudi_error_write_overflow;
if (ei_encode_string(buffer.get<char>(), &index, pattern))
return cloudi_error_ei_encode;
int result = write_exact(api->fd, api->use_header,
buffer.get<char>(), index);
if (result)
return result;
result = poll_request(api, -1, 0);
if (result)
return result;
return cloudi_success;
}
int cloudi_unsubscribe(cloudi_instance_t * api,
char const * const pattern)
{
std::string str(api->prefix);
str += pattern;
lookup_t & lookup = *reinterpret_cast<lookup_t *>(api->lookup);
if (lookup.remove(str))
{
buffer_t & buffer = *reinterpret_cast<buffer_t *>(api->buffer_send);
int index = 0;
if (api->use_header)
index = 4;
if (ei_encode_version(buffer.get<char>(), &index))
return cloudi_error_ei_encode;
if (ei_encode_tuple_header(buffer.get<char>(), &index, 2))
return cloudi_error_ei_encode;
if (ei_encode_atom(buffer.get<char>(), &index, "unsubscribe"))
return cloudi_error_ei_encode;
if (buffer.reserve(index + strlen(pattern) + 128) == false)
return cloudi_error_write_overflow;
if (ei_encode_string(buffer.get<char>(), &index, pattern))
return cloudi_error_ei_encode;
int result = write_exact(api->fd, api->use_header,
buffer.get<char>(), index);
if (result)
return result;
return cloudi_success;
}
else
{
return cloudi_error_function_parameter;
}
}
static int cloudi_send_(cloudi_instance_t * api,
char const * const command_name,
char const * const name,
void const * const request_info,
uint32_t const request_info_size,
void const * const request,
uint32_t const request_size,
uint32_t timeout,
int8_t const priority)
{
buffer_t & buffer = *reinterpret_cast<buffer_t *>(api->buffer_send);
int index = 0;
if (api->use_header)
index = 4;
if (ei_encode_version(buffer.get<char>(), &index))
return cloudi_error_ei_encode;
if (ei_encode_tuple_header(buffer.get<char>(), &index, 6))
return cloudi_error_ei_encode;
if (ei_encode_atom(buffer.get<char>(), &index, command_name))
return cloudi_error_ei_encode;
if (buffer.reserve(index + strlen(name) +
request_info_size + request_size + 128) == false)
return cloudi_error_write_overflow;
if (ei_encode_string(buffer.get<char>(), &index, name))
return cloudi_error_ei_encode;
if (ei_encode_binary(buffer.get<char>(), &index,
request_info, request_info_size))
return cloudi_error_ei_encode;
if (ei_encode_binary(buffer.get<char>(), &index, request, request_size))
return cloudi_error_ei_encode;
if (ei_encode_ulong(buffer.get<char>(), &index, timeout))
return cloudi_error_ei_encode;
if (ei_encode_long(buffer.get<char>(), &index, priority))
return cloudi_error_ei_encode;
int result = write_exact(api->fd, api->use_header,
buffer.get<char>(), index);
if (result)
return result;
result = poll_request(api, -1, 0);
if (result)
return result;
return cloudi_success;
}
int cloudi_send_async(cloudi_instance_t * api,
char const * const name,
void const * const request,
uint32_t const request_size)
{
return cloudi_send_(api, "send_async", name, "", 0,
request, request_size,
api->timeout_async, api->priority_default);
}
int cloudi_send_async_(cloudi_instance_t * api,
char const * const name,
void const * const request_info,
uint32_t const request_info_size,
void const * const request,
uint32_t const request_size,
uint32_t timeout,
int8_t const priority)
{
if (timeout == 0)
timeout = api->timeout_async;
return cloudi_send_(api, "send_async", name,
request_info, request_info_size,
request, request_size, timeout, priority);
}
int cloudi_send_sync(cloudi_instance_t * api,
char const * const name,
void const * const request,
uint32_t const request_size)
{
return cloudi_send_(api, "send_sync", name, "", 0,
request, request_size,
api->timeout_sync, api->priority_default);
}
int cloudi_send_sync_(cloudi_instance_t * api,
char const * const name,
void const * const request_info,
uint32_t const request_info_size,
void const * const request,
uint32_t const request_size,
uint32_t timeout,
int8_t const priority)
{
if (timeout == 0)
timeout = api->timeout_sync;
return cloudi_send_(api, "send_sync", name,
request_info, request_info_size,
request, request_size, timeout, priority);
}
int cloudi_mcast_async(cloudi_instance_t * api,
char const * const name,
void const * const request,
uint32_t const request_size)
{
return cloudi_send_(api, "mcast_async", name, "", 0,
request, request_size,
api->timeout_async, api->priority_default);
}
int cloudi_mcast_async_(cloudi_instance_t * api,
char const * const name,
void const * const request_info,
uint32_t const request_info_size,
void const * const request,
uint32_t const request_size,
uint32_t timeout,
int8_t const priority)
{
if (timeout == 0)
timeout = api->timeout_async;
return cloudi_send_(api, "mcast_async", name,
request_info, request_info_size,
request, request_size, timeout, priority);
}
static void cloudi_forward_free(cloudi_instance_t * api,
char const * const name,
void const * const request_info,
void const * const request)
{
if (api->free_name)
{
char * const name_p = const_cast<char *>(name);
if (api->free_with_delete)
delete [] name_p;
else
::free(name_p);
api->free_name = 0;
}
assert(api->free_pattern == 0);
if (api->free_request_info)
{
char * const request_info_p =
const_cast<char *>(reinterpret_cast<char const *>(request_info));
if (api->free_with_delete)
delete [] request_info_p;
else
::free(request_info_p);
api->free_request_info = 0;
}
if (api->free_request)
{
char * const request_p =
const_cast<char *>(reinterpret_cast<char const *>(request));
if (api->free_with_delete)
delete [] request_p;
else
::free(request_p);
api->free_request = 0;
}
assert(api->free_response_info == 0);
assert(api->free_response == 0);
}
static int cloudi_forward_(cloudi_instance_t * api,
char const * const command_name,
char const * const name,
void const * const request_info,
uint32_t const request_info_size,
void const * const request,
uint32_t const request_size,
uint32_t timeout,
int8_t const priority,
char const * const trans_id,
char const * const source,
uint32_t const source_size)
{
buffer_t & buffer = *reinterpret_cast<buffer_t *>(api->buffer_send);
int index = 0;
if (api->use_header)
index = 4;
if (ei_encode_version(buffer.get<char>(), &index))
return cloudi_error_ei_encode;
if (ei_encode_tuple_header(buffer.get<char>(), &index, 8))
return cloudi_error_ei_encode;
if (ei_encode_atom(buffer.get<char>(), &index, command_name))
return cloudi_error_ei_encode;
if (buffer.reserve(index + strlen(name) +
request_info_size + request_size + 128) == false)
return cloudi_error_write_overflow;
if (ei_encode_string(buffer.get<char>(), &index, name))
return cloudi_error_ei_encode;
if (ei_encode_binary(buffer.get<char>(), &index,
request_info, request_info_size))
return cloudi_error_ei_encode;
if (ei_encode_binary(buffer.get<char>(), &index, request, request_size))
return cloudi_error_ei_encode;
if (ei_encode_ulong(buffer.get<char>(), &index, timeout))
return cloudi_error_ei_encode;
if (ei_encode_long(buffer.get<char>(), &index, priority))
return cloudi_error_ei_encode;
if (ei_encode_binary(buffer.get<char>(), &index, trans_id, 16))
return cloudi_error_ei_encode;
int version;