dht協議中有四種查詢操作:
主要負責通過udp與外部節點互動,封裝4種基本操作的請求以及響應。
ping:檢查乙個節點是否「存活」
find_node:向乙個節點傳送查詢節點的請求
get_peers:向乙個節點傳送查詢資源的請求
具體的協議可參考:
delimiter $$
use `torrent`$$
drop procedure if exists `updatehots_pro`$$
create definer=`shixq`@`%` procedure `updatehots_pro`(in tid varchar(64))
begin
declare t_error integer default 0;
declare t_id varchar(64) default null;
declare t_hots json default null;
declare hotslength integer default 0;
declare mtime datetime;
declare mhot integer default 0;
declare mnow varchar(20);
declare continue handler for sqlexception set t_error=1;
start transaction;
select t.id,t.hots into t_id,t_hots from t_torrent t where t.id = tid;
if t_id is not null then
set mnow = date_format(now(),'%y-%m-%d');
if t_hots is not null then
set mtime = str_to_date(replace(json_extract(t_hots,'$[0].time'),'"',''),'%y-%m-%d');
if str_to_date(mnow,'%y-%m-%d') = mtime then /* 和第乙個時間相同就在第乙個基礎上+1*/
set mhot = json_extract(t_hots,'$[0].hot');
update t_torrent t set t.hot = mhot + 1, t.hots = json_replace(t_hots,'$[0].hot',mhot + 1) where t.id = tid;
elseif str_to_date(mnow,'%y-%m-%d') > mtime then /* 在頭部插入*/
set hotslength = ceil((length(t_hots) - length(replace(t_hots,'},',''))) / 2 + 1);
if hotslength = 14 then /* 只保留最近兩周熱度*/
set t_hots = json_remove(t_hots,'$[13]');
end if;
if 'object' = json_type(t_hots) then
set t_hots = json_array(json_object("time",mnow,"hot",1),t_hots);
elseif 'array' = json_type(t_hots) then
set t_hots = json_merge(json_object("time",mnow,"hot",1),t_hots);
end if;
update t_torrent t set t.hot = 1, t.hots = t_hots where t.id = tid;
end if;
else
update t_torrent t set t.hot = 1, t.hots = json_object("time",mnow,"hot",1) where t.id = tid;
end if;
end if;
if t_error = 1 then
rollback;
else
commit;
end if;
select t_error,mnow,mtime;
end$$
delimiter ;
DHT網路爬蟲
p2p系統的應用越來越廣泛,在檔案共享 流 服務 即時通訊交流 計算和儲存能力共享以及協同處理與服務等方面都能看到p2p的存在,一些p2p應用如napster emule bittorrent等早已是家喻戶曉了。效果可以看下具體51搜尋展示結合xunsearch全文檢索技術,可以達到毫秒級的資料搜尋...
Android中計算textView長度問題
1.textview的寬度由螢幕總寬度 額外寬度 得到。然後乘以2得到textview可以顯示的內容的寬度。2.使用工具方法textutils.ellipsize方法,將1中計算的長度傳入,得到textview中可以顯示的文字的內容。3.將2中得到的文字set到textview中。private v...
Numpy中計算各種距離
詳細 1.閔可夫斯基距離 minkowski distance 2.歐氏距離 euclidean distance 3.曼哈頓距離 manhattan distance 4.切比雪夫距離 chebyshev distance 5.夾角余弦 cosine 6.漢明距離 hamming distance...