Erlang Dictionary fetch crash -
edit
i have 2 modules , both cause bad args errors when fetching dictionary(gen_server state)
here code 1 module
init([chunksize, runningcounter]) -> d0 = dict:new(), d1 = dict:store(chunksize, chunksize, d0), d2 = dict:store(torrentuploadspeed, 0, d1), d3 = dict:store(torrentdownloadspeed, 0, d2), torrentdownloadqueue = queue:new(), torrentuploadqueue = queue:new(), d4 = dict:store(torrentdownloadqueue, torrentdownloadqueue, d3), d5 = dict:store(torrentuploadqueue, torrentuploadqueue, d4), d6 = dict:store(runningcounter, runningcounter, d5), {ok, d6}. i set_peer_state sets peer dictionary(1 unique each peer) dictionary holds download , upload (queue , speed) , add main gen_server state(dictionary) have main torrent data in main dictionary dictionary each peer stored peer id.
set_peer_state(id) -> gen_server:cast(?server, {setpeerstate, id}). handle_cast({setpeerstate, id}, state) -> io:format("in set peer state ~p~n", [dict:fetch(runningcounter, state)]), id0 = dict:new(), peerdownloadqueue = queue:new(), peeruploadqueue = queue:new(), id1 = dict:store(peerdownloadqueue, peerdownloadqueue, id0), id2 = dict:store(peeruploadqueue, peeruploadqueue, id1), id3 = dict:store(peerdownloadspeed, 0, id2), id4 = dict:store(peeruploadspeed, 0, id3), d = dict:store(id, id4, state), {noreply, d}; this seems work far. when try updating torrent state crashes when fetching dictionary.
handle_cast({updatetorrentdownloadstate, time}, state) -> % fetch counter speed calculation , queue length runningcounter = dict:fetch(runningcounter, state), % fetch torrents download queue torrentdownloadqueue = dict:fetch(torrentdownloadqueue, state), io:format("the fetched queue ~p~n", [dict:fetch(torrentdownloadqueue, state)]), % add item queue (main torrent upload queue) torrentdownloadqueue2 = queue:in(time, torrentdownloadqueue), % lenght of downloadqueue torrentdownloadqueuelength = queue:len(torrentdownloadqueue2), % if queue larger running counter remove item if torrentdownloadqueuelength >= runningcounter -> % remove item queue torrentdownloadqueue3 = queue:drop(torrentdownloadqueue2), update_torrent_download(torrentdownloadqueue3, state); torrentdownloadqueuelength < runningcounter -> update_torrent_download(torrentdownloadqueue2, state) end; and here 2 internal functions
update_torrent_download(torrentdownloadqueue, state) -> % store queue new torrent dict state2 = dict:store(torrentdownloadqueue, torrentdownloadqueue, state), speed = calculate_speed(torrentdownloadqueue, state2), state3 = dict:store(torrentdownloadspeed, speed, state2), {noreply, state3}. calculate_speed(queue, state) -> list = queue:to_list(queue), sum = lists:sum(list), count = queue:len(queue), chunksize = dict:fetch(chunksize, state), speed = (count * chunksize) div sum, {ok, speed}. could passing incorrect data setters crash server? or state lost along way? way of doing seems messy new dicts store in old dict, there better way handle data structure(main torrent , data each peer)?
i know make dictionaries lists, messing mind @ point testing module.
thanks
your problem state not dict.
1> dict:fetch(runningcounter, not_a_dict). ** exception error: {badrecord,dict} in function dict:get_slot/2 in call dict:fetch/2
Comments
Post a Comment