/*********************************************************************** hotpotato.c ************************************************************************/ #include #include #include #include int frameNumber = 0; #define CHECK(call) if( (call) != 0 ) {\ (void)fprintf( stderr, "%s:%s, line%d : %s\n",\ nodeinfo.nodename, __FILE__, __LINE__, \ cnet_errstr[cnet_errno]); exit(1); } #define FRAME_HEADER_SIZE (2*sizeof(CnetAddr)+2*sizeof(int)) typedef struct { int frameID; int hops; CnetAddr source, destination; char data[MAX_MESSAGE_SIZE]; } Frame; static void AppReady( CnetEvent ev, CnetTimestamp ts, CnetData data ); static void PhysReady( CnetEvent ev, CnetTimestamp ts, CnetData data ); void reboot_node( CnetEvent ev, CnetTimestamp ts, CnetData data ) { CNET_set_handler( EV_APPLICATIONREADY, AppReady, 0 ); CNET_set_handler( EV_PHYSICALREADY, PhysReady, 0 ); CNET_enable_application( ALLNODES ); (void)srandom( (int)time(NULL) ); } static void AppReady( CnetEvent ev, CnetTimestamp ts, CnetData data ) { int link, length = MAX_MESSAGE_SIZE; Frame f; CHECK(CNET_read_application( &(f.destination), f.data, &length )); f.hops = 0; f.source = nodeinfo.address; f.frameID = ++frameNumber; length += FRAME_HEADER_SIZE; link = ((int)random() % nodeinfo.nlinks) + 1; CHECK(CNET_write_physical_reliable( link, (char *)(&f), &length )); fprintf( stderr, "Originating frame #%d from %ld bound for %ld\n", f.frameID, f.source, f.destination ); } static void PhysReady( CnetEvent ev, CnetTimestamp ts, CnetData data ) { int link, length = MAX_MESSAGE_SIZE + FRAME_HEADER_SIZE; Frame f; CHECK(CNET_read_physical( &link, (char *)(&f), &length )); (f.hops)++; if( f.destination == nodeinfo.address ) { length -= FRAME_HEADER_SIZE; CNET_write_application( f.data, &length ); fprintf( stderr, "Delivering frame #%d to %ld after %d hops\n", f.frameID, nodeinfo.address, f.hops ); } else { link = (random() % nodeinfo.nlinks) + 1; CHECK(CNET_write_physical_reliable( link, (char *)(&f), &length )); /*fprintf( stderr, "\tFrame #%d passing through %ld on %dth hop\n", f.frameID, nodeinfo.address, f.hops ); */ } }