00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <interface/interface.h>
00025
00026 #include <interface/mediators/interface_mediator.h>
00027 #include <interface/mediators/message_mediator.h>
00028 #include <core/threading/refc_rwlock.h>
00029 #include <core/exceptions/system.h>
00030 #include <utils/time/clock.h>
00031 #include <utils/time/time.h>
00032
00033 #include <cstring>
00034 #include <cstdio>
00035 #include <cstdlib>
00036 #include <typeinfo>
00037 #include <regex.h>
00038
00039 namespace fawkes {
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 InterfaceWriteDeniedException::InterfaceWriteDeniedException(const char *type,
00053 const char *id,
00054 const char *msg)
00055 : Exception("This interface instance '%s' of type '%s' is not opened for writing. %s",
00056 id, type, msg)
00057 {
00058 }
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 InterfaceMessageEnqueueException::InterfaceMessageEnqueueException(const char *type,
00071 const char *id)
00072 : Exception("This interface instance '%s' of type '%s' IS opened for writing, but "
00073 "messages can only be enqueued on reading interfaces.", id, type)
00074 {
00075 }
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 InterfaceInvalidMessageException::InterfaceInvalidMessageException(const Interface *interface,
00088 const Message *message)
00089 : Exception("Message of type '%s' cannot be enqueued in interface of type '%s'",
00090 message->type(), interface->type())
00091 {
00092 }
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105 InterfaceInvalidException::InterfaceInvalidException(const Interface *interface,
00106 const char *method)
00107 : Exception("The interface %s (instance serial %u) is invalid. You cannot call %s anymore.",
00108 interface->uid(), interface->serial(), method)
00109 {
00110 }
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196 Interface::Interface()
00197 {
00198 __write_access = false;
00199 __rwlock = NULL;
00200 __valid = true;
00201 __next_message_id = 0;
00202 __num_fields = 0;
00203 __fieldinfo_list = NULL;
00204 __messageinfo_list = NULL;
00205 __clock = Clock::instance();
00206 __timestamp = new Time(0, 0);
00207 __local_read_timestamp = new Time(0, 0);
00208 __auto_timestamping = true;
00209 data_changed = false;
00210 memset(__hash, 0, __INTERFACE_HASH_SIZE);
00211 memset(__hash_printable, 0, __INTERFACE_HASH_SIZE * 2 + 1);
00212
00213 data_ptr = NULL;
00214 data_size = 0;
00215
00216 __message_queue = new MessageQueue();
00217 }
00218
00219
00220
00221 Interface::~Interface()
00222 {
00223 if ( __rwlock) __rwlock->unref();
00224 delete __message_queue;
00225
00226 interface_fieldinfo_t *finfol = __fieldinfo_list;
00227 while ( finfol ) {
00228 __fieldinfo_list = __fieldinfo_list->next;
00229 free(finfol);
00230 finfol = __fieldinfo_list;
00231 }
00232
00233 interface_messageinfo_t *minfol = __messageinfo_list;
00234 while ( minfol ) {
00235 __messageinfo_list = __messageinfo_list->next;
00236 free(minfol);
00237 minfol = __messageinfo_list;
00238 }
00239 delete __timestamp;
00240 }
00241
00242
00243
00244
00245
00246
00247
00248 const unsigned char *
00249 Interface::hash() const
00250 {
00251 return __hash;
00252 }
00253
00254
00255
00256
00257
00258 const char *
00259 Interface::hash_printable() const
00260 {
00261 return __hash_printable;
00262 }
00263
00264
00265
00266
00267
00268 void
00269 Interface::set_hash(unsigned char *ihash)
00270 {
00271 memcpy(__hash, ihash, __INTERFACE_HASH_SIZE);
00272 for (size_t s = 0; s < __INTERFACE_HASH_SIZE; ++s) {
00273 snprintf(&__hash_printable[s*2], 3, "%02X", __hash[s]);
00274 }
00275 }
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288 void
00289 Interface::add_fieldinfo(interface_fieldtype_t type, const char *name,
00290 size_t length, void *value, const char *enumtype)
00291 {
00292 interface_fieldinfo_t *infol = __fieldinfo_list;
00293 interface_fieldinfo_t *newinfo = (interface_fieldinfo_t *)malloc(sizeof(interface_fieldinfo_t));
00294
00295 newinfo->type = type;
00296 newinfo->enumtype = enumtype;
00297 newinfo->name = name;
00298 newinfo->length = length;
00299 newinfo->value = value;
00300 newinfo->next = NULL;
00301
00302 if ( infol == NULL ) {
00303
00304 __fieldinfo_list = newinfo;
00305 } else {
00306
00307 while ( infol->next != NULL ) {
00308 infol = infol->next;
00309 }
00310 infol->next = newinfo;
00311 }
00312
00313 ++__num_fields;
00314 }
00315
00316
00317
00318
00319
00320
00321
00322
00323 void
00324 Interface::add_messageinfo(const char *type)
00325 {
00326 interface_messageinfo_t *infol = __messageinfo_list;
00327 interface_messageinfo_t *newinfo = (interface_messageinfo_t *)malloc(sizeof(interface_messageinfo_t));
00328
00329 newinfo->type = type;
00330 newinfo->next = NULL;
00331
00332 if ( infol == NULL ) {
00333
00334 __messageinfo_list = newinfo;
00335 } else {
00336
00337 while ( infol->next != NULL ) {
00338 infol = infol->next;
00339 }
00340 infol->next = newinfo;
00341 }
00342 }
00343
00344
00345
00346
00347
00348
00349 std::list<const char *>
00350 Interface::get_message_types()
00351 {
00352 std::list<const char *> types;
00353 interface_messageinfo_t *cur = __messageinfo_list;
00354
00355 while ( cur != NULL ) {
00356 types.push_back(cur->type);
00357 cur = cur->next;
00358 }
00359
00360 return types;
00361 }
00362
00363
00364
00365
00366
00367
00368 size_t
00369 Interface::hash_size() const
00370 {
00371 return __INTERFACE_HASH_SIZE;
00372 }
00373
00374
00375
00376
00377
00378
00379 const void *
00380 Interface::datachunk() const
00381 {
00382 return data_ptr;
00383 }
00384
00385
00386
00387
00388
00389 bool
00390 Interface::is_writer() const
00391 {
00392 return __write_access;
00393 }
00394
00395
00396
00397
00398
00399
00400
00401
00402 void
00403 Interface::set_validity(bool valid)
00404 {
00405 __rwlock->lock_for_write();
00406 __valid = valid;
00407 __rwlock->unlock();
00408 }
00409
00410
00411
00412
00413
00414 bool
00415 Interface::is_valid() const
00416 {
00417 return __valid;
00418 }
00419
00420
00421
00422
00423
00424 void
00425 Interface::read()
00426 {
00427 __rwlock->lock_for_read();
00428 if ( __valid ) {
00429 memcpy(data_ptr, __mem_data_ptr, data_size);
00430 *__local_read_timestamp = *__timestamp;
00431 __timestamp->set_time(data_ts->timestamp_sec, data_ts->timestamp_usec);
00432 } else {
00433 __rwlock->unlock();
00434 throw InterfaceInvalidException(this, "read()");
00435 }
00436 __rwlock->unlock();
00437 }
00438
00439
00440
00441
00442
00443 void
00444 Interface::write()
00445 {
00446 if ( ! __write_access ) {
00447 throw InterfaceWriteDeniedException(__type, __id, "Cannot write.");
00448 }
00449
00450 __rwlock->lock_for_write();
00451 if ( __valid ) {
00452 memcpy(__mem_data_ptr, data_ptr, data_size);
00453 if (data_changed) {
00454 if (__auto_timestamping) __timestamp->stamp();
00455 long sec = 0, usec = 0;
00456 __timestamp->get_timestamp(sec, usec);
00457 data_ts->timestamp_sec = sec;
00458 data_ts->timestamp_usec = usec;
00459 data_changed = false;
00460 }
00461 } else {
00462 __rwlock->unlock();
00463 throw InterfaceInvalidException(this, "write()");
00464 }
00465 __rwlock->unlock();
00466
00467 __interface_mediator->notify_of_data_change(this);
00468 }
00469
00470
00471
00472
00473
00474 unsigned int
00475 Interface::datasize() const
00476 {
00477 return data_size;
00478 }
00479
00480
00481
00482
00483
00484
00485
00486 void
00487 Interface::set_type_id(const char *type, const char *id)
00488 {
00489 strncpy(__type, type, __INTERFACE_TYPE_SIZE);
00490 strncpy(__id, id, __INTERFACE_ID_SIZE);
00491 snprintf(__uid, __INTERFACE_UID_SIZE, "%s::%s", type, id);
00492 }
00493
00494
00495
00496
00497
00498 void
00499 Interface::set_instance_serial(unsigned short instance_serial)
00500 {
00501 __instance_serial = instance_serial;
00502 }
00503
00504
00505
00506
00507
00508
00509 void
00510 Interface::set_mediators(InterfaceMediator *iface_mediator, MessageMediator *msg_mediator)
00511 {
00512 __interface_mediator = iface_mediator;
00513 __message_mediator = msg_mediator;
00514 }
00515
00516
00517
00518
00519
00520
00521
00522 void
00523 Interface::set_memory(unsigned int serial, void *real_ptr, void *data_ptr)
00524 {
00525 __mem_serial = serial;
00526 __mem_real_ptr = real_ptr;
00527 __mem_data_ptr = data_ptr;
00528 }
00529
00530
00531
00532
00533
00534
00535 void
00536 Interface::set_readwrite(bool write_access, RefCountRWLock *rwlock)
00537 {
00538 __write_access = write_access;
00539 __rwlock = rwlock;
00540 }
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553 bool
00554 Interface::operator==(Interface &comp) const
00555 {
00556 return ( (strncmp(__type, comp.__type, sizeof(__type)) == 0) &&
00557 (strncmp(__id, comp.__id, sizeof(__id)) == 0) );
00558 }
00559
00560
00561
00562
00563
00564
00565 bool
00566 Interface::oftype(const char *interface_type) const
00567 {
00568 return (strncmp(this->__type, interface_type, sizeof(this->__type)) == 0);
00569 }
00570
00571
00572
00573
00574
00575 const char *
00576 Interface::type() const
00577 {
00578 return __type;
00579 }
00580
00581
00582
00583
00584
00585 const char *
00586 Interface::id() const
00587 {
00588 return __id;
00589 }
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599 const char *
00600 Interface::uid() const
00601 {
00602 return __uid;
00603 }
00604
00605
00606
00607
00608
00609 unsigned short
00610 Interface::serial() const
00611 {
00612 return __instance_serial;
00613 }
00614
00615
00616
00617
00618
00619 unsigned int
00620 Interface::mem_serial() const
00621 {
00622 return __mem_serial;
00623 }
00624
00625
00626
00627
00628
00629
00630 const Time *
00631 Interface::timestamp() const
00632 {
00633 return __timestamp;
00634 }
00635
00636
00637
00638
00639
00640
00641 void
00642 Interface::set_timestamp(const Time *t)
00643 {
00644 if (!__auto_timestamping) throw Exception("Auto timestamping enabled, cannot "
00645 "set explicit timestamp");
00646 if (!__write_access) throw Exception("Timestamp can only be set on writing "
00647 "instance");
00648
00649 if (t) {
00650 *__timestamp = t;
00651 } else {
00652 __timestamp->stamp();
00653 }
00654 data_changed = true;
00655 }
00656
00657
00658
00659
00660
00661 void
00662 Interface::set_clock(Clock *clock)
00663 {
00664 __clock = clock;
00665 __timestamp->set_clock(clock);
00666 }
00667
00668
00669
00670
00671
00672 void
00673 Interface::set_auto_timestamping(bool enabled)
00674 {
00675 __auto_timestamping = enabled;
00676 }
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686 bool
00687 Interface::changed() const
00688 {
00689 return (*__timestamp != __local_read_timestamp);
00690 }
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701 void
00702 Interface::set_from_chunk(void *chunk)
00703 {
00704
00705
00706
00707 memcpy(data_ptr, chunk, data_size);
00708 }
00709
00710
00711
00712
00713
00714
00715
00716 bool
00717 Interface::has_writer() const
00718 {
00719 return __interface_mediator->exists_writer(this);
00720 }
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743 unsigned int
00744 Interface::num_readers() const
00745 {
00746 return __interface_mediator->num_readers(this);
00747 }
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762 unsigned int
00763 Interface::msgq_enqueue(Message *message)
00764 {
00765 if ( __write_access ) {
00766 throw InterfaceMessageEnqueueException(__type, __id);
00767 }
00768
00769 if ( message_valid(message) ) {
00770 message->set_interface(this);
00771 message->set_id(next_msg_id());
00772
00773 __message_mediator->transmit(message);
00774 unsigned int msgid = message->id();
00775 message->unref();
00776 return msgid;
00777 } else {
00778 throw InterfaceInvalidMessageException(this, message);
00779 }
00780 }
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797 unsigned int
00798 Interface::msgq_enqueue_copy(Message *message)
00799 {
00800 if ( __write_access ) {
00801 throw InterfaceMessageEnqueueException(__type, __id);
00802 }
00803 if ( message == NULL ) {
00804 throw NullPointerException("Message may not be NULL");
00805 }
00806
00807 if ( message_valid(message) ) {
00808 Message *mcopy = message->clone();
00809 mcopy->set_interface(this);
00810 mcopy->set_id(next_msg_id());
00811 __message_mediator->transmit(mcopy);
00812 unsigned int msgid = mcopy->id();
00813 mcopy->unref();
00814 message->set_id(msgid);
00815 return msgid;
00816 } else {
00817 throw InterfaceInvalidMessageException(this, message);
00818 }
00819 }
00820
00821
00822
00823
00824
00825
00826
00827 void
00828 Interface::msgq_append(Message *message)
00829 {
00830 if ( ! __write_access ) {
00831 throw InterfaceWriteDeniedException(__type, __id, "Cannot work on message queue on "
00832 "reading instance of an interface (append).");
00833 }
00834
00835 __message_queue->append(message);
00836 }
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847 void
00848 Interface::msgq_remove(Message *message)
00849 {
00850 if ( ! __write_access ) {
00851 throw InterfaceWriteDeniedException(__type, __id, "Cannot work on message queue on "
00852 "reading instance of an interface (remove msg).");
00853 }
00854
00855 return __message_queue->remove(message);
00856 }
00857
00858
00859
00860
00861
00862
00863
00864 void
00865 Interface::msgq_remove(unsigned int message_id)
00866 {
00867 if ( ! __write_access ) {
00868 throw InterfaceWriteDeniedException(__type, __id, "Cannot work on message queue on "
00869 "reading instance of an interface (remove id).");
00870 }
00871
00872 return __message_queue->remove(message_id);
00873 }
00874
00875
00876
00877
00878
00879
00880 unsigned int
00881 Interface::msgq_size()
00882 {
00883 if ( ! __write_access ) {
00884 throw InterfaceWriteDeniedException(__type, __id, "Cannot work on message queue on "
00885 "reading instance of an interface (size).");
00886 }
00887
00888 return __message_queue->size();
00889 }
00890
00891
00892
00893
00894
00895
00896 bool
00897 Interface::msgq_empty()
00898 {
00899 if ( ! __write_access ) {
00900 throw InterfaceWriteDeniedException(__type, __id, "Cannot work on message queue on "
00901 "reading instance of an interface (empty).");
00902 }
00903
00904 return __message_queue->empty();
00905 }
00906
00907
00908
00909
00910
00911
00912 void
00913 Interface::msgq_flush()
00914 {
00915 if ( ! __write_access ) {
00916 throw InterfaceWriteDeniedException(__type, __id, "Cannot work on message queue on "
00917 "reading instance of an interface (flush).");
00918 }
00919
00920 __message_queue->flush();
00921 }
00922
00923
00924
00925
00926
00927
00928 void
00929 Interface::msgq_lock()
00930 {
00931 if ( ! __write_access ) {
00932 throw InterfaceWriteDeniedException(__type, __id, "Cannot work on message queue on "
00933 "reading instance of an interface (lock).");
00934 }
00935
00936 __message_queue->lock();
00937 }
00938
00939
00940
00941
00942
00943
00944
00945
00946 bool
00947 Interface::msgq_try_lock()
00948 {
00949 if ( ! __write_access ) {
00950 throw InterfaceWriteDeniedException(__type, __id, "Cannot work on message queue on "
00951 "reading instance of an interface (try_lock).");
00952 }
00953
00954 return __message_queue->try_lock();
00955 }
00956
00957
00958
00959
00960
00961
00962 void
00963 Interface::msgq_unlock()
00964 {
00965 if ( ! __write_access ) {
00966 throw InterfaceWriteDeniedException(__type, __id, "Cannot work on message queue on "
00967 "reading instance of an interface (unlock).");
00968 }
00969
00970 __message_queue->unlock();
00971 }
00972
00973
00974
00975
00976
00977
00978
00979 MessageQueue::MessageIterator
00980 Interface::msgq_begin()
00981 {
00982 if ( ! __write_access ) {
00983 throw InterfaceWriteDeniedException(__type, __id, "Cannot work on message queue on "
00984 "reading instance of an interface (begin).");
00985 }
00986
00987 return __message_queue->begin();
00988 }
00989
00990
00991
00992
00993
00994
00995
00996
00997 MessageQueue::MessageIterator
00998 Interface::msgq_end()
00999 {
01000 if ( ! __write_access ) {
01001 throw InterfaceWriteDeniedException(__type, __id, "Cannot work on message queue on "
01002 "reading instance of an interface (end).");
01003 }
01004
01005 return __message_queue->end();
01006 }
01007
01008
01009
01010
01011
01012
01013 Message *
01014 Interface::msgq_first()
01015 {
01016 if ( ! __write_access ) {
01017 throw InterfaceWriteDeniedException(__type, __id, "Cannot work on message queue on "
01018 "reading instance of an interface (first).");
01019 }
01020
01021 return __message_queue->first();
01022 }
01023
01024
01025
01026
01027 void
01028 Interface::msgq_pop()
01029 {
01030 if ( ! __write_access ) {
01031 throw InterfaceWriteDeniedException(__type, __id, "Cannot work on message queue on "
01032 "reading instance of an interface (pop).");
01033 }
01034
01035 __message_queue->pop();
01036 }
01037
01038
01039
01040
01041
01042 InterfaceFieldIterator
01043 Interface::fields()
01044 {
01045 return InterfaceFieldIterator(this, __fieldinfo_list);
01046 }
01047
01048
01049
01050
01051
01052 InterfaceFieldIterator
01053 Interface::fields_end()
01054 {
01055 return InterfaceFieldIterator();
01056 }
01057
01058
01059
01060
01061
01062 unsigned int
01063 Interface::num_fields()
01064 {
01065 return __num_fields;
01066 }
01067
01068
01069
01070
01071
01072
01073
01074
01075
01076 void
01077 Interface::parse_uid(const char *uid, char **type, char **id)
01078 {
01079 regex_t re;
01080 int ec = 0;
01081
01082 #define str(s) #s
01083 #define xstr(s) str(s)
01084 if ((ec = regcomp(&re,
01085 "^([a-zA-Z0-9]{1," xstr(__INTERFACE_TYPE_SIZE) "})::"
01086 "([a-zA-Z0-9 _\\.-]{1," xstr(__INTERFACE_ID_SIZE) "})$",
01087 REG_EXTENDED)) != 0) {
01088 char errbuf[1024];
01089 regerror(ec, &re, errbuf, 1024);
01090 throw Exception("Failed to created regular expression to parse UID (%s)",
01091 errbuf);
01092 }
01093 regmatch_t matches[3];
01094 if (regexec(&re, uid, 3, matches, 0) != 0) {
01095 regfree(&re);
01096 throw Exception("Failed to match UID %s, format error.", uid);
01097 }
01098
01099 *type = strndup(&(uid[matches[1].rm_so]), matches[1].rm_eo - matches[1].rm_so);
01100 *id = strndup(&(uid[matches[2].rm_so]), matches[2].rm_eo - matches[2].rm_so);
01101 }
01102
01103 }