您的位置:首页 > 运维架构

csma_0

2017-01-20 10:28 375 查看

节点模型 lmz_cct_csma_tx



进程模型 bbbccc_csma_tx



SV



TV

char  node_name [128];
char  module_name [128];
int namelength;

int i;
int i_temp;


HB

/* Input stream from ideal generator module */
#define IN_STRM     0
#define ACK_STRM 1
/* Output stream to bus transmitter module */
#define OUT_STRM    0

/* Input statistic indices */
#define CH_BUSY_STAT    0

/* Conditional macros */
#define STRM_INTRPT ((op_intrpt_type() == OPC_INTRPT_STRM) && (op_intrpt_strm() == IN_STRM) && beacon == 1)
#define STAT_INTRPT (op_intrpt_type() == OPC_INTRPT_STAT && beacon == 1)
#define INTRPT_ROB ((op_intrpt_type() == OPC_INTRPT_SELF) && (op_intrpt_code() == ROB_INIT) && beacon == 1)
#define INTRPT_DATA ((op_intrpt_type() == OPC_INTRPT_SELF) && (op_intrpt_code() == DATA_INIT) && beacon == 1)
#define INTRPT_TIMEOUT ((op_intrpt_type() == OPC_INTRPT_SELF) && (op_intrpt_code() == TIMEOUT_INIT) && beacon == 1)

#define INTRPT_ACK ((op_intrpt_type() == OPC_INTRPT_STRM) && (op_intrpt_strm() == ACK_STRM) )

/* Global Variable */
extern int subm_pkts;
extern int sending;
extern int sended;
//extern int beacon;

#define FREE (op_stat_local_read (CH_BUSY_STAT) == 0.0)
#define PKTS_QUEUED (!op_subq_empty (0))

#define ROB_INIT 0
#define DATA_INIT 1
#define TIMEOUT_INIT 2

#include <string.h>
#include <math.h>


init

my_id = op_id_self ();
p_id = op_topo_parent(my_id);
op_ima_obj_attr_get (p_id, "name", node_name);
op_ima_obj_attr_get (my_id, "name", module_name);
printf("%s ",node_name);
printf("%s ",module_name);
printf("initing...\n");

namelength = strlen(node_name);
nodenum = 0;
i_temp = 0;
for(i=5;i<=namelength - 1;i++)
{

i_temp = node_name[i] - '0';
nodenum = 10*nodenum + i_temp;

}

printf("node number is %d\n",nodenum);

node_busy = 0;
difs = 0.01;
rob = -1;

aSlotTime = 0.02;

cw_current = 0;
cw_num = 2;
retry_num = 0;

beacon = 0;

sending = 0;
sended = 0;


src_arvl

Packet* data_pkt;

data_pkt = op_pk_get(IN_STRM);

//op_pk_nfd_set (data_pkt, "src", nodenum);
//op_pk_print (data_pkt);

op_subq_pk_insert (0, data_pkt, OPC_QPOS_TAIL);

if (FREE && node_busy == 0)
{

rob_event = op_intrpt_schedule_self (op_sim_time() + difs, ROB_INIT);
node_busy = 1;

}


sense

if(!FREE)
{

printf("one\n");

if(op_ev_valid(rob_event) == OPC_TRUE)
{
op_ev_cancel(rob_event);
node_busy = 0;

printf("two\n");
}
}

if (FREE && node_busy == 0 && PKTS_QUEUED)
{
rob_event = op_intrpt_schedule_self (op_sim_time() + difs, ROB_INIT);
node_busy = 1;

printf("three\n");

}

printf("four\n");


ROB

cw_current = pow(2,cw_num)-1;

rob = aSlotTime*(int)op_dist_uniform(cw_current);

printf("rob time is %f\n",rob);
printf("cw_num is %f\n",cw_num);
printf("cw_current is %f\n",cw_current);

cw_num++;

data_event = op_intrpt_schedule_self (op_sim_time() + rob, DATA_INIT);


DATA

Packet* data_pkt;

timeout_event = op_intrpt_schedule_self (op_sim_time() + 0.018, TIMEOUT_INIT);

printf("time out event scheduling...\n");

data_pkt = op_pk_create_fmt ("aaa_csma_ca_data");
op_pk_nfd_set (data_pkt, "src", nodenum);
op_pk_nfd_set (data_pkt, "time", op_pk_creation_time_get (op_subq_pk_access (0, 0) ));

op_pk_print (data_pkt);
op_pk_send(data_pkt,OUT_STRM);
printf("data sending...\n");


TIME_OUT

if (retry_num <= 10)
{

retry_num++;
//cw_current = 2^cw_num-1;
cw_current = pow(2,cw_num)-1;

rob = aSlotTime*(int)op_dist_uniform(cw_current);

printf("rob time is %f\n",rob);
printf("cw_num is %f\n",cw_num);
printf("cw_current is %f\n",cw_current);
printf("retry num is %d\n",retry_num);

++cw_num;

data_event = op_intrpt_schedule_self (op_sim_time() + 0.002 +rob, DATA_INIT);

}

else
{

op_subq_pk_remove (0, 0);

node_busy = 0;
cw_num = 2;
retry_num = 0;
cw_current = 0;

if(beacon == 1)
{
sended++;

if(sended == sending)
{

Packet* beacon_pkt;

beacon_pkt = op_pk_create_fmt ("aaa_packet_beacon_on");

op_pk_nfd_set (beacon_pkt, "on", 0);

op_pk_print (beacon_pkt);

op_pk_send(beacon_pkt,OUT_STRM);

sended = 0;
sending = 0;
}

}

}


ACK

Packet* pkptr;
int node_flag;
int node_src;
int node_on;

pkptr = op_pk_get(ACK_STRM);
op_pk_nfd_get_int32 (pkptr, "flag", &node_flag);

//ack packet
if (node_flag == 1)
{
op_pk_nfd_get_int32 (pkptr, "dest", &node_src);
if(node_src == nodenum)
{
printf("ack packet received!\n");

node_busy = 0;
cw_num = 2;
retry_num = 0;
cw_current = 0;

if(beacon == 1)
{
sended++;

if(sended == sending)
{

Packet* beacon_pkt;

beacon_pkt = op_pk_create_fmt ("aaa_packet_beacon_on");

op_pk_nfd_set (beacon_pkt, "on", 0);

op_pk_print (beacon_pkt);

op_pk_send(beacon_pkt,OUT_STRM);

sended = 0;
sending = 0;
}

}

if(op_ev_valid(timeout_event) == OPC_TRUE)
{
op_ev_cancel(timeout_event);
printf("timeout_event canceled\n");
}

op_pk_destroy(pkptr);
op_subq_pk_remove (0, 0);

if (op_subq_empty (0) == OPC_TRUE)

{
printf("subqueue is empty!\n");

}

}
else

{
op_pk_destroy(pkptr);

}

}

//csma data packet
if (node_flag == 0)
{

printf("data packet received!");
op_pk_destroy(pkptr);

}

//beacon packet
if (node_flag == 3)

{

op_pk_nfd_get_int32 (pkptr, "on", &node_on);

if(node_on == 1)

{
beacon = 1;

if (!op_subq_empty (0) == OPC_TRUE)
{

sending++;

}

}
else
{
beacon = 0;
}

printf("beacon packet received!\n");
printf("beacon status is %d\n",beacon);
op_pk_destroy(pkptr);

}

///////////////////////////////////////////
if (node_flag == 4)
{

op_pk_destroy(pkptr);

}
///////////////////////////////////////////
if (node_flag == 5)
{

op_pk_destroy(pkptr);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  opnet