1
0

cardMgr.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #ifndef __INCLUDE_CARD_MGR__
  2. #define __INCLUDE_CARD_MGR__
  3. #include <memory>
  4. #include <thread>
  5. #include <boost/config.hpp>
  6. #include <boost/graph/adjacency_list.hpp>
  7. #include <boost/archive/text_oarchive.hpp>
  8. #include <boost/graph/vector_as_graph.hpp>
  9. #include "tqueue.h"
  10. #include "log.h"
  11. #include "geo_hash.h"
  12. struct card_graph
  13. {
  14. typedef boost::adjacency_list<boost::vecS,boost::vecS,boost::bidirectionalS> myGraph_vv;
  15. myGraph_vv m_g;
  16. uint32_t _count=0;
  17. void add_edge(uint32_t c1,uint32_t c2)
  18. {
  19. log_info("add_edge:%d,%d",c1,c2);
  20. boost::add_edge(c1,c2,m_g);
  21. boost::add_edge(c2,c1,m_g);
  22. _count=boost::num_vertices(m_g);
  23. }
  24. void clear_vertex(uint32_t c)
  25. {
  26. if(c>=_count) return;
  27. boost::clear_vertex(c,m_g);
  28. }
  29. void remove_edge(uint32_t c1,uint32_t c2)
  30. {
  31. if(c1>=_count || c2>=_count) return;
  32. boost::remove_edge(c1,c2,m_g);
  33. boost::remove_edge(c2,c1,m_g);
  34. }
  35. std::vector<uint32_t> getcard(uint32_t c)
  36. {
  37. log_info("--------getcard---------num_edges:%d,%d",boost::num_edges(m_g),_count);
  38. std::vector<uint32_t> tmp;
  39. if(c>=_count)return tmp;
  40. boost::graph_traits<myGraph_vv>::adjacency_iterator ai_it, ai_it_end;
  41. boost::tie(ai_it, ai_it_end) = boost::adjacent_vertices(c, m_g);
  42. while(ai_it != ai_it_end)
  43. {
  44. log_info("get_card%d:",c);
  45. log_info("get_card_%d:",*ai_it);
  46. tmp.push_back(*ai_it);
  47. ai_it++;
  48. }
  49. return std::move(tmp);
  50. }
  51. };
  52. enum{
  53. CMD_CLEAR,
  54. CMD_HANDLE
  55. };
  56. struct Msg
  57. {
  58. int cmd;
  59. int x;
  60. int y;
  61. uint64_t cardid;
  62. int type;
  63. };
  64. class CardFactory;
  65. struct cardMgr
  66. {
  67. cardMgr();
  68. static cardMgr* instance();
  69. void run();
  70. void onMessage(const Msg &m);
  71. void clear(uint64_t);
  72. void handleMessage(const Msg &m);
  73. void erase(const std::map<uint64_t ,std::string>&);
  74. void erase(uint64_t);
  75. void tryPut(Msg &m)
  76. {
  77. if(!m_queue.tryPut(m))
  78. log_error("m_queue tryPut makes error.");
  79. }
  80. void addVertex(uint64_t c1,uint64_t c2)
  81. {
  82. m_cgraph.add_edge(c1,c2);
  83. }
  84. std::vector<uint64_t> getcard(uint64_t cid)
  85. {
  86. std::vector<uint64_t> tmpv;
  87. auto v= m_cgraph.getcard(cid);
  88. std::for_each(v.begin(),v.end(),[cid,&tmpv](uint32_t cardid){uint64_t id=(cid>>32<<32)|cardid;tmpv.push_back(id);});
  89. return std::move(tmpv);
  90. }
  91. void remove_edge(uint64_t c1, uint64_t c2)
  92. {
  93. m_cgraph.remove_edge(c1,c2);
  94. }
  95. ~cardMgr();
  96. private:
  97. BoundedQueue<Msg> m_queue;
  98. bool m_bstop;
  99. geo_list m_glist;
  100. card_graph m_cgraph;
  101. std::unique_ptr<std::thread> m_pThread;
  102. std::array<std::shared_ptr<CardFactory>,2> m_cf;
  103. };
  104. #endif