1
0
zzj 6 лет назад
Родитель
Сommit
b2d7e90876
7 измененных файлов с 64 добавлено и 29 удалено
  1. 27 24
      card.cpp
  2. 3 2
      card.h
  3. 2 0
      main.cpp
  4. 1 1
      worker.cpp
  5. 5 2
      write-copy.h
  6. 1 0
      zloop.h
  7. 25 0
      znet.cpp

+ 27 - 24
card.cpp

@@ -21,7 +21,7 @@ struct card_location_base
 {
     std::shared_ptr<select_tool> m_sel_tool=nullptr;
     std::shared_ptr<smooth_tool> m_smo_tool=nullptr;
-	card_message_handle *m_message_handle=nullptr;
+	std::unique_ptr<card_message_handle> m_message_handle=nullptr;
     card_location_base()=default;
     card_location_base(std::string type)
     {
@@ -161,6 +161,14 @@ struct card_message_handle
 		}
 	}
 
+	~card_message_handle()
+	{
+		for(auto&it:m_ct_list)
+		{
+			delete it;
+		}
+	}
+
 	void on_message(zloop<task*> * loop,const message_locinfo&loc,bool is_history)
 	{
 		if(is_history)
@@ -193,7 +201,7 @@ struct person:card_location_base,card_area
 	person(std::string type)
         :card_location_base(type)
 	{
-        m_message_handle = new  card_message_handle(this);
+        m_message_handle .reset(new  card_message_handle(this));
 	}
 
 	void on_message(zloop<task*> * loop,const message_locinfo&loc,bool is_history)
@@ -207,7 +215,7 @@ struct car:card_location_base,card_area
     car(std::string type)
         :card_location_base(type)
     {
-        m_message_handle = new  card_message_handle(this);
+        m_message_handle .reset(new  card_message_handle(this));
     }
 	void on_message(zloop<task*> * loop,const message_locinfo&loc,bool is_history)
 	{
@@ -217,33 +225,28 @@ struct car:card_location_base,card_area
 
 loc_tool_main one_ct_message_handle::m_loc_tool;
 
-struct card_list_impl:card_list
+void card_list::init_card_from_db()
 {
-	card_list_impl()
-	{
-	}
+	card_list::
+		instance()->add(0,std::make_shared<car>(""));
+}
 
-	void init_card_from_db()
-	{
-	}
+void card_list::on_message(zloop<task*> *loop,const message_locinfo&loc,bool is_history)
+{
+	std::shared_ptr<card_location_base> c=get(loc.long_id());
 
-	void on_message(zloop<task*> *loop,const message_locinfo&loc,bool is_history)
+	if(c==nullptr)
 	{
-		std::shared_ptr<card_location_base> c=get(loc.long_id());
-
-		if(c==nullptr)
-		{
-			log_warn("数据库中未定义该卡的信息,type=%d,card_id=%d", loc.m_card_type, loc.m_card_id);
-			return;
-		}
+		log_warn("数据库中未定义该卡的信息,type=%d,card_id=%d", loc.m_card_type, loc.m_card_id);
+		return;
+	}
 
-		log_info("card message:site=%d,ant=%d,card=%d,tof=%lld,rav=%02X,acc=%02X,rssi=%d,stamp=%llu",
-				loc.m_site_id,loc.m_ant_id,loc.m_card_id,loc.m_tof,loc.m_rav,loc.m_acc,loc.m_rssi,loc.m_time_stamp);
+	log_info("card message:site=%d,ant=%d,card=%d,tof=%lld,rav=%02X,acc=%02X,rssi=%d,stamp=%llu",
+			loc.m_site_id,loc.m_ant_id,loc.m_card_id,loc.m_tof,loc.m_rav,loc.m_acc,loc.m_rssi,loc.m_time_stamp);
 
-		c->on_message(loop,loc,is_history);
-	}
-};
+	c->on_message(loop,loc,is_history);
+}
 
 template<> std::shared_ptr<card_list> 
-single_base<card_list, int64_t, std::shared_ptr<card_location_base>>::m_instance=std::make_shared<card_list_impl>();
+single_base<card_list, int64_t, std::shared_ptr<card_location_base>>::m_instance=std::make_shared<card_list>();
 

+ 3 - 2
card.h

@@ -13,10 +13,11 @@ template<typename T> struct zloop;
 
 struct card_list:single_base<card_list,int64_t,std::shared_ptr<card_location_base>>
 {
-	virtual void on_message(zloop<task*> *loop,const message_locinfo&loc,bool is_history)=0;
-	virtual void init_card_from_db()=0;
+	virtual void on_message(zloop<task*> *loop,const message_locinfo&loc,bool is_history);
+	virtual void init_card_from_db();
 	virtual ~card_list(){}
 };
 
 #endif
 
+

+ 2 - 0
main.cpp

@@ -1,3 +1,4 @@
+#include <ev++.h>
 #include <log.h>
 #include <net-service.h>
 #include "db_api/CDBConnPool.h"
@@ -31,6 +32,7 @@ struct Init_Setting
 		DBSetting.TimeOut = config.get("db.conn_timeout",5);
 		DBSetting.PoolSize = config.get("db.conn_timeout",30);
 		_mysql_init(DBSetting);
+
 	}
 
 	void _mysql_init(YADB::_DB_POOL_SETTING_ &dps)

+ 1 - 1
worker.cpp

@@ -120,7 +120,7 @@ struct worker_impl:worker
 worker_impl _worker_impl;
 worker*worker::instance()
 {
-	_worker_impl.init(4);
+	_worker_impl.init(1);
 	return &_worker_impl;
 }
 

+ 5 - 2
write-copy.h

@@ -18,17 +18,19 @@ struct write_copy_base:acceptor<V>
 			v.visit(me.second);
 		}
 	}
+
 	std::unordered_map<K,V> m_map;
 	write_copy_base()
 	{
 	
 	}
 
-	V	get(K k)const
+	V get(K k)const
 	{
 		return m_map[k];
 	}
-	V	get(K k)
+
+	V get(K k)
 	{
 		auto serch = m_map.find(k);
 		if(serch != m_map.end())
@@ -138,3 +140,4 @@ struct single_base:write_copy_base<T,K,V>
 
 #endif
 
+

+ 1 - 0
zloop.h

@@ -8,6 +8,7 @@
 #include <ev++.h>
 
 struct zloop_base:ev::dynamic_loop
+//struct zloop_base:ev::loop_ref
 {
 	std::atomic<bool>  m_stop_flag{false};
 	ev::async m_async;

+ 25 - 0
znet.cpp

@@ -132,6 +132,29 @@ struct fd_io:ev::io
 	}
 };
 
+struct stdin_io:fd_io
+{
+	stdin_io(io_context&ic)
+		:fd_io(ic,0)
+	{}
+
+	virtual void operator()(ev::io &w, int)
+	{
+		char buf[256];
+		if(!gets(buf))
+			return;
+
+		if(strchr(buf,'X'))
+		{
+			log_info("stdin input 'X', exiting...");
+
+			worker::instance()->stop();
+			stop();
+			m_ic.stop();
+		}
+	}
+};
+
 struct sock_client:fd_io,client_ex
 {
 	io_context&m_ic;
@@ -469,6 +492,8 @@ struct main_loop:io_context
 
 		sock_listen _1(*this,fd);
 
+		stdin_io _2(*this);
+
 		block_sig(SIGPIPE);
 		signal_w sint(*this,SIGINT),term(*this,SIGTERM);
 		ev::dynamic_loop::run(0);