-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSocketDS.cpp
More file actions
862 lines (746 loc) · 28.6 KB
/
SocketDS.cpp
File metadata and controls
862 lines (746 loc) · 28.6 KB
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
/*----- PROTECTED REGION ID(SocketDS.cpp) ENABLED START -----*/
static const char *RcsId = "$Id: $";
//=============================================================================
//
// file : SocketDS.cpp
//
// description : C++ source for the SocketDS class and its commands.
// The class is derived from Device. It represents the
// CORBA servant object which will be accessed from the
// network. All commands which can be executed on the
// SocketDS are implemented in this file.
//
// project : Socket
//
// This file is part of Tango device class.
//
// Tango is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Tango 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Tango. If not, see <http://www.gnu.org/licenses/>.
//
// $Author: $
//
// $Revision: $
// $Date: $
//
// $HeadURL: $
//
//=============================================================================
// This file is generated by POGO
// (Program Obviously used to Generate tango Object)
//=============================================================================
#include <SocketDS.h>
#include <SocketDSClass.h>
#include <sstream>
#include <locale.h>
/*----- PROTECTED REGION END -----*/ // SocketDS.cpp
/**
* SocketDS class description:
* This class implements basic `socket` IPC.
*/
//================================================================
// The following table gives the correspondence
// between command and method names.
//
// Command name | Method name
//================================================================
// State | Inherited (no method)
// Status | Inherited (no method)
// Write | write
// Read | read
// Reconnect | reconnect
// WriteAndRead | write_and_read
// Readln | readln
// ReadUntil | read_until
// WriteReadUntil | write_read_until
// CheckConnection | check_connection
// WriteAndReadBinary | write_and_read_binary
// ReadBinary | read_binary
// WriteBinary | write_binary
// AvalaibleBytes | avalaible_bytes
//================================================================
//================================================================
// Attributes managed is:
//================================================================
//================================================================
namespace SocketDS_ns
{
/*----- PROTECTED REGION ID(SocketDS::namespace_starting) ENABLED START -----*/
// static initializations
/*----- PROTECTED REGION END -----*/ // SocketDS::namespace_starting
//--------------------------------------------------------
/**
* Method : SocketDS::SocketDS()
* Description : Constructors for a Tango device
* implementing the classSocketDS
*/
//--------------------------------------------------------
SocketDS::SocketDS(Tango::DeviceClass *cl, string &s)
: TANGO_BASE_CLASS(cl, s.c_str())
{
/*----- PROTECTED REGION ID(SocketDS::constructor_1) ENABLED START -----*/
init_device();
/*----- PROTECTED REGION END -----*/ // SocketDS::constructor_1
}
//--------------------------------------------------------
SocketDS::SocketDS(Tango::DeviceClass *cl, const char *s)
: TANGO_BASE_CLASS(cl, s)
{
/*----- PROTECTED REGION ID(SocketDS::constructor_2) ENABLED START -----*/
init_device();
/*----- PROTECTED REGION END -----*/ // SocketDS::constructor_2
}
//--------------------------------------------------------
SocketDS::SocketDS(Tango::DeviceClass *cl, const char *s, const char *d)
: TANGO_BASE_CLASS(cl, s, d)
{
/*----- PROTECTED REGION ID(SocketDS::constructor_3) ENABLED START -----*/
init_device();
/*----- PROTECTED REGION END -----*/ // SocketDS::constructor_3
}
//--------------------------------------------------------
/**
* Method : SocketDS::delete_device()
* Description : will be called at device destruction or at init command
*/
//--------------------------------------------------------
void SocketDS::delete_device()
{
DEBUG_STREAM << "SocketDS::delete_device() " << device_name << endl;
/*----- PROTECTED REGION ID(SocketDS::delete_device) ENABLED START -----*/
// Delete device allocated objects
// boost::system::error_code ec;
// socket->close(ec);
// if (ec)
// {
// ERROR_STREAM << "close error code " << ec << endl;
// }
delete socket;
delete ep;
delete io_service;
delete buffer;
/*----- PROTECTED REGION END -----*/ // SocketDS::delete_device
}
//--------------------------------------------------------
/**
* Method : SocketDS::init_device()
* Description : will be called at device initialization.
*/
//--------------------------------------------------------
void SocketDS::init_device()
{
DEBUG_STREAM << "SocketDS::init_device() create device " << device_name << endl;
/*----- PROTECTED REGION ID(SocketDS::init_device_before) ENABLED START -----*/
// Initialization before get_device_property() call
/*----- PROTECTED REGION END -----*/ // SocketDS::init_device_before
// Get the device properties from database
get_device_property();
/*----- PROTECTED REGION ID(SocketDS::init_device) ENABLED START -----*/
// Initialize device
setlocale( LC_ALL,"Russian" );
std::string ip = (strcmp(hostname.c_str(), "localhost") == 0) ? "127.0.0.1" : hostname;
DEBUG_STREAM << "Connecting to: " << ip << ":" << port << endl;
try
{
io_service = new boost::asio::io_service();
socket = new boost::asio::ip::tcp::socket(*io_service);
timer = new boost::asio::deadline_timer(*io_service);
ep = new boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string(ip), port);
buffer = new boost::asio::streambuf(1000);
// if (no_delay) {
// boost::asio::ip::tcp::no_delay option(true);
// socket->set_option(option);
// }
socket->connect(*ep);
timer->expires_at(boost::posix_time::pos_infin);
check_deadline();
set_state(Tango::ON);
set_status("Connected!");
DEBUG_STREAM << "Success!"<< endl;
}
catch(std::exception& e)
{
set_state(Tango::FAULT);
std::stringstream ss;
ss << "Connection exception: " << e.what();
set_status(ss.str());
}
/*----- PROTECTED REGION END -----*/ // SocketDS::init_device
}
//--------------------------------------------------------
/**
* Method : SocketDS::get_device_property()
* Description : Read database to initialize property data members.
*/
//--------------------------------------------------------
void SocketDS::get_device_property()
{
/*----- PROTECTED REGION ID(SocketDS::get_device_property_before) ENABLED START -----*/
// Initialize property data members
hostname = "localhost";
port = 9999;
readtimeout = 1000;
autoReconnect = true;
/*----- PROTECTED REGION END -----*/ // SocketDS::get_device_property_before
// Read device properties from database.
Tango::DbData dev_prop;
dev_prop.push_back(Tango::DbDatum("Hostname"));
dev_prop.push_back(Tango::DbDatum("Port"));
dev_prop.push_back(Tango::DbDatum("Readtimeout"));
dev_prop.push_back(Tango::DbDatum("AutoReconnect"));
// is there at least one property to be read ?
if (dev_prop.size()>0)
{
// Call database and extract values
if (Tango::Util::instance()->_UseDb==true)
get_db_device()->get_property(dev_prop);
// get instance on SocketDSClass to get class property
Tango::DbDatum def_prop, cl_prop;
SocketDSClass *ds_class =
(static_cast<SocketDSClass *>(get_device_class()));
int i = -1;
// Try to initialize Hostname from class property
cl_prop = ds_class->get_class_property(dev_prop[++i].name);
if (cl_prop.is_empty()==false) cl_prop >> hostname;
else {
// Try to initialize Hostname from default device value
def_prop = ds_class->get_default_device_property(dev_prop[i].name);
if (def_prop.is_empty()==false) def_prop >> hostname;
}
// And try to extract Hostname value from database
if (dev_prop[i].is_empty()==false) dev_prop[i] >> hostname;
// Try to initialize Port from class property
cl_prop = ds_class->get_class_property(dev_prop[++i].name);
if (cl_prop.is_empty()==false) cl_prop >> port;
else {
// Try to initialize Port from default device value
def_prop = ds_class->get_default_device_property(dev_prop[i].name);
if (def_prop.is_empty()==false) def_prop >> port;
}
// And try to extract Port value from database
if (dev_prop[i].is_empty()==false) dev_prop[i] >> port;
// Try to initialize Readtimeout from class property
cl_prop = ds_class->get_class_property(dev_prop[++i].name);
if (cl_prop.is_empty()==false) cl_prop >> readtimeout;
else {
// Try to initialize Readtimeout from default device value
def_prop = ds_class->get_default_device_property(dev_prop[i].name);
if (def_prop.is_empty()==false) def_prop >> readtimeout;
}
// And try to extract Readtimeout value from database
if (dev_prop[i].is_empty()==false) dev_prop[i] >> readtimeout;
// Try to initialize AutoReconnect from class property
cl_prop = ds_class->get_class_property(dev_prop[++i].name);
if (cl_prop.is_empty()==false) cl_prop >> autoReconnect;
else {
// Try to initialize AutoReconnect from default device value
def_prop = ds_class->get_default_device_property(dev_prop[i].name);
if (def_prop.is_empty()==false) def_prop >> autoReconnect;
}
// And try to extract AutoReconnect value from database
if (dev_prop[i].is_empty()==false) dev_prop[i] >> autoReconnect;
}
/*----- PROTECTED REGION ID(SocketDS::get_device_property_after) ENABLED START -----*/
// Check device property data members init
/*----- PROTECTED REGION END -----*/ // SocketDS::get_device_property_after
}
//--------------------------------------------------------
/**
* Method : SocketDS::always_executed_hook()
* Description : method always executed before any command is executed
*/
//--------------------------------------------------------
void SocketDS::always_executed_hook()
{
DEBUG_STREAM << "SocketDS::always_executed_hook() " << device_name << endl;
/*----- PROTECTED REGION ID(SocketDS::always_executed_hook) ENABLED START -----*/
// code always executed before all requests
/*----- PROTECTED REGION END -----*/ // SocketDS::always_executed_hook
}
//--------------------------------------------------------
/**
* Method : SocketDS::read_attr_hardware()
* Description : Hardware acquisition for attributes
*/
//--------------------------------------------------------
void SocketDS::read_attr_hardware(TANGO_UNUSED(vector<long> &attr_list))
{
DEBUG_STREAM << "SocketDS::read_attr_hardware(vector<long> &attr_list) entering... " << endl;
/*----- PROTECTED REGION ID(SocketDS::read_attr_hardware) ENABLED START -----*/
// Add your own code
/*----- PROTECTED REGION END -----*/ // SocketDS::read_attr_hardware
}
//--------------------------------------------------------
/**
* Method : SocketDS::add_dynamic_attributes()
* Description : Create the dynamic attributes if any
* for specified device.
*/
//--------------------------------------------------------
void SocketDS::add_dynamic_attributes()
{
/*----- PROTECTED REGION ID(SocketDS::add_dynamic_attributes) ENABLED START -----*/
// Add your own code to create and add dynamic attributes if any
/*----- PROTECTED REGION END -----*/ // SocketDS::add_dynamic_attributes
}
//--------------------------------------------------------
/**
* Command Write related method
* Description: Command used to send a string to the socket.
*
* @param argin
*/
//--------------------------------------------------------
void SocketDS::write(Tango::DevString argin)
{
DEBUG_STREAM << "SocketDS::Write() - " << device_name << endl;
/*----- PROTECTED REGION ID(SocketDS::write) ENABLED START -----*/
try
{
std::stringstream command;
command << argin;
socket_write(command.str());
}
catch(std::exception& e)
{
set_state(Tango::FAULT);
std::stringstream desc;
desc << "Connection exception: " << e.what();
set_status(desc.str());
std::stringstream origin;
origin << "Socket::write(" << argin << ") ";
Tango::Except::throw_exception((const char *) "SocketError", desc.str(), origin.str());
}
/*----- PROTECTED REGION END -----*/ // SocketDS::write
}
//--------------------------------------------------------
/**
* Command Read related method
* Description: Command used to read a string from the socket.
*
* @returns
*/
//--------------------------------------------------------
Tango::DevString SocketDS::read()
{
Tango::DevString argout;
DEBUG_STREAM << "SocketDS::Read() - " << device_name << endl;
/*----- PROTECTED REGION ID(SocketDS::read) ENABLED START -----*/
std::stringstream result;
result << "";
try
{
socket_read();
result << buffer;
}
catch (std::exception& e)
{
set_state(Tango::FAULT);
std::stringstream desc;
desc << "Connection exception: " << e.what();
set_status(desc.str());
std::stringstream origin;
origin << "Socket::read()";
Tango::Except::throw_exception((const char *) "SocketError", desc.str(), origin.str());
}
argout = Tango::string_dup(result.str().c_str());
/*----- PROTECTED REGION END -----*/ // SocketDS::read
return argout;
}
//--------------------------------------------------------
/**
* Command Reconnect related method
* Description: Attempts to re-establish the socket connection.
*
*/
//--------------------------------------------------------
void SocketDS::reconnect()
{
DEBUG_STREAM << "SocketDS::Reconnect() - " << device_name << endl;
/*----- PROTECTED REGION ID(SocketDS::reconnect) ENABLED START -----*/
delete_device();
init_device();
/*----- PROTECTED REGION END -----*/ // SocketDS::reconnect
}
//--------------------------------------------------------
/**
* Command WriteAndRead related method
* Description: Write to the socket and waits for the answer.
* The end of the answer is triggered by the new line character!
*
* @param argin Command string.
* @returns Answer string.
*/
//--------------------------------------------------------
Tango::DevString SocketDS::write_and_read(Tango::DevString argin)
{
Tango::DevString argout;
DEBUG_STREAM << "SocketDS::WriteAndRead() - " << device_name << endl;
/*----- PROTECTED REGION ID(SocketDS::write_and_read) ENABLED START -----*/
write(argin);
argout = read();
/*----- PROTECTED REGION END -----*/ // SocketDS::write_and_read
return argout;
}
//--------------------------------------------------------
/**
* Command Readln related method
* Description: Reads from a socket until a "\n" has been received.
*
* @returns argout
*/
//--------------------------------------------------------
Tango::DevString SocketDS::readln()
{
Tango::DevString argout;
DEBUG_STREAM << "SocketDS::Readln() - " << device_name << endl;
/*----- PROTECTED REGION ID(SocketDS::readln) ENABLED START -----*/
std::string line = "";
try
{
socket_read();
std::istream is(buffer);
std::getline(is, line);
}
catch (std::exception& e)
{
set_state(Tango::FAULT);
std::stringstream desc;
desc << "Connection exception: " << e.what();
set_status(desc.str());
std::stringstream origin;
origin << "Socket::readln()";
Tango::Except::throw_exception((const char *) "SocketError", desc.str(), origin.str());
}
argout = Tango::string_dup(line.c_str());
/*----- PROTECTED REGION END -----*/ // SocketDS::readln
return argout;
}
//--------------------------------------------------------
/**
* Command ReadUntil related method
* Description: This command read data from the socket until the terminator has been reached.
*
* @param argin This is the terminator
* @returns This is the read string.
*/
//--------------------------------------------------------
Tango::DevString SocketDS::read_until(Tango::DevString argin)
{
Tango::DevString argout;
DEBUG_STREAM << "SocketDS::ReadUntil() - " << device_name << endl;
/*----- PROTECTED REGION ID(SocketDS::read_until) ENABLED START -----*/
boost::system::error_code error;
std::stringstream until, result;
until << argin;
result << "";
try
{
int size = boost::asio::read_until(*socket, *buffer, until.str());
char *contents = new char [size];
buffer->sgetn (contents,size);
result.write(contents, size);
}
catch (std::exception& e)
{
set_state(Tango::FAULT);
std::stringstream desc;
desc << "Connection exception: " << e.what();
set_status(desc.str());
std::stringstream origin;
origin << "Socket::read_until(" << argin << ") ";
Tango::Except::throw_exception((const char *) "SocketError", desc.str(), origin.str());
}
argout = Tango::string_dup(result.str().c_str());
/*----- PROTECTED REGION END -----*/ // SocketDS::read_until
return argout;
}
//--------------------------------------------------------
/**
* Command WriteReadUntil related method
* Description: Write to the socket and waits for the answer.
* The answer is triggered by select() which indicates that the the anser has arrived.
*
* @param argin [0] = Command string to be send, [1] = end of message character
* @returns Answer string.
*/
//--------------------------------------------------------
Tango::DevString SocketDS::write_read_until(const Tango::DevVarStringArray *argin)
{
Tango::DevString argout;
DEBUG_STREAM << "SocketDS::WriteReadUntil() - " << device_name << endl;
/*----- PROTECTED REGION ID(SocketDS::write_read_until) ENABLED START -----*/
stringstream command, until;
command << (*argin)[0];
until << (*argin)[1];
write(Tango::string_dup(command.str().c_str()));
argout = read_until(Tango::string_dup(until.str().c_str()));
/*----- PROTECTED REGION END -----*/ // SocketDS::write_read_until
return argout;
}
//--------------------------------------------------------
/**
* Command CheckConnection related method
* Description:
*
*/
//--------------------------------------------------------
void SocketDS::check_connection()
{
DEBUG_STREAM << "SocketDS::CheckConnection() - " << device_name << endl;
/*----- PROTECTED REGION ID(SocketDS::check_connection) ENABLED START -----*/
reconnect();
/*----- PROTECTED REGION END -----*/ // SocketDS::check_connection
}
//--------------------------------------------------------
/**
* Command WriteAndReadBinary related method
* Description: Write command and read reply (binary)
*
* @param argin command
* @returns Reply String
*/
//--------------------------------------------------------
Tango::DevVarCharArray *SocketDS::write_and_read_binary(const Tango::DevVarCharArray *argin)
{
Tango::DevVarCharArray *argout;
DEBUG_STREAM << "SocketDS::WriteAndReadBinary() - " << device_name << endl;
/*----- PROTECTED REGION ID(SocketDS::write_and_read_binary) ENABLED START -----*/
// Add your own code
write_binary(argin);
argout = read_binary();
//argout = read_until(Tango::string_dup(until.str().c_str()));
/*----- PROTECTED REGION END -----*/ // SocketDS::write_and_read_binary
return argout;
}
//--------------------------------------------------------
/**
* Command ReadBinary related method
* Description: Command used to read a string from the socket.
*
* @returns
*/
//--------------------------------------------------------
Tango::DevVarCharArray *SocketDS::read_binary()
{
Tango::DevVarCharArray *argout;
DEBUG_STREAM << "SocketDS::ReadBinary() - " << device_name << endl;
/*----- PROTECTED REGION ID(SocketDS::read_binary) ENABLED START -----*/
// Add your own code
std::stringstream result;
result << "";
int sizeN;
try
{
socket_read_binary(); // ??? FOR CONSTANT
result << buffer;
sizeN = result.str().size();
argout = new Tango::DevVarCharArray();
argout->length(sizeN);
}
catch (std::exception& e)
{
set_state(Tango::FAULT);
std::stringstream desc;
desc << "Connection exception: " << e.what();
set_status(desc.str());
std::stringstream origin;
origin << "Socket::read_binary()";
Tango::Except::throw_exception((const char *) "SocketError", desc.str(), origin.str());
}
string tmpS = result.str();
for (int i = 0; i < sizeN; i++)
{
(*argout)[i] = tmpS[i];
}
/*----- PROTECTED REGION END -----*/ // SocketDS::read_binary
return argout;
}
//--------------------------------------------------------
/**
* Command WriteBinary related method
* Description: Command used to send a string to the socket. (binary)
*
* @param argin
*/
//--------------------------------------------------------
void SocketDS::write_binary(const Tango::DevVarCharArray *argin)
{
DEBUG_STREAM << "SocketDS::WriteBinary() - " << device_name << endl;
/*----- PROTECTED REGION ID(SocketDS::write_binary) ENABLED START -----*/
// Add your own code
try
{
string temp;
int NN = argin->length();
for (int i = 0; i < NN; i++)
{
temp.push_back((*argin)[i]);
}
socket_write_binary(temp);
}
catch (std::exception& e)
{
set_state(Tango::FAULT);
std::stringstream desc;
desc << "Connection exception: " << e.what();
set_status(desc.str());
std::stringstream origin;
origin << "Socket::write(" << argin << ") ";
Tango::Except::throw_exception((const char *) "SocketError", desc.str(), origin.str());
}
/*----- PROTECTED REGION END -----*/ // SocketDS::write_binary
}
//--------------------------------------------------------
/**
* Command AvalaibleBytes related method
* Description: Determine the number of bytes available for reading
*
* @returns The number of bytes available for reading
*/
//--------------------------------------------------------
Tango::DevLong SocketDS::avalaible_bytes()
{
Tango::DevLong argout;
DEBUG_STREAM << "SocketDS::AvalaibleBytes() - " << device_name << endl;
/*----- PROTECTED REGION ID(SocketDS::avalaible_bytes) ENABLED START -----*/
argout = socket->available();
/*----- PROTECTED REGION END -----*/ // SocketDS::avalaible_bytes
return argout;
}
//--------------------------------------------------------
/**
* Method : SocketDS::add_dynamic_commands()
* Description : Create the dynamic commands if any
* for specified device.
*/
//--------------------------------------------------------
void SocketDS::add_dynamic_commands()
{
/*----- PROTECTED REGION ID(SocketDS::add_dynamic_commands) ENABLED START -----*/
// Add your own code to create and add dynamic commands if any
/*----- PROTECTED REGION END -----*/ // SocketDS::add_dynamic_commands
}
/*----- PROTECTED REGION ID(SocketDS::namespace_ending) ENABLED START -----*/
// Additional Methods
void SocketDS::check_deadline()
{
if (timer->expires_at() <= boost::asio::deadline_timer::traits_type::now())
{
// The deadline has passed. The socket is closed so that any outstanding
// asynchronous operations are cancelled. This allows the blocked
// connect(), read_line() or write_line() functions to return.
boost::system::error_code ignored_ec;
socket->close(ignored_ec);
set_state(Tango::FAULT);
set_status("Socket timeout..");
// There is no longer an active deadline. The expiry is set to positive
// infinity so that the actor takes no action until a new deadline is set.
timer->expires_at(boost::posix_time::pos_infin);
DEBUG_STREAM << "DEADLINE FAULT " << endl;
}
// Put the actor back to sleep.
timer->async_wait(bind(&SocketDS::check_deadline, this));
DEBUG_STREAM << "DEADLINE" << endl;
}
void SocketDS::socket_read_binary()
{
// Set a deadline for the asynchronous operation. Since this function uses
// a composed operation (async_read_until), the deadline applies to the
// entire operation, rather than individual reads from the socket.
timer->expires_from_now(boost::posix_time::milliseconds(readtimeout));
// Set up the variable that receives the result of the asynchronous
// operation. The error code is set to would_block to signal that the
// operation is incomplete. Asio guarantees that its asynchronous
// operations will never fail with would_block, so any other value in
// ec indicates completion.
boost::system::error_code ec = boost::asio::error::would_block;
// Start the asynchronous operation itself. The boost::lambda function
// object is used as a callback and will update the ec variable when the
// operation completes. The blocking_udp_client.cpp example shows how you
// can use boost::bind rather than boost::lambda.
boost::asio::async_read(*socket,*buffer, boost::asio::transfer_at_least(1), boost::lambda::var(ec) = boost::lambda::_1);
// Block until the asynchronous operation has completed.
do {io_service->run_one();} while (ec == boost::asio::error::would_block);
if (ec)
throw boost::system::system_error(ec);
}
void SocketDS::socket_write_binary(std::string message)
{
std::string data = message;
// Set a deadline for the asynchronous operation. Since this function uses
// a composed operation (async_write), the deadline applies to the entire
// operation, rather than individual writes to the socket.
timer->expires_from_now(boost::posix_time::milliseconds(readtimeout));
// Set up the variable that receives the result of the asynchronous
// operation. The error code is set to would_block to signal that the
// operation is incomplete. Asio guarantees that its asynchronous
// operations will never fail with would_block, so any other value in
// ec indicates completion.
boost::system::error_code ec = boost::asio::error::would_block;
// Start the asynchronous operation itself. The boost::lambda function
// object is used as a callback and will update the ec variable when the
// operation completes. The blocking_udp_client.cpp example shows how you
// can use boost::bind rather than boost::lambda.
boost::asio::async_write(*socket, boost::asio::buffer(data), boost::lambda::var(ec) = boost::lambda::_1);
// Block until the asynchronous operation has completed.
do io_service->run_one(); while (ec == boost::asio::error::would_block);
if (ec)
throw boost::system::system_error(ec);
}
void SocketDS::socket_read()
{
// Set a deadline for the asynchronous operation. Since this function uses
// a composed operation (async_read_until), the deadline applies to the
// entire operation, rather than individual reads from the socket.
timer->expires_from_now(boost::posix_time::milliseconds(readtimeout));
// Set up the variable that receives the result of the asynchronous
// operation. The error code is set to would_block to signal that the
// operation is incomplete. Asio guarantees that its asynchronous
// operations will never fail with would_block, so any other value in
// ec indicates completion.
boost::system::error_code ec = boost::asio::error::would_block;
// Start the asynchronous operation itself. The boost::lambda function
// object is used as a callback and will update the ec variable when the
// operation completes. The blocking_udp_client.cpp example shows how you
// can use boost::bind rather than boost::lambda.
boost::asio::async_read_until(*socket, *buffer, '\n', boost::lambda::var(ec) = boost::lambda::_1);
// Block until the asynchronous operation has completed.
do io_service->run_one(); while (ec == boost::asio::error::would_block);
if (ec)
throw boost::system::system_error(ec);
}
void SocketDS::socket_write(std::string message)
{
std::string data = message + "\n";
// Set a deadline for the asynchronous operation. Since this function uses
// a composed operation (async_write), the deadline applies to the entire
// operation, rather than individual writes to the socket.
timer->expires_from_now(boost::posix_time::milliseconds(readtimeout));
// Set up the variable that receives the result of the asynchronous
// operation. The error code is set to would_block to signal that the
// operation is incomplete. Asio guarantees that its asynchronous
// operations will never fail with would_block, so any other value in
// ec indicates completion.
boost::system::error_code ec = boost::asio::error::would_block;
// Start the asynchronous operation itself. The boost::lambda function
// object is used as a callback and will update the ec variable when the
// operation completes. The blocking_udp_client.cpp example shows how you
// can use boost::bind rather than boost::lambda.
boost::asio::async_write(*socket, boost::asio::buffer(data), boost::lambda::var(ec) = boost::lambda::_1);
// Block until the asynchronous operation has completed.
do io_service->run_one(); while (ec == boost::asio::error::would_block);
if (ec)
throw boost::system::system_error(ec);
}
/*----- PROTECTED REGION END -----*/ // SocketDS::namespace_ending
} // namespace