simple_source模塊可以在外部設置的屬性
有四個局部統計量,分別爲產生的bit速率、包速率、包大小,包間隔
狀態機爲三個非強制對象,在頭文件裏定義了自中斷和轉移條件。
/*Include files. */
#include <oms_dist_support.h>
/*Special attribute values. */
#define SSC_INFINITE_TIME -1.0
/*Interrupt code values. */ 自定義中斷
#define SSC_START 0
#define SSC_GENERATE 1
#define SSC_STOP 2
/* Nodeconfiguration constants. */
#define SSC_STRM_TO_LOW 0
/* Macrodefinitions for state */
/*transitions. */定義轉移條件
#define START (intrpt_code == SSC_START)
#define DISABLED (intrpt_code == SSC_STOP)
#define STOP (intrpt_code == SSC_STOP)
#define PACKET_GENERATE (intrpt_code == SSC_GENERATE)
/*Function prototypes. */
staticvoid ss_packet_generate(void);
Init非強制狀態的入口程序爲
/* Atthis initial state, we read the values of source attributes */包含源的屬性定義
/* andschedule a selt interrupt that will indicate our start time */表明開始的時間
/* forpacket generation. */
/* Obtainthe object id of the surrounding module. */
own_id = op_id_self ();獲得所屬處理器或隊列的對象ID
/* Readthe values of the packet generation parameters, i.e. the */
/*attribute values of the surrounding module. */獲取給定對象的某屬性
op_ima_obj_attr_get (own_id,"Packet Interarrival Time", interarrival_str);包間隔
op_ima_obj_attr_get(own_id, "Packet Size", size_str); 包大小
op_ima_obj_attr_get(own_id, "Packet Format", format_str);包格式
op_ima_obj_attr_get(own_id, "Start Time", &start_time);開始時間
op_ima_obj_attr_get(own_id, "Stop Time", &stop_time);停止時間
/* Loadthe PDFs that will be used in computing the packet */
/*interarrival times and packet sizes. */從PDF中加載分佈
interarrival_dist_ptr= oms_dist_load_from_string (interarrival_str);
pksize_dist_ptr = oms_dist_load_from_string (size_str);
/* Verifythe existence of the packet format to be used for */
/*generated packets. */
if(strcmp (format_str, "NONE") == 0) 檢查是否爲無格式的包
{
/* We will generate unformattedpackets. Set the flag. */
generate_unformatted = OPC_TRUE;
}
else
{
/* We will generate formatted packets.Turn off the flag. */
generate_unformatted = OPC_FALSE;
/* Get the list of all available packetformats.*/函數返回一系列包格式的名字
pk_format_names_lptr =prg_tfile_name_list_get (PrgC_Tfile_Type_Packet_Format);
/* Search the list for the requestedpacket format.*/查找所需要的包格式
format_found = OPC_FALSE;
for (i = prg_list_size(pk_format_names_lptr); ((format_found == OPC_FALSE) && (i > 0));i--)
{
/* Access the next formatname and compare with requested */
/* format name. */
found_format_str = (char *)prg_list_access (pk_format_names_lptr, i - 1);
if (strcmp (found_format_str,format_str) == 0)
format_found =OPC_TRUE;找到了所需要的包格式
}
if (format_found == OPC_FALSE)
{
/* The requested format doesnot exist. Generate */
/* unformatted packets. */
generate_unformatted =OPC_TRUE;
/* Display an appropriatewarning.*/沒有找到需要的包格式,輸出警告
op_prg_odb_print_major("Warning from simple packet generator model (simple_source):",
"Thespecified packet format", format_str,
"isnot found. Generating unformatted packets instead.", OPC_NIL);
}
/* Destroy the lits and its elementssince we don't need it */
/* anymore. */
prg_list_free(pk_format_names_lptr);釋放內存
prg_mem_free (pk_format_names_lptr);
}
/* Makesure we have valid start and stop times, i.e. stop time is */
/* notearlier than start time. */
if((stop_time <= start_time) && (stop_time != SSC_INFINITE_TIME))
{如果結束時間早於開始時間或結束時間不爲無窮
/* Stop time is earlier than starttime. Disable the source. */
start_time = SSC_INFINITE_TIME; 設置開始時間爲無窮並且輸出警告
/* Display an appropriate warning. */
op_prg_odb_print_major ("Warningfrom simple packet generator model (simple_source):",
"Althoughthe generator is not disabled (start time is set to a finite value),",
"astop time that is not later than the start time is specified.",
"Disablingthe generator.", OPC_NIL);
}
/*Schedule a self interrupt that will indicate our start time for */
/* packetgeneration activities. If the source is disabled, */
/*schedule it at current time with the appropriate code value. */
if(start_time == SSC_INFINITE_TIME) 如果開始時間是無窮,安排自中斷開始時間爲仿真時間,否則爲實際開始時間
{
op_intrpt_schedule_self (op_sim_time(), SSC_STOP);
}
else
{
op_intrpt_schedule_self (start_time, SSC_START);
/* In this case, also schedule theinterrupt when we will stop */
/* generating packets, unless we areconfigured to run until */
/* the end of the simulation. */ 仿真結束時間安排一個自中斷
if (stop_time != SSC_INFINITE_TIME)
{
op_intrpt_schedule_self(stop_time,SSC_STOP);
}
next_intarr_time = oms_dist_outcome(interarrival_dist_ptr);通過一個分佈生成一個浮點數
/* Make sure that interarrival time isnot negative. In that case it */
/* will be set to 0. */確認間隔時間不爲負,否則設爲0
if (next_intarr_time <0)
{
next_intarr_time = 0.0;
}
}
/*Register the statistics that will be maintained by this model. */聲明局部統計量
bits_sent_hndl =op_stat_reg ("Generator.Traffic Sent (bits/sec)", OPC_STAT_INDEX_NONE,OPC_STAT_LOCAL);
packets_sent_hndl = op_stat_reg ("Generator.Traffic Sent(packets/sec)", OPC_STAT_INDEX_NONE,OPC_STAT_LOCAL);
packet_size_hndl = op_stat_reg ("Generator.Packet Size(bits)", OPC_STAT_INDEX_NONE, OPC_STAT_LOCAL);
interarrivals_hndl = op_stat_reg ("Generator.PacketInterarrival Time (secs)", OPC_STAT_INDEX_NONE, OPC_STAT_LOCAL);
init出口程序
/*Determine the code of the interrupt, which is used in evaluating */
/* statetransition conditions. */
intrpt_code= op_intrpt_code ();
init轉到generate狀態時的執行的函數代碼
staticvoid
ss_packet_generate(void)
{
Packet* pkptr;
double pksize;
/** This function creates a packetbased on the packet generation**/
/** specifications of the source modeland sends it to the lower layer.**/
FIN (ss_packet_generate ());
/* Generate a packet size outcome. */
pksize = (double) ceil(oms_dist_outcome (pksize_dist_ptr));
ceil返回大於或等於指定表達式的最小整數
/* Create a packet of specified formatand size.*/
if (generate_unformatted == OPC_TRUE)
{
/* We produce unformattedpackets. Create one. */
pkptr = op_pk_create(pksize);
}
else
{
/* Create a packet with the specifiedformat.*/
pkptr = op_pk_create_fmt(format_str);創建一個預定義結構的包,返回包格式
op_pk_total_size_set (pkptr,pksize); 設置包的大小
}
/* Update the packet generationstatistics. */
op_stat_write (packets_sent_hndl, 1.0);
op_stat_write (packets_sent_hndl, 0.0);
op_stat_write (bits_sent_hndl, (double)pksize);
op_stat_write (bits_sent_hndl, 0.0);
op_stat_write (packet_size_hndl,(double) pksize);
op_stat_write (interarrivals_hndl,next_intarr_time);
/* Send the packet via the stream tothe lower layer. */
op_pk_send (pkptr, SSC_STRM_TO_LOW);將包發送到輸出包流中
FOUT;
}
generate狀態的入口代碼
/* At theenter execs of the "generate" state we schedule the */
/*arrival of the next packet. */
next_intarr_time= oms_dist_outcome (interarrival_dist_ptr);
/* Makesure that interarrival time is not negative. In that case it */
/* will beset to 0. */ 確定包生成的間隔時間是有效的
if(next_intarr_time <0)
{
next_intarr_time = 0;
}
next_pk_evh= op_intrpt_schedule_self (op_sim_time () + next_intarr_time, SSC_GENERATE);
generate的出口代碼
/*Determine the code of the interrupt, which is used in evaluating */
/* statetransition conditions. */
intrpt_code= op_intrpt_code ();
stop狀態的入口代碼
/* Whenwe enter into the "stop" state, it is the time for us to */
/* stopgenerating traffic. We simply cancel the generation of the */
/* nextpacket and go into a silent mode by not scheduling anything */
/* else. */
if(op_ev_valid (next_pk_evh) == OPC_TRUE)
{
op_ev_cancel (next_pk_evh); 撤銷事件
}