ChipMaster's trial hacks on C++CMS starting with v1.2.1. Not sure I'll follow on with the v2 since it looks to be breaking and mostly frivolous.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

133 lines
3.2 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
  4. //
  5. // See accompanying file COPYING.TXT file for licensing details.
  6. //
  7. ///////////////////////////////////////////////////////////////////////////////
  8. #define CPPCMS_SOURCE
  9. #include "tcp_messenger.h"
  10. #include "tcp_cache_client.h"
  11. #include "tcp_cache_protocol.h"
  12. #include <time.h>
  13. #include <string.h>
  14. namespace cppcms {
  15. namespace impl {
  16. tcp_cache::~tcp_cache()
  17. {
  18. // Nothing
  19. }
  20. void tcp_cache::rise(std::string const &trigger)
  21. {
  22. tcp_operation_header h=tcp_operation_header();
  23. h.opcode=opcodes::rise;
  24. h.size=trigger.size();
  25. std::string data=trigger;
  26. h.operations.rise.trigger_len=trigger.size();
  27. broadcast(h,data);
  28. }
  29. void tcp_cache::clear()
  30. {
  31. tcp_operation_header h=tcp_operation_header();
  32. h.opcode=opcodes::clear;
  33. h.size=0;
  34. std::string empty;
  35. broadcast(h,empty);
  36. }
  37. int tcp_cache::fetch( std::string const &key,
  38. std::string &a,
  39. std::set<std::string> *tags,
  40. time_t &timeout,
  41. uint64_t &generation,
  42. bool transfer_if_not_updated)
  43. {
  44. std::string data=key;
  45. tcp_operation_header h=tcp_operation_header();
  46. h.opcode=opcodes::fetch;
  47. h.size=data.size();
  48. h.operations.fetch.key_len=data.size();
  49. if(transfer_if_not_updated) {
  50. h.operations.fetch.transfer_if_not_uptodate=1;
  51. h.operations.fetch.current_gen=generation;
  52. }
  53. if(tags)
  54. h.operations.fetch.transfer_triggers = 1;
  55. get(key).transmit(h,data);
  56. if(transfer_if_not_updated && h.opcode==opcodes::uptodate)
  57. return up_to_date;
  58. if(h.opcode!=opcodes::data)
  59. return not_found;
  60. timeout = to_time_t(h.operations.data.timeout);
  61. generation=h.operations.data.generation;
  62. char const *ptr=data.c_str();
  63. a.assign(ptr,h.operations.data.data_len);
  64. ptr+=h.operations.data.data_len;
  65. int len=h.operations.data.triggers_len;
  66. while(len>0) {
  67. std::string tag;
  68. unsigned tmp_len=strlen(ptr);
  69. tag.assign(ptr,tmp_len);
  70. ptr+=tmp_len+1;
  71. len-=tmp_len+1;
  72. tags->insert(tag);
  73. }
  74. return found;
  75. }
  76. void tcp_cache::stats(unsigned &keys,unsigned &triggers)
  77. {
  78. keys=0; triggers=0;
  79. for(int i=0;i<conns;i++) {
  80. tcp_operation_header h=tcp_operation_header();
  81. std::string data;
  82. h.opcode=opcodes::stats;
  83. tcp[i].transmit(h,data);
  84. if(h.opcode==opcodes::out_stats) {
  85. keys+=h.operations.out_stats.keys;
  86. triggers+=h.operations.out_stats.triggers;
  87. }
  88. }
  89. }
  90. void tcp_cache::store( std::string const &key,
  91. std::string const &a,
  92. std::set<std::string> const &triggers,
  93. time_t timeout)
  94. {
  95. tcp_operation_header h=tcp_operation_header();
  96. std::string data;
  97. h.opcode=opcodes::store;
  98. data.append(key);
  99. h.operations.store.key_len=key.size();
  100. data.append(a);
  101. h.operations.store.data_len=a.size();
  102. h.operations.store.timeout=timeout;
  103. unsigned tlen=0;
  104. for(std::set<std::string>::const_iterator p=triggers.begin(),e=triggers.end();p!=e;++p) {
  105. tlen+=p->size()+1;
  106. data.append(p->c_str(),p->size()+1);
  107. }
  108. h.operations.store.triggers_len=tlen;
  109. h.size=data.size();
  110. get(key).transmit(h,data);
  111. }
  112. } // impl
  113. } // cppcms