//***************byteGenerator Layer*************** void applicationLayerByteGenerator(BYTES *_bytes) { int i; size_t rnd; while (1) { usleep(400 * 1000); rnd = rand() % 8; _bytes->size = rnd; if (rnd > 0) { _bytes->byteArr = (char*) malloc((rnd) * sizeof(char)); for (i = 0; i < rnd; i++) { _bytes->byteArr[i] = rand(); } } else { _bytes->byteArr = NULL; } transportLayer(_bytes);//send them out to a transport layer. if (rnd > 0) { free(_bytes->byteArr); } } }
int main(int argc, char **argv) { int ret = validateArgs(argc, argv); if(ret == 0){ printf("Argument's not supported\n"); return 1; } else if(ret == 2){ return 0; } else if(ret == 1){ transportLayer(); } return 0; }
void *physicalPacket(BYTES *_bytes) { time_t lastByte = time(NULL); if (_bytes->mode == 0) { switch (_bytes->statePhy) { case PHYSICAL_SEND_TO_BYTE_PHYSICAL: _bytes->mode = 1; _bytes->statePhy = PHYSICAL_GET_FROM_PACKET_PHYSICAL; for (;;) { //wait to transport/////////////////////////// pthread_mutex_lock(&tranprotPhysical); if (bufferIndexTransToPhy == -1) { pthread_cond_wait(&BufferTtP_Not_Empty, &tranprotPhysical); } if (time(NULL) - lastByte > 1) { physicalByte(_bytes); } _bytes->byteArr[_bytes->size++] = BUFFERTtP[bufferIndexTransToPhy--]; lastByte = time(NULL); if (_bytes->size == 10) { physicalByte(_bytes); } pthread_mutex_unlock(&tranprotPhysical); pthread_cond_signal(&BufferTtP_Not_Full); ///////////////////////////////////////////////// } break; case PHYSICAL_SEND_TO_PACKET_PHYSICAL: for (;;) { pthread_mutex_lock(&physicalM); if (bufferIndexPhyToPhy == BufferSize) { pthread_cond_wait(&BufferPtP_Not_Full, &physicalM); } ///////////wait to transport/////////////////////////// pthread_mutex_lock(&tranprotPhysical); if (bufferIndexTransToPhy == -1) { pthread_cond_wait(&BufferTtP_Not_Empty, &tranprotPhysical); } while (!(time(NULL) - lastByte > 1 || _bytes->size == 10 || bufferIndexPhyToPhy > 9 || bufferIndexTransToPhy < 0)) { BUFFERPtP[bufferIndexPhyToPhy++] = BUFFERTtP[bufferIndexTransToPhy--]; lastByte = time(NULL); } pthread_mutex_unlock(&tranprotPhysical); pthread_cond_signal(&BufferTtP_Not_Full); ///////////////////////////////////////////////////// pthread_mutex_unlock(&physicalM); pthread_cond_signal(&BufferPtP_Not_Empty); } break; } } else { for (;;) { pthread_mutex_lock(&physicalM); if (bufferIndexPhyToPhy == -1) { pthread_cond_wait(&BufferPtP_Not_Empty, &physicalM); } if (time(NULL) - lastByte > 1) { transportLayer(_bytes); } while (bufferIndexPhyToPhy > 0) {//taking all the packet _bytes->byteArr[_bytes->size++] = BUFFERPtP[bufferIndexPhyToPhy--]; lastByte = time(NULL); if (_bytes->size == 10) { transportLayer(_bytes); } } pthread_mutex_unlock(&physicalM); pthread_cond_signal(&BufferPtP_Not_Full); } } free(_bytes->byteArr); free(_bytes); return 0; }
//***************Physical Layer********************************* void *physicalByte(BYTES *_bytes) { int i = 0; if (_bytes->mode == 0) { for (;;) { pthread_mutex_lock(&physicalM); if (bufferIndexPhyToPhy == BufferSize) { pthread_cond_wait(&BufferPtP_Not_Full, &physicalM); } //wait to transport////////////////////// pthread_mutex_lock(&tranprotPhysical); if (bufferIndexTransToPhy == -1) { pthread_cond_wait(&BufferTtP_Not_Empty, &tranprotPhysical); } BUFFERPtP[bufferIndexPhyToPhy++] = BUFFERTtP[bufferIndexTransToPhy--]; pthread_mutex_unlock(&tranprotPhysical); pthread_cond_signal(&BufferTtP_Not_Full); ////////////////////////////////////////// pthread_mutex_unlock(&physicalM); pthread_cond_signal(&BufferPtP_Not_Empty); } } else { switch (_bytes->statePhy) { case PHYSICAL_GET_FROM_BYTE_PHYSICAL: for (;;) { pthread_mutex_lock(&physicalM); if (bufferIndexPhyToPhy == -1) { pthread_cond_wait(&BufferPtP_Not_Empty, &physicalM); } _bytes->byteArr[0] = BUFFERPtP[bufferIndexPhyToPhy--]; transportLayer(_bytes); pthread_mutex_unlock(&physicalM); pthread_cond_signal(&BufferPtP_Not_Full); } break; case PHYSICAL_GET_FROM_PACKET_PHYSICAL: _bytes->size--; for (i = 0; i < _bytes->size - 1; i++) { _bytes->byteArr[0] = _bytes->byteArr[i]; transportLayer(_bytes); } _bytes->size = 0; break; } return 0; } free(_bytes->byteArr); free(_bytes); return 0; }