algorithm - C# system.timers.timer weird behavior -
i trying implement bully coordinator election algorithm. in algorithm, coordinator sends alive message every 10 seconds , processes wait @ least 14 seconds receive alive, if don't receive message within time, initiate dead coordinator election.
the problem alivetimer (timer3_count) increasing exponentially , active processes affecting it. don't know why behaving weirdly.
when initial coordinator sending alive message counter works after dead coordinator election, behaves weirdly.
else if (received_text.contains("alive:")) { settext(received_text + "\n"); coordinator_alive = true; timer3_counter = 0; if (alive_count == 0) { alive_count++; alivetimer.interval = (1 * 1000); alivetimer.enabled = true; alivetimer.elapsed += new system.timers.elapsedeventhandler(alivetimer_elapsed); alivetimer.start(); } }
the elapsed function here think there wrong program, tried everything.
private void alivetimer_elapsed(object sender, eventargs e) { timer3_counter++; settimer(timer3_counter.tostring()); random rnd = new random(); int rand_time = rnd.next(14, 18); if (timer3_counter == 14) { alivetimer.stop(); timer3_counter = 0; alive_count = 0; if (coordinator_alive == false) { byte[] buffer = encoding.ascii.getbytes("dead coordinator election: " + txname.text); _clientsocket.send(buffer); timer4_counter = 0; deadtimer.interval = (1 * 1000); deadtimer.elapsed += new system.timers.elapsedeventhandler(deadtimer_elapsed); deadtimer.enabled = true; deadtimer.start(); } } if (coordinator_alive == true) coordinator_alive = false; }
and dead coordinator election code here
else if (received_text.contains("dead coordinator election:")) { setcpid(""); coordinator_alive = false; alive_count = 0; timer3_counter = 0; alivetimer.stop(); alivetimer.enabled = false; string output = regex.match(received_text, @"\d+").value; settext("dead coordinator election received process id: " + output + "\n"); if (convert.toint32(txname.text) > convert.toint32(output)) { byte[] buffer = encoding.ascii.getbytes("greater process no: " + txname.text + " found " + output + "\n"); _clientsocket.send(buffer); settext("our process no: " + txname.text + " greater " + output + "\n"); lower_count++; byte[] buffer1 = encoding.ascii.getbytes("dead coordinator election: " + txname.text); _clientsocket.send(buffer1); } else { byte[] txt_send = encoding.ascii.getbytes("our process no: " + txname.text + " less " + output); _clientsocket.send(txt_send); greater_count++; } }
the full code can found here bully algorithm
note: using passive server broadcast messages each process
i don't know causes problem, think you able figure cause if log start , stop all methods , analyse output.
this establish if: 1. @idle_mind suggested, adding more , more handlers 2. time taken execute each method creeps , more...
i don't know how app built can start console.writeline
or debug.writeline
.
Comments
Post a Comment