2008年8月29日 星期五

NS-2 - UDP Agent

############################################################
# set option value

# 設定參數
############################################################

#利用陣利設定參數
set opt(trace) out.nam                                        ;# 設定檔案參數
set opt(nodes) 10                                             ;# 節點個數
set opt(rtproto) DV                                           ;# 路由協定
set opt(src.packet.size) 800                                  ;# 來源端封包大小
set opt(src.packet.int) 0.5                                   ;# 來源端封包間隔
set opt(udp.MMS) 100                                          ;# Max Sefment Size for UDP afent
set opt(udp.dest.addr) 1                                      ;# 還不知道這個怎麼用?
set opt(udp.dest.port) 80
set opt(udp.ttl) 6                                            ;# Time-To-Live

############################################################
# main program
############################################################
set ns [new Simulator]                                        ;# 在檔案一開始會先建立一個的模擬環境
$ns rtproto $opt(rtproto)                                     ;# 設定路由協定

set nf [open $opt(trace) w]                                   ;# 開啟一個可寫入的檔案out.nam
$ns namtrace-all $nf                                          ;# 將所有模擬的資料都寫入$nf這個檔案中
$ns use-newtrace                                              ;# Use new trace format 使用新的trace format

proc finish {} {                                              ;# 新增一個procedure "finish"
  global ns nf opt                                            ;# 讓這個proc能讀取這個scrpit的其他變數,其中opt是陣列,只要global這一個opt,陣列其它元素就可以使用
  $ns flush-trace                                             ;# 將所有的trace object 寫入 buffer
  close $nf                                                   ;# 關掉out.nam檔
  exec nam $opt(trace) &                                      ;# 透過背景執行nam來開起out.nam檔
  exit 0                                                      ;# 離開
}

for {set i 0} {$i < $opt(nodes)} {incr i} {
  set n($i) [$ns node]                                        ;# 建立節點
}

for {set i 0} {$i < $opt(nodes)} {incr i} {                   ;# 建立一個環狀的topology
$ns duplex-link $n($i) $n([expr ($i+1)%$opt(nodes)]) 1Mb 10ms DropTail
}

set udp0 [new Agent/UDP]                                      ;# 新增一個udp,第一個node裝UDP Agent
$udp0 set packetSize_ $opt(udp.MMS)                           ;# Max Segment Size for UDP agent
$udp0 set dst_addr_ $opt(udp.dest.addr)                       ;# 目的地IP
$udp0 set dst_port_ $opt(udp.dest.port)                       ;# 目的地Port number
$udp0 set ttl_ $opt(udp.ttl)                                  ;# Time-To-Live

$ns attach-agent $n(0) $udp0                                  ;# 在$n(0)上裝上$udp0

set cbr0 [new Application/Traffic/CBR]                        ;# 先建立一個cbr,然後在udp上裝CBR
$cbr0 set packetSize_ $opt(src.packet.size)                   ;# 設定CBR封包大小
$cbr0 set interval_ $opt(src.packet.int)                      ;# 設定CBR發射封包的間隔時間
$cbr0 attach-agent $udp0                                      ;# 把cbr0架在udp0上,CBR應用程式是在UDP協定上運作的

set null0 [new Agent/Null]                                    ;# 先建立一個NULL特性,在第3個node上裝Null Agent,NULL Agent是用來drop packet的
$ns attach-agent $n(2) $null0                                 ;# 把null0建立在n(2)上

$ns connect $udp0 $null0                                      ;# 將來源端和目的端連結起來

$ns at 0.5 "$cbr0 start"                                      ;# 在第0.5秒開始送封包
$ns rtmodel-at 1.5 down $n(1)                                 ;# 在第1.5秒$n(1)停掉所有的interface
$ns rtmodel-at 3.5 up $n(0) $n(1)                             ;# 在第2.5秒,將$n(0)和$n(1)的介面up
$ns rtmodel-at 5.5 up $n(1) $n(2)                             ;# 在第2.5秒,將$n(1)和$n(2)的介面up
$ns at 9.5 "finish"                                           ;# 在第9.5秒,結束模擬

$ns run                                                       ;# 開始執行模擬

透過Windows Live Writer來寫blog

最近常用blog記錄我的學習進度~ 直接在網頁寫程式有時候會很麻煩~像是<要用&lt;取代, 因此,是有一點麻煩啦~

在網路上找到原來Windows Live Writer可以支援blogspot哩~

因為,在網頁使用半行的空白會被忽略~在Html模式要用&nbsp;一直取代~
真的很麻煩~不過,我覺得就是有經驗過之前用Html去編輯過~現在我對Html比較了解哩~

之後,應該常用Windows Live Writer來寫blog吧~

Windows Live Writer 下載 :
http://get.live.com/writer/overview

系統需求:
需安裝 .NET Framework 2.0 套件。

參考資料:

Windows Live Writer 超強部落格寫作軟體 讓您想寫就寫
如何用Windows Live Writer修改更早之前的舊文?

ns2 - Link的基本指令



透過ns-2來模擬上面的網路情況
simple.tcl

set ns [new Simulator]
;# 在檔案一開始會先建立一個的模擬
;# step1:先建立一個Simulator物件
;# step2:把ns變數設定為物件:set ns 物件

$ns color 1 Blue
;# 定義color index 1=> Blue
$ns color 2 Red
;# 定義color index 2=> Red

set nf [open out.nam w]
;# 開啟一個可寫入的檔案out.nam
;# step1:先執行open out.nam w,並傳回這一個物件
;# step2:設定nf為記錄這一個物件

$ns namtrace-all $nf
;# 將所有模擬的資料都寫入$nf這個檔案中

;# 新增一個procedure "finish"
proc finish {} {
  global ns nf
  ;# 宣告ns與nf為在外面定義過的變數
  $ns flush-trace
  ;# 把trace的結果寫入檔案中
  close $nf
  ;# 關掉檔案
  exec nam out.nam &
  ;# 在背景中,透過nam去執行out.nam
  exit 0
  ;# 離開
}

#建立4個Node
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
;# 設定n0為一個node
;# step1:$ns node傳回一個node物件
;# step2:設定n0為這一個傳回的物件

#建立三個link
$ns duplex-link $n0 $n2 1Mb 10ms DropTail
#; node 0連到node 2 頻寬為1Mb,delay time為10ms 為DropTail
$ns duplex-link $n1 $n2 1Mb 10ms DropTail
#; node 1連到node 2 頻寬為1Mb,delay time為10ms 為DropTail
$ns duplex-link $n3 $n2 1Mb 10ms SFQ
#; node 2連到node 3 頻寬為1Mb,delay time為10ms 為SFQ

#設定link的屬性
$ns duplex-link-op $n0 $n2 orient right-down
#; 把它想成一點,往右下延伸
$ns duplex-link-op $n1 $n2 orient right-up
#; 把它想成一點,往右上延伸
$ns duplex-link-op $n2 $n3 orient right
#; 把它想成一點,往右延伸

#Monitor the queue for the link between node 2 and node 3
$ns duplex-link-op $n2 $n3 queuePos 0.5
;# 觀測n2到n3之間queue的變化,這是要給NAM用的
;# 也就是說只查看安置由n2到n3的封包Queue的情況

set udp0 [new Agent/UDP]
;# 新增一個UDP agent
$udp0 set class_ 1
;# 將$upd1顏色設成1 (Blue)
$ns attach-agent $n0 $udp0
;# $n0架構在$udp0上

set cbr0 [new Application/Traffic/CBR]
;# 建立應用層的流量描述
$cbr0 set packetSize_ 500
;# 設定封包大小
$cbr0 set interval_ 0.005
;# 設定封包間隔時間
$cbr0 attach-agent $udp0
;# 依附在UDP agent上

set udp1 [new Agent/UDP]
$udp1 set class_ 2
;# 將$upd1顏色設成2 (Red)
$ns attach-agent $n1 $udp1

set cbr1 [new Application/Traffic/CBR]
$cbr1 set packetSize_ 500
$cbr1 set interval_ 0.005
$cbr1 attach-agent $udp1

set null0 [new Agent/Null]
;# 建立一個Null Agent
$ns attach-agent $n3 $null0
;# 將$n3架構在$null0

$ns connect $udp0 $null0
;# 把agent $udp0和agent $null0連在一起
$ns connect $udp1 $null0
;# 把agent $udp1和agent $null0連在一起

$ns at 0.5 "$cbr0 start"
$ns at 1.0 "$cbr1 start"
$ns at 4.0 "$cbr1 stop"
$ns at 4.5 "$cbr0 stop"
$ns at 5.0 "finish"

$ns run


注意在attach-agent的時候,有時候會不確定哪一個在前面,哪一個在後面~
反正,目前就這樣記,udp、tcp的份量比較大,放在後面

2008年8月28日 星期四

好用的免費軟體

Gimp[portable][Linux、Windows]
Gimp為媲美Photoshop的影像處理軟體
不過,我覺得那一些功能都太高級了~目前我使用不到~簡單的圖示,可以用OpenOffice.org Drawing喔~

OpenOffice.org[portable][Linux、Windows、Mac]:
OpenOffice.org(簡稱OOo或OO.o)是一套開放原始碼的辦公室軟體、包含Writer、Calc、Impress、Math、Draw、Base

Google 軟體集

Google瀏覽器



參考資料:
自由軟體學習地圖
Portable軟體
在 Windows 環境下使用自由軟體

2008年8月27日 星期三

ns2 - 指令格式

建立一個simple-link

$ns simplex-link <node1> <node2> <bandwidth> <delay> <queue type> <args>


建立一個duplex-link

$ns duplex-link <node1> <node2> <bandwidth> <delay> <queue type> <args>

<queue type> := DropTail | RED | CBQ | FQ | SFQ | DRR

設定simple-link的屬性

$ns simplex-link-op <n1> <n2> <op> <args>


設定duplex-link的屬性

$ns duplex-link-op <n1> <n2> <op> <args>

<op> := orient | color | queuePos | label

為nam定義color index, 後面可以這個index來指名顏色。

$ns color <color-id> <color>


[UDP Agent]

Maximum Segment Size (MSS)

$udp set packetSize_ <pksize>


目的地IP

$udp set dst_addr_ <address>


目的地Port number

$udp set dst_port_ <portnum>


Time-To-Live

$udp set ttl_ <time-to-live>


[TCP Agent]

TCP Agent被實作在tcp.{cc, h}中,主要有兩種TCP agent:one-way agent和 two-way agent。One-way就是單純只扮演Sender或是 Receiver,Two-way同時是Sender也是Receiver。

[One-Way Sender]

Agent/TCP:a "tahoe" TCP sender

Agent/TCP/Reno: a "Reno" TCP sender

Agent/TCP/Newreno:a "Reno" TCP sender with a modification

Agent/TCP/Sack1:TCP with selective repeat (follows RFC2018)

Agent/TCP/Vegas:TCP Vegas

Agent/TCP/Fack:Reno TCP with "forward acknowledgement"

One-Way Receiver

Agent/TCPSink:TCP sink with one ACK per Packet

Agent/TCPSink/DelAck:TCP sink with configurable delay per ACK

Agent/TCPSink/Sack1:selective ACK sink (follows RFC2018)

Agent/TCPSink/Sack1/DelAck:Sack1 with DelAck

Two-way只有支援Reno:Agent/TCP/FullTcp

[Wireless]

wireless model主要是由MobileNode所組成,MobileNode由Node這個類別繼承而來,因此MobileNode除了有基本的Node能力,還有移動和無線傳送接收的能力。目前ad-hoc routing protocol有支援四種Destination Sequence Distance Vector (DSDV)、 Dynamic Source Routing (DSR) 、 Temporally Ordered Routing Algorithm (TORA)以及 Ad-hoc On-demand Distance Vector (AODV)。

在建立Mobile Node之前,我們必須定義Mobile Node所活動的topology大小。

set topo [ new Topography ]
$topo load_flatgrid $opt(x) $opt(y)


接著我們要建立一個God instance,這是用來建立一個矩陣,用以儲存topology的連線資訊。

create-god $opt(nn)


舊的API建立Mobile Node根據使用的routing protocol,而新的 API則是藉由$ns node-config這個instproc來設定,接著就使用產生一般的node的方式產生Mobile Node。

$ns_ node-config -adhocRouting $opt(adhocRouting)
                 -llType $opt(ll)
                 -macType $opt(mac)
                 -ifqType $opt(ifq)
                 -ifqLen $opt(ifqlen)
                 -antType $opt(ant)
                 -propInstance [new $opt(prop)]
                 -phyType $opt(netif)
                 -channel [new $opt(chan)]
                 -topoInstance $topo
                 -wiredRouting OFF
                 -agentTrace ON
                 -routerTrace OFF
                 -macTrace OFF

for { set i 0 } { $i < $opt(nn) } { incr i } {
set node($i) [ $ns node ]
$node($i) random-motion 0 ;# disable random motion
}


雖然Mobile Node被設計成可以在三維空間中移動(X,Y,Z),但是Z尚未被使用,因此我們假設只在平面上移動,也就是Z永遠等於0。我們藉由設定X,Y,Z來初始Mobile Node的初始位置,並且使用setdest來移動Mobile Node,也可以使用$node start來做random的移動,不過不建議使用此方式,如果要做random的scenario可以使用CMU提供的tool -- setdest (放在~ns/indep-utils/cmu-scen-gen/中)。

$node set X_ <x1>
$node set Y_ <x2>
$node set Z_ 0
$ns at <time> $node setdest <x2> <y2> <speed> ;# 在時間time秒, 以速度speed往(x2,y2)移動

Tcl 語法

一開始可以先參考這裡:TCL 簡介
書簽:
[變數(variable)和變數替換(variable substituion)]、[表示式(expressions)]、[指令替換(command substitution)]、[流程控制(control flow)]、[程序(procedures)]、[陣列(arrays)]、[字串(strings)]、[輸出(output)]

key words:$、set、puts、expr、ns、if、elseif、else、incr、foreach、while、switch、proc、global

因為ns2的劇本是由tcl語法寫成的~所以,我們要先了解tcl語法如何使用~

因為沒有時間,
下面的網頁就寫的很清楚了,所以,大部分還是去參考下面的網頁吧~若以後,我有空再補吧~
我只寫出我遇到的問題~

;#
上面兩個聯合符號是表示後面的東西就是註解

[變數(variable)和變數替換(variable substituion)]

[表示式(expressions)]

[指令替換(command substitution)]

[流程控制(control flow)]
寫完範例後,竟然出現:「extra characters after close-brace while executing...」
因為在tcl語法中:
if {$value > 10}{
要改成: if {$value > 10} {
也就是if {}空一格{}
也就是說:
{左邊一定要存在一個空格
}右邊一定要存在一個空格

而且不能用

if {...}
{
  // do something
}

必需要用

if {...} {
  // do something
}


[程序(procedures)]:應該就是副程式的意思啦~
宣告:
proc name {params1 params2 } {
  body
}
其中name為程序的名稱,params是參數列表,body則是程序的主體
例:

proc sum_proc {a b} {
  global myglobalvar
  puts "The value of the global variable is $myglobalvar"
  return [expr $a + $b]
}


呼叫:
name param1 param2
例:

set myglobalvar 79
set sum [sum_proc $num1 $num2]

說明:
這裡的中括號表示是先運算中括號裡面的值,中括號裡面運算出來是2,則再執行set sum 2這一行指令

這個範例主要在說明區域變數(local variable)和全域變數(global variable)的觀念。這樣要特別說明的是當程序中有需要用到程序外已經定義好的變數前,必需先使用global這個保留字做宣告,才能存取程序外已經定義好的變數。

[陣列(arrays)]


set myarray(0) "Zero"
set myarray(1) "One"
set myarray(2) "Two"
for {set i 0} {$i < 3} {incr i 1} {
  puts $myarray($i)
}

這裡的陣列是用小括號來包住引數

ex6_2.tcl

set person_info(name) "Fred Smith"
set person_info(age) "25"
set person_info(occupation) "Plumber"
foreach thing {name age occupation} {
  puts "$thing == $person_info($thing)"
}

foreach thing{name age occupation}就是把它想成
set thing name;
set thing age;
set thing occupation;
分三個階段設定

ex6_3.tcl

set person_info(name) "Fred Smith"
set person_info(age) "25"
set person_info(occupation) "Plumber"
foreach thing [array names person_info] {
  puts "$thing == $person_info($thing)"
}

第一個loop時,$thing為name;而後面的就變成$person_info(name)也就是"Fred Smith"
第二個loop時,$thing為age;而後面的就變成$person_info(age)也就是"25"
第三個loop時,$thing為occupation;而後面的就變成$person_info(occupation)也就是"Plumber"

這個範例跟6.2最大的不同是在foreach這個迴圈,在6.2中,陣列中每個元素存放的位置必須一一列出,才能秀出陣列中每個元素的值,但是在這個範例,使用了[array names 矩陣名稱](注意是中括號喔~),使用的這樣的方法就可以列出person_info中的name、age、occupation,這個在元素很多的時候特別好用。而這裡的thing代表陣列的引數。

[字串特殊函數(strings function)]
ex7_1.tcl

set str "This is a string"
puts "The string is: $str"
puts "The length of the string is: [string length $str]"
puts "The character at index 3 is: [string index $str 3]"
puts "The characters from index 4 through 8 are: [string range $str 4 8]"
puts "The index of the first occurrence of letter \"i\" is: [string first i $str]"

說明:
[string length $str]可以用來顯示字串長度
[string index $str 3]可以用來顯示字串中的第四個字母為何(第一個字母的index為0)
[string range $str 4 8]可以用顯示從字串中第五個到第九個的字母
[string first i $str]可以用來顯示字母i在字串中第一次出現時的index值,也就是第一次出現時在第(index+1)個位置。

[輸出(output)]
ex8_1.tcl

set f [open "/tmp/myfile" "w"]
puts $f "We live in Texas. It's already 110 degrees out here."
puts $f "456"
close $f


執行的結果:
請使用文字編輯器打開/tmp/myfile,其內容為:

We live in Texas. It's already 110 degrees out here.
456

說明:
一般來說,puts若是沒有指定輸出裝置的話,內定的輸出裝置是螢幕,但此範例中有指定輸出的裝置為檔案,所以會把要秀出的字串寫入檔案内。

參考網頁:
TCL 簡介

Firefox - 套件

首先,firefox會那麼受歡迎,http://www.blogger.com/img/blank.gif
其中一個很重要的原因就是它額外的套件很多哩~
可以自動把下一個頁面先載入,並且把資料接在目前這一個頁面中,省去找下一頁的按紐與等待讀取下一頁的時間,非常之好用!尤其是在看網路漫畫的時候,更是好用!

Video DownloadHelper
可以下載flash的檔案~非常方便,特別是可以下載youtube的一堆影片~

Tab Mix Plus
可以設定分頁的功能,在配置上很好用~

若不知道有什麼套件好用,就先參考別人推薦的吧~

firefox add-one top 15

另外,就是firefox中文的套件網站

另外,我寫一下我目前的使用的套件

SuperDrapAndGo
可以直接拉連結來開新分頁,而不用在按右鍵,再按左鍵,選開分頁,很好用啦~

Download Statusbar
再下載檔案時,不會再跑出另一個視窗了~進度會在下面的顯示列上

FlashGot
可以整合一堆下載軟體,不錯~

IE Tab
可以在firefox的視窗中用IE核心瀏覽網頁

IE View
可以選擇聯結由IE視窗開啟,與IE Tab的差別在於,這是真正的IE視窗

Foxmarks Bookmark Synchronizer
可以同步多台電腦firefox上的書籤

Show Image
不用等整張圖片下載完才顯示~

新同文堂
可以翻譯繁體字與簡體字

Adblock Plus
可以拿來擋廣告,非常好用~

另外,也可以參考網友整理出來的東西
但是,我還沒有時間去弄

還有anki大大的推薦
軟件: Firefox 2.0.0.9 (2007102514)
操作系統: WINNT (x86-msvc)

軟件: Firefox 2.0.0.9 (2007102514)
操作系統: WINNT (x86-msvc)



【Firefox】Add-ons list @ Ubuntu 8.04