2727# Classes
2828##########################################################################
2929
30+
3031class Node (Machine ):
3132
3233 def __init__ (self , env , * args , ** kwargs ):
@@ -45,44 +46,55 @@ def create(cls, env, *args, **kwargs):
4546 while True :
4647 yield cls (env , * args , ** kwargs )
4748
48- def send (self , address , size , value = None ):
49+ def send (self , address , port , size , value = None ):
4950 """
50- Puts a message onto the containing Rack.
51+ Puts a message onto the parent Rack.
5152 """
52- pass
53+ self . rack . send ( address = address , port = port , size = size , value = value )
5354
54- def recv (self ):
55+ def recv (self , port , size , value = None ):
5556 """
56- Obtains a message from the containing Rack.
57+ Obtains a message from the parent Rack.
5758 """
58- pass
59+ program = None
60+ for p in self .programs .itervalues ():
61+ if port in p .ports :
62+ program = p
63+
64+ if program :
65+ program .recv (value )
5966
6067 def assign (self , program ):
6168 """
6269 Ingests a new Program for processing. If there aren't enough resources
6370 available then raises `NodeLacksCapacity`.
6471 """
65- # TODO: ensure we have capacity
72+ if self .idle_cpus < program .cpus :
73+ raise NodeLacksCapacity ('{} cpus requested but only {} are free.'
74+ .format (program .cpus , self .idle_cpus ))
75+
76+ if self .idle_memory < program .memory :
77+ raise NodeLacksCapacity ('{}GB requested but only {}GB are free.'
78+ .format (program .memory , self .idle_memory ))
6679
6780 self .programs [program .id ] = program
6881 program .node = self
69- program .run ()
7082
7183 def run (self ):
7284 """
7385 Method to kickoff process simulation.
7486 """
75- # TODO: replace with actual code or returned Program process
76- while True :
77- yield self .env .timeout (1 )
78- # print "Node {} checking in at {}".format(self.id, self.env.now)
87+ yield self .env .timeout (1 )
7988
8089 @property
8190 def address (self ):
8291 """
8392 Addressable identifier for this node containing the Rack and Node ID.
8493 """
85- pass
94+ return "{}:{}" .format (
95+ self .rack .id ,
96+ self .id
97+ )
8698
8799 @property
88100 def id (self ):
@@ -121,8 +133,6 @@ def __repr__(self):
121133 return "<{}>" .format (self .__str__ ())
122134
123135
124-
125-
126136##########################################################################
127137# Execution
128138##########################################################################
0 commit comments