Commit 1406a81e authored by Saswat's avatar Saswat

fix broadcast and latency

parent 2e11cbbd
...@@ -7,7 +7,7 @@ class Network: ...@@ -7,7 +7,7 @@ class Network:
def __init__(self, sim, args): def __init__(self, sim, args):
self.args = args self.args = args
self.sim = sim self.sim = sim
self.pij = random.randrange(10, 500) # speed of light propagation delay in ms self.pij = float(random.randrange(10, 500)) * 0.001# speed of light propagation delay in ms
self.create_nodes() self.create_nodes()
self.create_connections() self.create_connections()
self.sim.push_event(Event(0, self.init_simulation,)) self.sim.push_event(Event(0, self.init_simulation,))
......
...@@ -5,11 +5,14 @@ from utils import * ...@@ -5,11 +5,14 @@ from utils import *
#TODO(SM): Need to bind each transaction with a time of arrival at a node #TODO(SM): Need to bind each transaction with a time of arrival at a node
txn_id = 0 txn_id = 0
def get_txn_id(): def gen_txn_id():
global txn_id global txn_id
txn_id += 1 txn_id += 1
return txn_id return txn_id
def get_txn_id(txn):
return txn.split(":")[0]
class Node: class Node:
def __init__(self, sim, ID, bandwidth_type, cpu_type, Tk, T_tx, num_nodes, pij): def __init__(self, sim, ID, bandwidth_type, cpu_type, Tk, T_tx, num_nodes, pij):
# Initialize node attributes # Initialize node attributes
...@@ -49,8 +52,9 @@ class Node: ...@@ -49,8 +52,9 @@ class Node:
cij = 100 cij = 100
else: else:
cij = 5 cij = 5
cij *= 1e6
dij = sample_exponential(96/cij) dij = sample_exponential((96*1e3)/cij)
latency = dij + self.pij latency = dij + self.pij
if(type(data) == str): # transaction if(type(data) == str): # transaction
...@@ -65,19 +69,26 @@ class Node: ...@@ -65,19 +69,26 @@ class Node:
""" """
Event to receive a transaction from a neighbor Event to receive a transaction from a neighbor
""" """
print(self.nodeId, "Received transaction", txn) txn = txn[0]
print(self.sim.curr_time, self.nodeId, "Received transaction", get_txn_id(txn))
if txn not in self.pending_transactions: if txn not in self.pending_transactions:
self.pending_transactions.append(txn) self.pending_transactions.append(txn)
# Broadcast the received transaction to the neighbors immediately
self.broadcast_transaction(txn)
else:
print(self.sim.curr_time, self.nodeId, "Already exist, discarding ", get_txn_id(txn))
return return
def _event_broadcast_transaction(self, txn): def broadcast_transaction(self, txn):
""" """
Broadcast the transaction to the neighbors Broadcast the transaction to the neighbors
""" """
print(self.nodeId, "Broadcasting txn", txn) print(self.sim.curr_time, self.nodeId, "Broadcasting txn", get_txn_id(txn))
for neighbor in self.neighbors: for neighbor in self.neighbors:
print(self.nodeId, "Sending txn", txn, "to", neighbor.nodeId) print(self.sim.curr_time, self.nodeId, "Sending txn", get_txn_id(txn),\
"to", neighbor.nodeId, self.sim.curr_time\
+ self.get_latency(neighbor, txn))
self.sim.push_event(Event(self.sim.curr_time + self.get_latency(neighbor, txn),\ self.sim.push_event(Event(self.sim.curr_time + self.get_latency(neighbor, txn),\
neighbor._event_receive_transaction, txn)) neighbor._event_receive_transaction, txn))
return return
...@@ -95,9 +106,9 @@ class Node: ...@@ -95,9 +106,9 @@ class Node:
amount = random.randint(1, self.balance + 10) amount = random.randint(1, self.balance + 10)
# Create a transaction and it to current nodes pending transactions # Create a transaction and it to current nodes pending transactions
txn = str(get_txn_id()) + ": " + str(self.nodeId) + " pays "\ txn = str(gen_txn_id()) + ": " + str(self.nodeId) + " pays "\
+ str(dest) + " " + str(amount) + " coins" + str(dest) + " " + str(amount) + " coins"
print(self.nodeId, "Generated a txn:", txn) print(self.sim.curr_time, self.nodeId, "Generated a txn:", txn)
self.pending_transactions.append(txn) self.pending_transactions.append(txn)
...@@ -106,5 +117,5 @@ class Node: ...@@ -106,5 +117,5 @@ class Node:
self._event_generate_transaction)) self._event_generate_transaction))
# broadcast the transaction to the neighbors # broadcast the transaction to the neighbors
self._event_broadcast_transaction(txn) self.broadcast_transaction(txn)
pass pass
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment