Skip to content

Commit

Permalink
Multihost functional. Also added debug arg.
Browse files Browse the repository at this point in the history
Multihost is now fully functional. Most the the time was spent debugging an error caused by me being an idiot mixing up neighbour ids and station ids. Yeah.
Also, made commandline arg 3 (index 2, first after -a) determine the log level based on enummeration.

Next big thing is the report I guess and more extensive testing. And a less stupid way of testing in general.
  • Loading branch information
Zelaven committed Oct 17, 2017
1 parent 303de19 commit 4bc0390
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 19 deletions.
5 changes: 3 additions & 2 deletions debug.c
Expand Up @@ -14,14 +14,15 @@
extern mlock_t *write_lock;
extern int ThisStation;


int global_log_level_limit;

int logLine(log_level level, const char * __restrict format, ... ) {
va_list arg;
int rv;
char *level_string;

int log_level_limit = succes; // INFO: Here you can set the log level you wish to output
//int log_level_limit = succes; // INFO: Here you can set the log level you wish to output
int log_level_limit = global_log_level_limit;

switch(level) {
case trace:
Expand Down
56 changes: 42 additions & 14 deletions rdt.c
Expand Up @@ -80,10 +80,11 @@ static void send_frame(frame_kind fk, seq_nr frame_nr, seq_nr frame_expected, pa
{
neighbours[0].no_nak = false; /* one nak per frame, please */
}
//logLine(succes, "Sending frame to physical layer for recipient: %d\n", recipient);
to_physical_layer(&s, neighbours[recipient].stationID); /* transmit the frame */
if (fk == DATA)
{
start_timer(neighbours[recipient].stationID, frame_nr); //TODO
start_timer(recipient, frame_nr); //TODO
}
stop_ack_timer(0); /* no need for separate ack frame */
}
Expand Down Expand Up @@ -265,7 +266,12 @@ void selective_repeat() {
//logLine(trace, "Timeout with id: %d - acktimer_id is %d\n", timer_id, ack_timer_id);
//logLine(trace, "Timeout with id: %d - acktimer_id is %d\n", timer_id, ack_timer_ids[0]);
logLine(trace, "Timeout with id: %d - acktimer_id is %d\n", timer_id, neighbours[0].ack_timer_id);
logLine(info, "Message from timer: '%s'\n", (char *) event.msg );
//logLine(info, "Message from timer: '%s'\n", (char *) event.msg ); //[PJ] since it is no longer a char*, this won't work.

//Figure out which neighbour to resend to.
currentNeighbour = ((packetTimerMessage*) event.msg)->neighbour;
//logLine(succes, "Timer values: %d, %d\n", ((packetTimerMessage*)event.msg)->k, currentNeighbour);
//logLine(succes, "timer stuff: %s\n", (char *) event.msg);

//if( timer_id == ack_timer_id ) { // Ack timer timer out
//if( timer_id == ack_timer_ids[0] ) { // Ack timer timer out
Expand All @@ -278,7 +284,7 @@ void selective_repeat() {
send_frame(ACK,0,neighbourData[currentNeighbour].frame_expected, neighbourData[currentNeighbour].out_buf, currentNeighbour); /* ack timer expired; send ack */
} else {
//int timed_out_seq_nr = atoi( (char *) event.msg );
int timed_out_seq_nr = ((char *) event.msg)[4];
int timed_out_seq_nr = ((packetTimerMessage *) event.msg)->k;

logLine(debug, "Timeout for frame - need to resend frame %d\n", timed_out_seq_nr);
send_frame(DATA, timed_out_seq_nr, neighbourData[currentNeighbour].frame_expected, neighbourData[currentNeighbour].out_buf, currentNeighbour);
Expand Down Expand Up @@ -309,6 +315,11 @@ neighbourid stationID2neighbourindex(int stationID) {
int i = 0;
while(neighbours[i].stationID != stationID) {
i++;

if(i >= NUM_MAX_NEIGHBOURS) {
logLine(error, "stationID2neighboutindex: unable to find neighbour for station: %d\n", stationID);
Stop(); //[PJ] Nope!
}
}

return i;
Expand Down Expand Up @@ -404,20 +415,30 @@ void to_physical_layer(frame *s, int target)
}*/

print_frame(s, "sending");


//char temp[MAX_PKT+1];
//packet_to_string(&(s->info), temp);
//ToSubnet(ThisStation, send_to, (char *) s, sizeof(frame));
ToSubnet(ThisStation, target, (char *) s, sizeof(frame));
//logLine(succes, "Trying to send packet to: %d; with contents: %s\n", target, temp);
}


void start_timer(neighbourid neighbour, seq_nr k) {

char *msg;
//char *msg;
//msg = (char *) malloc(100*sizeof(char));
//sprintf(msg, "%d", k); // Save seq_nr in message
msg = malloc(sizeof(neighbourid)+sizeof(seq_nr));
msg[0] = neighbour;
msg[sizeof(neighbourid)] = k;
//msg = malloc(sizeof(neighbourid)+sizeof(seq_nr));
//msg[0] = neighbour;
//msg[sizeof(neighbourid)] = k;

//logLine(succes, "starting timer with values: %d, %d\n", k, neighbour);
packetTimerMessage *msg = malloc(sizeof(packetTimerMessage));
msg->k = k;
msg->neighbour = neighbour;

//char *msg = "Du Milde Moses!\n"; //[PJ] Just debugging

//timer_ids[k % NR_BUFS] = SetTimer( frame_timer_timeout_millis, (void *)msg );
neighbours[neighbour].timer_ids[k % NR_BUFS] = SetTimer( frame_timer_timeout_millis, (void *)msg );
Expand Down Expand Up @@ -451,9 +472,12 @@ void start_ack_timer(neighbourid neighbour)
//if( ack_timer_ids[neighbour] == -1 ) {
if( neighbours[neighbour].ack_timer_id == -1 ) {
logLine(trace, "Starting ack-timer\n");
char *msg;
msg = (char *) malloc(100*sizeof(char));
strcpy(msg, "Ack-timer");
//char *msg;
//msg = (char *) malloc(100*sizeof(char));
//strcpy(msg, "Ack-timer");
packetTimerMessage *msg = malloc(sizeof(packetTimerMessage));
msg->k = -1;
msg->neighbour = neighbour;
//ack_timer_id = SetTimer( act_timer_timeout_millis, (void *)msg );
//ack_timer_ids[neighbour] = SetTimer( act_timer_timeout_millis, (void *)msg );
neighbours[neighbour].ack_timer_id = SetTimer( act_timer_timeout_millis, (void *)msg );
Expand Down Expand Up @@ -483,14 +507,18 @@ void stop_ack_timer(neighbourid neighbour)
}



extern int global_log_level_limit;
int main(int argc, char *argv[])
{
StationName = argv[0];
ThisStation = atoi( argv[1] );

if (argc == 3)
if (argc == 3) {
printf("Station %d: arg2 = %s\n", ThisStation, argv[2]);
global_log_level_limit = atoi(argv[2]);
} else {
global_log_level_limit = succes;
}

mylog = InitializeLB("mytest");

Expand Down Expand Up @@ -539,7 +567,7 @@ int main(int argc, char *argv[])
ACTIVATE(3, FakeNetworkLayer_Test1);
ACTIVATE(3, selective_repeat);


//asdasd
/* simuleringen starter */
Start();
exit(0);
Expand Down
4 changes: 4 additions & 0 deletions rdt.h
Expand Up @@ -90,6 +90,10 @@ void start_timer(neighbourid neighbour, seq_nr k);
void stop_timer(neighbourid neighbour, seq_nr k);

/* Start an auxiliary timer and enable the ack_timeout event. */
typedef struct {
seq_nr k;
neighbourid neighbour;
} packetTimerMessage;
void start_ack_timer(unsigned int neighbor);

/* Stop the auxiliary timer and disable the ack_timeout event. */
Expand Down
6 changes: 3 additions & 3 deletions rdt_fakeNetworkLayers.c
Expand Up @@ -140,7 +140,7 @@ void FakeNetworkLayer_Test1()
// Setup some messages
for( i = 0; i < 2; i++ ) {
buffer = (char *) malloc ( sizeof(char) * MAX_PKT);
sprintf( buffer, "%d to %d", ThisStation, i );
sprintf( buffer, "%d to %d", ThisStation, neighbours[i].stationID );
EnqueueFQ( NewFQE( (void *) buffer ), from_network_layer_queue );
}

Expand All @@ -161,7 +161,7 @@ void FakeNetworkLayer_Test1()
// Signal element is ready
logLine(info, "Sending signal for message #%d\n", i);
network_layer_enabled = false;
Signal(network_layer_ready, 1);
Signal(network_layer_ready, i);
i++;
}
Unlock( network_layer_lock );
Expand All @@ -178,7 +178,7 @@ void FakeNetworkLayer_Test1()
break;
}

if( i >= 20 && j >= 10) {
if( i >= 2 && j >= 2) {
logLine(info, "Station %d done. - (\'sleep(5)\')\n", ThisStation);
// A small break, so all stations can be ready
sleep(5);
Expand Down

0 comments on commit 4bc0390

Please sign in to comment.