-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rconnection2.h
854 lines (695 loc) · 24 KB
/
Rconnection2.h
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
/*
* C++ Interface to Rserve
* Copyright (C) 2004-8 Simon Urbanek, All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; version 2.1 of the License
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Leser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Although this code is licensed under LGPL v2.1, we strongly encourage
* everyone modifying this software to contribute back any improvements and
* bugfixes to the project for the benefit all other users. Thank you.
*
* $Id$
*/
/* external defines:
SWAPEND - needs to be defined for platforms with inverse endianess related to Intel
MAIN - should be defined in just one file that will contain the fn definitions and variables
(this is inherited from Rsrv.h and sisocks.h)
*/
#pragma once
#ifndef __RCONNECTION2_H__
#define __RCONNECTION2_H__
#if defined __GNUC__ && !defined unix && !defined WIN32
#define unix
#endif
#ifdef WIN32
#include <WinSock2.h>
#else
#include <sys/socket.h>
typedef int SOCKET;
#endif
#include <iostream>
#include <vector>
#include <memory>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdint>
#include "Rsrv.h"
#ifdef WIN32
#if defined(RCONNECTION2_DLL_AS_LIB)
#define RCONNECTION2_API
#else
#if defined(BUILDING_RCONNECTION2_DLL)
#define RCONNECTION2_API __declspec(dllexport)
#else
#define RCONNECTION2_API __declspec(dllimport)
#endif
#endif
#else
#define RCONNECTION2_API
#endif
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable:4251)
#endif
namespace Rconnection2 {
typedef uint32_t Rsize_t;
//=== Rconnection error codes
#define CERR_connect_failed -1
#define CERR_handshake_failed -2
#define CERR_invalid_id -3
#define CERR_protocol_not_supp -4
#define CERR_not_connected -5
#define CERR_peer_closed -7
#define CERR_malformed_packet -8
#define CERR_send_error -9
#define CERR_out_of_mem -10
#define CERR_not_supported -11
#define CERR_io_error -12
// this one is custom - authentication method required by
// the server is not supported in this client
#define CERR_auth_unsupported -20
#define A_required 0x001
#define A_crypt 0x002
#define A_plain 0x004
class RCONNECTION2_API IRconnection
{
protected:
IRconnection() {}
public:
virtual ~IRconnection() {}
virtual int connect() = 0;
virtual bool disconnect() = 0;
virtual SOCKET getSocket() const = 0;
};
//===================================== Rmessage ---- QAP1 storage
class RCONNECTION2_API MessageBuffer
{
public:
typedef unsigned int buffer_element_type;
MessageBuffer()
:
bytes_()
{
}
explicit MessageBuffer(size_t nbytes)
:
bytes_((nbytes / sizeof(buffer_element_type)) + ((nbytes % sizeof(buffer_element_type)) ? 1 : 0))
{
}
template<class T> T* get() const { return (T*)(&bytes_[0]); }
private:
std::vector<buffer_element_type> bytes_;
};
class RCONNECTION2_API Rmessage
{
public:
std::shared_ptr<MessageBuffer> get_buffer() const { return data_; }
protected:
struct phdr header_;
std::shared_ptr<MessageBuffer> data_;
int complete_;
Rsize_t len_;
// the following is avaliable only for parsed messages (max 16 pars)
std::vector<unsigned int *>par_;
void alloc_data_only(size_t n)
{
if (n == 0) n = 1;
data_ = std::make_shared<MessageBuffer>(n);
}
protected:
Rmessage();
Rmessage(int cmd); // 0 data_
Rmessage(int cmd, const char *txt); // DT_STRING data_
Rmessage(int cmd, int i); // DT_INT data_ (1 entry)
Rmessage(int cmd, const void *buf, Rsize_t len, int raw_data = 0); // raw data_ or DT_BYTESTREAM
public:
static std::shared_ptr<Rmessage> create() { return std::shared_ptr<Rmessage>(new Rmessage()); }
static std::shared_ptr<Rmessage> create(int cmd) { return std::shared_ptr<Rmessage>(new Rmessage(cmd)); } // 0 data_
static std::shared_ptr<Rmessage> create(int cmd, const char *txt) { return std::shared_ptr<Rmessage>(new Rmessage(cmd, txt)); } // DT_STRING data_
static std::shared_ptr<Rmessage> create(int cmd, int i) { return std::shared_ptr<Rmessage>(new Rmessage(cmd, i)); } // DT_INT data_ (1 entry)
static std::shared_ptr<Rmessage> create(int cmd, const void *buf, Rsize_t len = 0, int raw_data = 0) { return std::shared_ptr<Rmessage>(new Rmessage(cmd, buf, len, raw_data)); } // raw data_ or DT_BYTESTREAM
virtual ~Rmessage() {}
int command() { return complete_ ? header_.cmd : -1; }
Rsize_t length() { return complete_ ? header_.len : -1; }
int is_complete() { return complete_; }
const unsigned int* get_par(int index) const { return par_[index]; }
unsigned get_par(int index1, int index2) { return par_[index1][index2]; }
size_t get_par_count() const { return par_.size(); }
Rsize_t get_len() const { return len_; }
char* get_data() { return data_ ? data_->get<char>() : NULL; }
const char* get_data() const { return data_ ? data_->get<char>() : NULL; }
void alloc_data(size_t n)
{
alloc_data_only(n);
len_ = header_.len = n;
}
const struct phdr& get_header() const { return header_; }
int read(IRconnection& conn);
void parse();
int send(IRconnection& conn);
};
//===================================== Rexp --- basis for all SEXPs
class RCONNECTION2_API Rexp : public std::enable_shared_from_this < Rexp >
{
private:
explicit Rexp(const Rexp&);
Rexp& operator=(const Rexp&);
protected:
Rsize_t len_;
int type_;
char* data_;
char* next_;
// the next_ two are only cached if requested, no direct access allowed
std::vector<char> databuf_;
mutable std::vector<std::string> attrnames_;
std::shared_ptr<Rexp> attr_;
std::shared_ptr<MessageBuffer> buffer_;
protected:
explicit Rexp(const std::shared_ptr<Rmessage>& msg);
Rexp(const unsigned int *pos, const std::shared_ptr<MessageBuffer>& buffer);
Rexp(int type, const char *data = 0, Rsize_t = 0, std::shared_ptr<Rexp> attr = std::shared_ptr<Rexp>());
template<class V, class Extractor>
void initFromContainer(const std::vector<V>& data, const Extractor& extractor);
virtual void fix_content() {}
char *parseBytes(const unsigned int *pos);
static std::shared_ptr<Rexp> createFromBytes(const unsigned int *d, const std::shared_ptr<MessageBuffer>& buffer);
public:
static std::shared_ptr<Rexp> create(const std::shared_ptr<Rmessage>& msg);
static std::shared_ptr<Rexp> create(const unsigned int *pos,
std::shared_ptr<Rmessage> msg = std::shared_ptr<Rmessage>())
{
return std::shared_ptr<Rexp>(new Rexp(pos, msg ? msg->get_buffer()
: std::shared_ptr<MessageBuffer>()));
}
static std::shared_ptr<Rexp> create(int type, const char *data = 0, Rsize_t len = 0,
std::shared_ptr<Rexp> attr = std::shared_ptr<Rexp>())
{
return std::shared_ptr<Rexp>(new Rexp(type, data, len, attr));
}
virtual ~Rexp() {}
int get_type() const { return type_; }
virtual Rsize_t storageSize() const { return len_ + ((len_ > 0x7fffff) ? 8 : 4); }
virtual void store(char *buf) const;
std::shared_ptr<Rexp> attribute(const char *name) const;
const std::vector<std::string>& attributeNames() const;
char* get_next() const { return next_; }
virtual Rsize_t length() { return len_; }
friend std::ostream& operator<< (std::ostream& os, const Rexp& exp)
{
return ((Rexp&)exp).os_print(os);
}
friend std::ostream& operator<< (std::ostream& os, const Rexp* exp)
{
return ((Rexp*)exp)->os_print(os);
}
virtual std::ostream& os_print(std::ostream& os)
{
return os << "Rexp[type=" << type_ << ",len=" << len_ << "]";
}
};
template<class V, class Extractor>
void Rexp::initFromContainer(const std::vector<V>& data, const Extractor& extractor)
{
#ifdef DEBUG_CXX
std::cout << "intiFromContainer Rexp2@" << static_cast<void*>(this) << std::endl;
#endif
len_ = data.size() * sizeof(typename Extractor::result_type);
if (len_)
{
buffer_ = std::make_shared<MessageBuffer>(len_);
data_ = buffer_->get<char>();
typename Extractor::result_type* p = buffer_->get<typename Extractor::result_type>();
for (const auto& v: data)
*p++ = extractor(v);
}
next_ = NULL;
}
//===================================== Rint --- XT_INT/XT_ARRAY_INT
class RCONNECTION2_API Rinteger : public Rexp {
protected:
Rinteger() : Rexp(XT_ARRAY_INT) {}
Rinteger(const std::shared_ptr<Rmessage>& msg) : Rexp(msg) { }
Rinteger(const unsigned int *ipos, const std::shared_ptr<MessageBuffer>& buffer) : Rexp(ipos, buffer) { }
Rinteger(const int *array, int count) : Rexp(XT_ARRAY_INT, (char*)array, count*sizeof(int)) { }
Rinteger(const unsigned *array, int count) : Rexp(XT_ARRAY_INT, (char*)array, count*sizeof(unsigned)) { }
Rinteger(const std::vector<int>& array) : Rexp(XT_ARRAY_INT, (char*)&array[0], array.size()*sizeof(int)) {}
Rinteger(const std::vector<unsigned>& array) : Rexp(XT_ARRAY_INT, (char*)&array[0], array.size()*sizeof(unsigned)) {}
virtual void fix_content();
public:
static std::shared_ptr<Rinteger> create(const std::shared_ptr<Rmessage>& msg)
{
auto p = std::shared_ptr<Rinteger>(new Rinteger(msg));
p->fix_content();
return p;
}
static std::shared_ptr<Rinteger> create(const unsigned int *ipos, const std::shared_ptr<MessageBuffer>& buffer)
{
auto p = std::shared_ptr<Rinteger>(new Rinteger(ipos, buffer));
p->fix_content();
return p;
}
static std::shared_ptr<Rinteger> create(const int *array, int count)
{
auto p = std::shared_ptr<Rinteger>(new Rinteger(array, count));
p->fix_content();
return p;
}
static std::shared_ptr<Rinteger> create(const unsigned *array, int count)
{
auto p = std::shared_ptr<Rinteger>(new Rinteger(array, count));
p->fix_content();
return p;
}
static std::shared_ptr<Rinteger> create(const std::vector<int>& array)
{
auto p = std::shared_ptr<Rinteger>(new Rinteger(array));
p->fix_content();
return p;
}
static std::shared_ptr<Rinteger> create(const std::vector<unsigned>& array)
{
auto p = std::shared_ptr<Rinteger>(new Rinteger(array));
p->fix_content();
return p;
}
template<class V, class Extractor>
static std::shared_ptr<Rinteger> create(const std::vector<V>& array, const Extractor& extractor)
{
auto p = std::shared_ptr<Rinteger>(new Rinteger());
p->initFromContainer(array, extractor);
p->fix_content();
return p;
}
virtual ~Rinteger() {}
int *intArray() { return (int*)data_; }
int intAt(int pos) { return (pos >= 0 && (unsigned)pos < len_ / 4) ? ((int*)data_)[pos] : 0; }
virtual Rsize_t length() { return len_ / 4; }
virtual std::ostream& os_print(std::ostream& os)
{
return os << "Rinteger[" << (len_ / 4) << "]";
}
};
//===================================== Rdouble --- XT_DOUBLE/XT_ARRAY_DOUBLE
class RCONNECTION2_API Rdouble : public Rexp {
protected:
Rdouble() : Rexp(XT_ARRAY_DOUBLE) {}
Rdouble(const std::shared_ptr<Rmessage>& msg) : Rexp(msg) { }
Rdouble(const unsigned int *ipos, const std::shared_ptr<MessageBuffer>& buffer) : Rexp(ipos, buffer) {}
Rdouble(const double *array, int count) : Rexp(XT_ARRAY_DOUBLE, (char*)array, count*sizeof(double)) {}
Rdouble(const std::vector<double>& array) : Rexp(XT_ARRAY_DOUBLE, (char*)&array[0], array.size()*sizeof(double)) {}
virtual void fix_content();
public:
static std::shared_ptr<Rdouble> create(const std::shared_ptr<Rmessage>& msg)
{
auto p = std::shared_ptr<Rdouble>(new Rdouble(msg));
p->fix_content();
return p;
}
static std::shared_ptr<Rdouble> create(const unsigned int *ipos, const std::shared_ptr<MessageBuffer>& buffer)
{
auto p = std::shared_ptr<Rdouble>(new Rdouble(ipos, buffer));
p->fix_content();
return p;
}
static std::shared_ptr<Rdouble> create(const double *array, int count)
{
auto p = std::shared_ptr<Rdouble>(new Rdouble(array, count));
p->fix_content();
return p;
}
static std::shared_ptr<Rdouble> create(const std::vector<double>& array)
{
auto p = std::shared_ptr<Rdouble>(new Rdouble(array));
p->fix_content();
return p;
}
template<class V, class Extractor>
static std::shared_ptr<Rdouble> create(const std::vector<V>& array, const Extractor& extractor)
{
auto p = std::shared_ptr<Rdouble>(new Rdouble());
p->initFromContainer(array, extractor);
p->fix_content();
return p;
}
virtual ~Rdouble() {}
double *doubleArray() { return (double*)data_; }
double doubleAt(int pos) { return (pos >= 0 && (unsigned)pos < len_ / 8) ? ((double*)data_)[pos] : 0; }
virtual Rsize_t length() { return len_ / 8; }
virtual std::ostream& os_print(std::ostream& os)
{
return os << "Rdouble[" << (len_ / 8) << "]";
}
};
//===================================== Rsymbol --- XT_SYM
class RCONNECTION2_API Rsymbol : public Rexp
{
protected:
std::string name_;
Rsymbol(const std::shared_ptr<Rmessage>& msg) : Rexp(msg), name_() {}
Rsymbol(const unsigned int *ipos, const std::shared_ptr<MessageBuffer>& buffer) : Rexp(ipos, buffer), name_() {}
virtual void fix_content();
public:
static std::shared_ptr<Rsymbol> create(const std::shared_ptr<Rmessage>& msg)
{
auto p = std::shared_ptr<Rsymbol>(new Rsymbol(msg));
p->fix_content();
return p;
}
static std::shared_ptr<Rsymbol> create(const unsigned int *ipos, const std::shared_ptr<MessageBuffer>& buffer)
{
auto p = std::shared_ptr<Rsymbol>(new Rsymbol(ipos, buffer));
p->fix_content();
return p;
}
virtual ~Rsymbol() {}
const char *symbolName() { return name_.c_str(); }
virtual std::ostream& os_print(std::ostream& os)
{
return os << "Rsymbol[" << symbolName() << "]";
}
};
//===================================== Rstrings --- XT_ARRAY_STR
// NOTE: XT_ARRAY_STR is new in 0103 and ths class is just a
// very crude implementation. It replaces Rstring because
// XT_STR has been deprecated.
// FIXME: it should be a subclass of Rvector!
class RCONNECTION2_API Rstrings : public Rexp
{
protected:
std::vector<const char*> cont_;
Rstrings(const std::shared_ptr<Rmessage>& msg) : Rexp(msg), cont_() {}
Rstrings(const unsigned int *ipos, const std::shared_ptr<MessageBuffer>& buffer) : Rexp(ipos, buffer), cont_()
{}
virtual void fix_content();
public:
static std::shared_ptr<Rstrings> create(const std::shared_ptr<Rmessage>& msg)
{
auto p = std::shared_ptr<Rstrings>(new Rstrings(msg));
p->fix_content();
return p;
}
static std::shared_ptr<Rstrings> create(const unsigned int *ipos, const std::shared_ptr<MessageBuffer>& buffer)
{
auto p = std::shared_ptr<Rstrings>(new Rstrings(ipos, buffer));
p->fix_content();
return p;
}
static std::shared_ptr<Rstrings> create(const std::vector<std::string>& sv);
virtual ~Rstrings() {}
const std::vector<const char*>& strings() const { return cont_; }
std::string str(size_t i = 0) const { return cont_.at(i); }
unsigned int count() { return cont_.size(); }
int indexOfString(const char *str);
virtual std::ostream& os_print(std::ostream& os)
{
return os << "char*[" << cont_.size() << "]\"" << str() << "\"..";
}
};
//===================================== Rstring --- XT_STR
class RCONNECTION2_API Rstring : public Rexp
{
protected:
Rstring(const std::shared_ptr<Rmessage>& msg) : Rexp(msg) {}
Rstring(const unsigned int *ipos, const std::shared_ptr<MessageBuffer>& buffer) : Rexp(ipos, buffer) {}
Rstring(const char *str) : Rexp(XT_STR, str, strlen(str) + 1) {}
public:
static std::shared_ptr<Rstring> create(const std::shared_ptr<Rmessage>& msg)
{
auto p = std::shared_ptr<Rstring>(new Rstring(msg));
p->fix_content();
return p;
}
static std::shared_ptr<Rstring> create(const unsigned int *ipos, const std::shared_ptr<MessageBuffer>& buffer)
{
auto p = std::shared_ptr<Rstring>(new Rstring(ipos, buffer));
p->fix_content();
return p;
}
static std::shared_ptr<Rstring> create(const char *str)
{
auto p = std::shared_ptr<Rstring>(new Rstring(str));
p->fix_content();
return p;
}
virtual ~Rstring() {}
const char* c_str() { return (char*)data_; }
virtual std::ostream& os_print(std::ostream& os)
{
return os << "\"" << c_str() << "\"";
}
};
//===================================== Rlist --- XT_LIST (CONS lists)
class RCONNECTION2_API Rlist : public Rexp
{
protected:
std::shared_ptr<Rexp> head_, tag_, tail_;
Rlist(const std::shared_ptr<Rmessage>& msg) : Rexp(msg), head_(), tag_(), tail_() {}
Rlist(const unsigned int *ipos, const std::shared_ptr<MessageBuffer>& buffer)
:
Rexp(ipos, buffer),
head_(), tag_(), tail_()
{
}
/* this is a sort of special constructor that allows to create a Rlist
based solely on its content. This is necessary since 0.5 because
each LISTSXP is no longer represented by its own encoded SEXP
but they are packed in one content list instead */
Rlist(int type, const std::shared_ptr<Rexp>& head, const std::shared_ptr<Rexp>& tag,
char *next)
:
Rexp(type, 0, 0),
head_(head),
tag_(tag),
tail_()
{
next_ = next;
}
public:
static std::shared_ptr<Rlist> create(const std::shared_ptr<Rmessage>& msg)
{
auto p = std::shared_ptr<Rlist>(new Rlist(msg));
p->fix_content();
return p;
}
static std::shared_ptr<Rlist> create(const unsigned int *ipos, const std::shared_ptr<MessageBuffer>& buffer)
{
auto p = std::shared_ptr<Rlist>(new Rlist(ipos, buffer));
p->fix_content();
return p;
}
static std::shared_ptr<Rlist> create(int type, const std::shared_ptr<Rexp>& head, const std::shared_ptr<Rexp>& tag,
char *next)
{
auto p = std::shared_ptr<Rlist>(new Rlist(type, head, tag, next));
p->fix_content();
return p;
}
virtual ~Rlist() {}
std::shared_ptr<Rexp> get_head() const { return head_; }
std::shared_ptr<Rexp> get_tail() const { return tail_; }
std::shared_ptr<Rexp> get_tag() const { return tag_; }
std::shared_ptr<Rexp> entryByTagName(const char *tagName)
{
if (tag_ && (tag_->get_type() == XT_SYM || tag_->get_type() == XT_SYMNAME)
&& !strcmp((static_cast<Rsymbol*>(tag_.get()))->symbolName(), tagName))
return head_;
else if (tail_)
return static_cast<Rlist*>(tail_.get())->entryByTagName(tagName);
else
return std::shared_ptr<Rexp>();
}
virtual std::ostream& os_print(std::ostream& os)
{
os << "Rlist[tag=";
if (tag_) os << *tag_; else os << "<none>";
os << ",head_=";
if (head_) os << *head_; else os << "<none>";
if (tail_) os << ",tail=" << *tail_;
return os << "]";
}
virtual void fix_content();
};
//===================================== Rvector --- XT_VECTOR (general lists)
class RCONNECTION2_API Rvector : public Rexp
{
protected:
mutable std::vector< std::shared_ptr<Rexp> >cont_;
// cached
std::vector<std::string> strs_;
bool strs_populated_;
Rvector(const std::shared_ptr<Rmessage>& msg)
:
Rexp(msg),
cont_(),
strs_(),
strs_populated_(false)
{
}
Rvector(const unsigned int *ipos, const std::shared_ptr<MessageBuffer>& buffer)
:
Rexp(ipos, buffer),
cont_(),
strs_(),
strs_populated_(false)
{
}
public:
static std::shared_ptr<Rvector> create(const std::shared_ptr<Rmessage>& msg)
{
auto p = std::shared_ptr<Rvector>(new Rvector(msg));
p->fix_content();
return p;
}
static std::shared_ptr<Rvector> create(const unsigned int *ipos, const std::shared_ptr<MessageBuffer>& buffer)
{
auto p = std::shared_ptr<Rvector>(new Rvector(ipos, buffer));
p->fix_content();
return p;
}
virtual ~Rvector() {}
const std::vector<std::string>& strings();
size_t indexOf(const std::shared_ptr<Rexp>& exp) const;
size_t indexOfString(const char *str) const;
virtual Rsize_t length() { return (Rsize_t)cont_.size(); }
const char *stringAt(size_t i)
{
if (i >= cont_.size() || !cont_[i] || cont_[i]->get_type() != XT_STR)
return 0;
else
return ((Rstring*)cont_[i].get())->c_str();
}
std::shared_ptr<Rexp> elementAt(int i) { return cont_[i]; }
template<class V> std::shared_ptr<V> byName(const char *name) const
{
std::shared_ptr<Rexp> p = byName_Rexp(name);
return std::shared_ptr<V>(p, static_cast<V*>(p.get()));
}
std::shared_ptr<Rexp> byName_Rexp(const char *name) const;
virtual std::ostream& os_print(std::ostream& os)
{
os << "Rvector[count=" << cont_.size() << ":";
int i = 0;
for (const auto& p : cont_)
{
if (i) os << ",";
if (p) os << *cont_[i]; else os << "NULL";
i++;
}
return os << "]";
}
virtual void fix_content();
};
//===================================== Rconnection ---- Rserve interface class
class Rconnection;
class RCONNECTION2_API Rsession
{
protected:
std::string host_;
int port_;
char key_[32];
Rsession(const char *host, int port, const char key[32])
:
host_(host),
port_ (port)
{
memcpy(key_, key, 32);
}
public:
static std::shared_ptr<Rsession> create(const char *host, int port, const char key[32])
{
return std::shared_ptr<Rsession>(new Rsession(host, port, key));
}
~Rsession() {}
const char *host() const { return host_.c_str(); }
int port() const { return port_; }
const char *key() const { return key_; }
};
class RCONNECTION2_API Rconnection: public IRconnection
{
protected:
std::string host_;
int port_;
int family_;
SOCKET s_;
int auth_;
char salt_[2];
std::vector<char> session_key_;
/** host - either host name or unix socket path
port - either TCP port or -1 if unix sockets should be used */
explicit Rconnection(const char *host = "127.0.0.1", int port = default_Rsrv_port);
explicit Rconnection(const Rsession& session);
public:
static std::shared_ptr<Rconnection> create(const char *host = "127.0.0.1", int port = default_Rsrv_port)
{
return std::shared_ptr<Rconnection>(new Rconnection(host, port));
}
static std::shared_ptr<Rconnection> create(const Rsession& session)
{
return std::shared_ptr<Rconnection>(new Rconnection(session));
}
virtual ~Rconnection()
{
disconnect();
}
virtual int connect();
virtual bool disconnect();
virtual SOCKET getSocket() const { return s_; }
int getLastSocketError(char* buffer, size_t buffer_len, int options) const;
/** --- high-level functions --- */
int assign(const char *symbol, const Rexp& exp);
int assign(const std::string& symbol, const Rexp& exp)
{
return assign(symbol.c_str(), exp);
}
int voidEval(const char *cmd);
int voidEval(const std::string& cmd)
{
return voidEval(cmd.c_str());
}
template<class V> std::shared_ptr<V> eval(const char *cmd, int *status = 0, int opt = 0)
{
std::shared_ptr<Rexp> p = eval_to_Rexp(cmd, status, opt);
return std::shared_ptr<V>(p, static_cast<V*>(p.get()));
}
std::shared_ptr<Rexp> eval_to_Rexp(const char *cmd, int *status, int opt);
int login(const char *user, const char *pwd);
int shutdown(const char *key);
/* ( I/O functions ) */
int openFile(const char *fn);
int createFile(const char *fn);
Rsize_t readFile(char *buf, unsigned int len);
int writeFile(const char *buf, unsigned int len);
int closeFile();
int removeFile(const char *fn);
/* session methods - results of detach [if not NULL] must be deleted by the caller when no longer needed! */
std::shared_ptr<Rsession> detachedEval(const char *cmd, int *status = 0);
std::shared_ptr<Rsession> detach(int *status = 0);
// sessions are resumed using resume() method of the Rsession object
int queryCustomStatus(bool& status); // [IP] our custom API
#ifdef CMD_ctrl
/* server control functions (need Rserve 0.6-0 or higher) */
int serverEval(const char *cmd);
int serverSource(const char *fn);
int serverShutdown();
#endif
protected:
int request(Rmessage& msg, int cmd, Rsize_t len = 0, void *par = 0);
int request(Rmessage& targetMsg, Rmessage& contents);
};
} // namespace Rconnection2
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#endif