miércoles, 28 de enero de 2009

Net simulator toy problem in Io

Another little test in Io language.

Packets are generated in a node with a content and a destination. Nodes are linked in a ring and passOn packages unless the package is for themselves.

Next versions to come, with subclassed nodes and more tests.

Syntax highlighted version in http://pastie.org/373095

Packet := Object clone do(
content := nil
destinatary := nil
)

Node := Object clone do (
id ::= nil
next ::= nil
generate := method(content, dest,
a := Packet clone
a content = content
a destinatary = dest
#a destinatary println
passOn(a)
)

receive := method(packet,
("I'm ".. self id .. ". The packet goes to".. packet destinatary .. "") println
if(packet destinatary == self id,
("I've received a package with the message:".. packet content .. "") println ,
#if (packet content asString containsSeq("sleep") and self id == 4, wait(2)) #nonblocking? anyone?
passOn(packet)))

passOn := method(packet,
#self id asString println
if(self next != nil, r:= self next receive(packet), "WTF?! I've no next node!" println))
)

lista := List clone
lista append (Node clone do (setId(0)))

for(i,100,1,-1,
lista append(Node clone do(setId(i) setNext(lista at(lista size -1)))))

lista at(0) next = lista at(lista size-1)
l1 := lista at(0) generate("matat1",45)
#l2 := lista at(0) generate("matatsleep",65)
#l3 := lista at(0) generate("matat3",20)
Future Work is Handling packages in a thread (and learning how to use futures), to be able to block some packages in certain nodes but be able to continue passing nodes along while processing (blocking) a given package.

That would allow having many packages navigating the 'net'.

No hay comentarios: