# Model for my problem (network simulator)

**URL:** <https://rubytalk.org/t/model-for-my-problem-network-simulator/11338>\
**Category:** ruby-talk\
**Created:** [3 May 2004 16:17 UTC](https://rubytalk.org/t/model-for-my-problem-network-simulator/11338 "2004-05-03T16:17:14Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gergely\_Kontra](https://avatars.discourse-cdn.com/v4/letter/g/7c8e57/32.png) [@Gergely\_Kontra](https://rubytalk.org/u/Gergely_Kontra)\
**Post date:** [3 May 2004 16:17 UTC](https://rubytalk.org/t/model-for-my-problem-network-simulator/11338/1 "2004-05-03T16:17:14Z")

</div>

Hello!

I’d like to ask for help for my problem. Maybe the Q isn’t  
ruby-specific, but I WILL implement it in ruby, and I want to make a  
rubyish program. I hope I can learn from the problem.

So: I had to write a network simulator. It consists of nodes, link  
between nodes. The delay of the link should be user-programmable (maybe  
proc is the right way, but it must be evaluated in the object’s scope,  
because delay is not independent of the link’s state…

And finally the simulator has agents. Agents are programs, which can  
migrate. Migration is requested by agent. (Request goes to the node, and  
the node does the actual migration…)  
Agent must know about their own location, it can ask the node about  
outgoing links (so nodes must know about links ending at the node, of  
course).

Shall I submit my code? Or did anybody has a model for it already?

thx in advance  
Gergo

> **···**
>
> –  
> ±[Kontra, Gergely[kgergely@mcl.hu](mailto:kgergely@mcl.hu) PhD student Room IB113 ]---------+
> 
> > [http://www.mcl.hu/~kgergely](http://www.mcl.hu/~kgergely) “Olyan langesz vagyok, hogy |  
> > Mobil:(+36 20) 356 9656 ICQ: 175564914 poroltoval kellene jarnom” |  
> > ±- Magyar php mirror es magyar php dokumentacio: [http://hu.php.net](http://hu.php.net) --+

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [3 May 2004 16:59 UTC](https://rubytalk.org/t/model-for-my-problem-network-simulator/11338/2 "2004-05-03T16:59:00Z")

</div>

“Gergely Kontra” [kgergely@mcl.hu](mailto:kgergely@mcl.hu) schrieb im Newsbeitrag  
news:20040503161710.GA16300@mlabdial.hit.bme.hu…

> Hello!
> 
> I’d like to ask for help for my problem. Maybe the Q isn’t  
> ruby-specific, but I WILL implement it in ruby, and I want to make a  
> rubyish program. I hope I can learn from the problem.
> 
> So: I had to write a network simulator. It consists of nodes, link  
> between nodes. The delay of the link should be user-programmable (maybe  
> proc is the right way, but it must be evaluated in the object’s scope,  
> because delay is not independent of the link’s state…
> 
> And finally the simulator has agents. Agents are programs, which can  
> migrate. Migration is requested by agent. (Request goes to the node, and  
> the node does the actual migration…)  
> Agent must know about their own location, it can ask the node about  
> outgoing links (so nodes must know about links ending at the node, of  
> course).
> 
> Shall I submit my code? Or did anybody has a model for it already?

What was your question exactly? You seem to have implemented the simulator  
already…

As a general OO rule of thumb, take all the interesting nouns and make them  
classes. This seems to be pretty much straightforward in your case.

The thing I find more interesting is the scheduling. This could be done  
like this (attached).

Regards

```
robert

```

[schedule.rb](https://rubytalk.org/uploads/short-url/5T8DWLnODvlqp1vrAp7TSOsnoms.rb) (1021 Bytes)

---

<div class="post-metadata">

**Author:** ![Gergely\_Kontra](https://avatars.discourse-cdn.com/v4/letter/g/7c8e57/32.png) [@Gergely\_Kontra](https://rubytalk.org/u/Gergely_Kontra)\
**Post date:** [4 May 2004 12:13 UTC](https://rubytalk.org/t/model-for-my-problem-network-simulator/11338/3 "2004-05-04T12:13:23Z")

</div>

Hey!  
I have almost the same code for scheduling. I just used something  
similar to this. The only difference: I used relative time, so my method  
is called after…  
Maybe it worth to make something generally usable?

BTW  
Hash.new {|h,k| h[k]= } can be written as  
Hash.new {}

> **···**
>
> On 0504, Robert Klemme wrote:
> 
> > The thing I find more interesting is the scheduling. This could be done  
> > like this (attached).  
> > –  
> > ±[Kontra, Gergely[kgergely@mcl.hu](mailto:kgergely@mcl.hu) PhD student Room IB113 ]---------+  
> > [http://www.mcl.hu/~kgergely](http://www.mcl.hu/~kgergely) “Olyan langesz vagyok, hogy |  
> > Mobil:(+36 20) 356 9656 ICQ: 175564914 poroltoval kellene jarnom” |  
> > ±- Magyar php mirror es magyar php dokumentacio: [http://hu.php.net](http://hu.php.net) --+

---

<div class="post-metadata">

**Author:** ![Gergely\_Kontra](https://avatars.discourse-cdn.com/v4/letter/g/7c8e57/32.png) [@Gergely\_Kontra](https://rubytalk.org/u/Gergely_Kontra)\
**Post date:** [6 May 2004 13:05 UTC](https://rubytalk.org/t/model-for-my-problem-network-simulator/11338/4 "2004-05-06T13:05:29Z")

</div>

Just one little thing (chicken-egg problem): the links should know about  
the 2 ending nodes, and nodes must know about in/outgoing links. So how  
to easily administrate it?

Gergo

> **···**
>
> On 0504, Robert Klemme wrote:
> 
> > > I’d like to ask for help for my problem. Maybe the Q isn’t  
> > > ruby-specific, but I WILL implement it in ruby, and I want to make a  
> > > rubyish program. I hope I can learn from the problem.
> > > 
> > > So: I had to write a network simulator. It consists of nodes, link  
> > > between nodes. The delay of the link should be user-programmable (maybe  
> > > proc is the right way, but it must be evaluated in the object’s scope,  
> > > because delay is not independent of the link’s state…
> > > 
> > > And finally the simulator has agents. Agents are programs, which can  
> > > migrate. Migration is requested by agent. (Request goes to the node, and  
> > > the node does the actual migration…)  
> > > Agent must know about their own location, it can ask the node about  
> > > outgoing links (so nodes must know about links ending at the node, of  
> > > course).
> > > 
> > > Shall I submit my code? Or did anybody has a model for it already?
> > 
> > What was your question exactly? You seem to have implemented the simulator  
> > already…
> 
> –  
> ±[Kontra, Gergely[kgergely@mcl.hu](mailto:kgergely@mcl.hu) PhD student Room IB113 ]---------+
> 
> > [http://www.mcl.hu/~kgergely](http://www.mcl.hu/~kgergely) “Olyan langesz vagyok, hogy |  
> > Mobil:(+36 20) 356 9656 ICQ: 175564914 poroltoval kellene jarnom” |  
> > ±- Magyar php mirror es magyar php dokumentacio: [http://hu.php.net](http://hu.php.net) --+

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [4 May 2004 12:49 UTC](https://rubytalk.org/t/model-for-my-problem-network-simulator/11338/5 "2004-05-04T12:49:02Z")

</div>

“Gergely Kontra” [kgergely@mcl.hu](mailto:kgergely@mcl.hu) schrieb im Newsbeitrag  
news:20040504121319.GA27180@mlabdial.hit.bme.hu…

> > The thing I find more interesting is the scheduling. This could be  
> > done  
> > like this (attached).  
> > Hey!  
> > I have almost the same code for scheduling. I just used something  
> > similar to this. The only difference: I used relative time, so my method  
> > is called after…

Do you convert to abs time internally or how do you make sure the order is  
correct?

> Maybe it worth to make something generally usable?

=\> RAA

> BTW  
> Hash.new {|h,k| h[k]= } can be written as  
> Hash.new {}

Nope, that’s not the same:

irb(main):001:0\> h=Hash.new {}  
=\> {}  
irb(main):002:0\> h[1]  
=\>   
irb(main):003:0\> h  
=\> {}  
irb(main):004:0\> h=Hash.new {|h,k| h[k]=}  
=\> {}  
irb(main):005:0\> h[1]  
=\>   
irb(main):006:0\> h  
=\> {1=\>}  
irb(main):007:0\> RUBY\_VERSION  
=\> “1.8.1”

Your solution makes the idiom “h[key] \<\< something” unusable since all  
those arrays never make it into the hash.

```
robert

```

> **···**
>
> > On 0504, Robert Klemme wrote:

---

<div class="post-metadata">

**Author:** ![Zsban\_Ambrus](https://avatars.discourse-cdn.com/v4/letter/z/e5b9ba/32.png) [@Zsban\_Ambrus](https://rubytalk.org/u/Zsban_Ambrus)\
**Post date:** [6 May 2004 13:26 UTC](https://rubytalk.org/t/model-for-my-problem-network-simulator/11338/6 "2004-05-06T13:26:54Z")

</div>

> > > I’d like to ask for help for my problem. Maybe the Q isn’t  
> > > ruby-specific, but I WILL implement it in ruby, and I want to make a  
> > > rubyish program. I hope I can learn from the problem.

“rubyish program” – does this mean a program in ruby? probably since  
you post this here

> > > So: I had to write a network simulator. It consists of nodes, link  
> > > between nodes.  
> > > …
> > > 
> > > Shall I submit my code? Or did anybody has a model for it already?
> > 
> > What was your question exactly? You seem to have implemented the simulator  
> > already…
> 
> Just one little thing (chicken-egg problem): the links should know about  
> the 2 ending nodes, and nodes must know about in/outgoing links. So how  
> to easily administrate it?

That’s easy in ruby, as ruby is garbage-collected, so it can clean up  
circular references by itself. Thus, you can add a list of both the in and  
outgoing links to each nodes.

> **···**
>
> On Thu, May 06, 2004 at 10:05:29PM +0900, Gergely Kontra wrote:
> 
> > On 0504, Robert Klemme wrote:

---

<div class="post-metadata">

**Author:** ![Gergely\_Kontra](https://avatars.discourse-cdn.com/v4/letter/g/7c8e57/32.png) [@Gergely\_Kontra](https://rubytalk.org/u/Gergely_Kontra)\
**Post date:** [4 May 2004 12:55 UTC](https://rubytalk.org/t/model-for-my-problem-network-simulator/11338/7 "2004-05-04T12:55:36Z")

</div>

Oh, yes, I run into the problem, and this is the weird case, where  
h[key] \<\< something and h[key] \<\<= something is different…

Gergo

> **···**
>
> On 0504, Robert Klemme wrote:
> 
> > Nope, that’s not the same:  
> > irb(main):001:0\> h=Hash.new {}  
> > irb(main):002:0\> h[1]  
> > irb(main):003:0\> h  
> > =\> {}  
> > irb(main):004:0\> h=Hash.new {|h,k| h[k]=}  
> > irb(main):005:0\> h[1]  
> > irb(main):006:0\> h  
> > =\> {1=\>}  
> > Your solution makes the idiom “h[key] \<\< something” unusable since all  
> > those arrays never make it into the hash.
> 
> –  
> ±[Kontra, Gergely[kgergely@mcl.hu](mailto:kgergely@mcl.hu) PhD student Room IB113 ]---------+
> 
> > [http://www.mcl.hu/~kgergely](http://www.mcl.hu/~kgergely) “Olyan langesz vagyok, hogy |  
> > Mobil:(+36 20) 356 9656 ICQ: 175564914 poroltoval kellene jarnom” |  
> > ±- Magyar php mirror es magyar php dokumentacio: [http://hu.php.net](http://hu.php.net) --+

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [6 May 2004 15:18 UTC](https://rubytalk.org/t/model-for-my-problem-network-simulator/11338/8 "2004-05-06T15:18:58Z")

</div>

“Zsban Ambrus” [ambrus@math.bme.hu](mailto:ambrus@math.bme.hu) schrieb im Newsbeitrag  
news:20040506132651.GB3743@math.bme.hu…

> > > > I’d like to ask for help for my problem. Maybe the Q isn’t  
> > > > ruby-specific, but I WILL implement it in ruby, and I want to make  
> > > > a  
> > > > rubyish program. I hope I can learn from the problem.
> 
> “rubyish program” – does this mean a program in ruby? probably since  
> you post this here
> 
> > > > So: I had to write a network simulator. It consists of nodes, link  
> > > > between nodes.  
> > > > …
> > > > 
> > > > Shall I submit my code? Or did anybody has a model for it already?
> > > 
> > > What was your question exactly? You seem to have implemented the  
> > > simulator  
> > > already…
> > 
> > Just one little thing (chicken-egg problem): the links should know  
> > about  
> > the 2 ending nodes, and nodes must know about in/outgoing links. So  
> > how  
> > to easily administrate it?
> 
> That’s easy in ruby, as ruby is garbage-collected, so it can clean up  
> circular references by itself. Thus, you can add a list of both the in  
> and  
> outgoing links to each nodes.

IMHO the question was rather how to manage those references so that they  
are always in sync. That would be the typical problem with bi directional  
references in OO languages. Typically you manually have to write code for  
that (unless you find a library that does it gracefully) but if done  
properly you need to do it only once and can then reuse that code.

```
robert

```

> **···**
>
> > On Thu, May 06, 2004 at 10:05:29PM +0900, Gergely Kontra wrote:
> > 
> > > On 0504, Robert Klemme wrote:

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [4 May 2004 16:48 UTC](https://rubytalk.org/t/model-for-my-problem-network-simulator/11338/9 "2004-05-04T16:48:56Z")

</div>

“Gergely Kontra” [kgergely@mcl.hu](mailto:kgergely@mcl.hu) schrieb im Newsbeitrag  
news:20040504125533.GB27180@mlabdial.hit.bme.hu…

> **···**
>
> > On 0504, Robert Klemme wrote:
> > 
> > > Nope, that’s not the same:  
> > > irb(main):001:0\> h=Hash.new {}  
> > > irb(main):002:0\> h[1]  
> > > irb(main):003:0\> h  
> > > =\> {}  
> > > irb(main):004:0\> h=Hash.new {|h,k| h[k]=}  
> > > irb(main):005:0\> h[1]  
> > > irb(main):006:0\> h  
> > > =\> {1=\>}  
> > > Your solution makes the idiom “h[key] \<\< something” unusable since all  
> > > those arrays never make it into the hash.
> > 
> > Oh, yes, I run into the problem, and this is the weird case, where  
> > h[key] \<\< something and h[key] \<\<= something is different…
> 
> Note also that h[key] \<\< something is more efficient since it does only one  
> hash lookup and no hash update.
> 
> Regards
> 
> ```
> robert
> 
> ```

---

<div class="post-metadata">

**Author:** ![Claus\_Spitzer](https://avatars.discourse-cdn.com/v4/letter/c/49beb7/32.png) [@Claus\_Spitzer](https://rubytalk.org/u/Claus_Spitzer)\
**Post date:** [6 May 2004 16:43 UTC](https://rubytalk.org/t/model-for-my-problem-network-simulator/11338/10 "2004-05-06T16:43:15Z")

</div>

8\< snip

> > > Just one little thing (chicken-egg problem): the links should know  
> > > about  
> > > the 2 ending nodes, and nodes must know about in/outgoing links. So  
> > > how  
> > > to easily administrate it?
> > 
> > That’s easy in ruby, as ruby is garbage-collected, so it can clean up  
> > circular references by itself. Thus, you can add a list of both the in  
> > and  
> > outgoing links to each nodes.
> 
> IMHO the question was rather how to manage those references so that they  
> are always in sync. That would be the typical problem with bi directional  
> references in OO languages. Typically you manually have to write code for  
> that (unless you find a library that does it gracefully) but if done  
> properly you need to do it only once and can then reuse that code.
> 
> ```
> robert
> 
> ```

Probably (and that is _only_ probably, as in “this is how I have seen  
it implemented in other instances by very smart professors”) you’ll  
want do do something along the lines of:

- both the nodes and links have a list of what is attached to them.
- both are observers and observables
- a change in the node notifies the link, and vice-versa

Otherwise, if for example only the links initiates the change, then  
you can make them be the observables and the nodes just the observers.  
Cheers!  
-CWS
