Created
December 3, 2012 14:43
-
-
Save adurpas/4195432 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ISU laboratory session #8, exercise #3. | |
| #include <osapi/Thread.hpp> | |
| #include <osapi/Message.hpp> | |
| #include <osapi/MsgQueue.hpp> | |
| #include <osapi/Utility.hpp> | |
| #include <unistd.h> | |
| #include <iostream> | |
| #include "car.hpp" | |
| using namespace std; | |
| car::car(int carThreadID, unsigned long queueMaxSize, entryGuard* entryGuard, exitGuard* exitGuard) : carQueue_(queueMaxSize) | |
| { | |
| carID_ = carThreadID; | |
| entryGuard_ = entryGuard; | |
| exitGuard_ = exitGuard; | |
| } | |
| osapi::MsgQueue* car::getMsgQueue() | |
| { | |
| return &carQueue_; | |
| } | |
| void car::handler(unsigned long id, osapi::Message* msg) | |
| { | |
| switch(id) | |
| { | |
| case ID_CAR_START_IND: | |
| { | |
| //wait a while | |
| osapi::sleep(rand()%2000); | |
| cout << "Car #" << carID_ << " has arrived to the entrance" << endl; | |
| //Create message | |
| entryGuardOpenReq* msg = new entryGuardOpenReq; | |
| msg->mq_ = getMsgQueue(); | |
| //Send message to entrygate | |
| entryGuard_->getMsgQueue()->send(entryGuard::ID_ENTRY_OPEN_REQ, msg); | |
| break; | |
| } | |
| case ID_ENTRY_OPEN_CFM: | |
| { | |
| cout << "Car #" << carID_ << " is parked" << endl; | |
| //Send message to entryguard | |
| entryGuard_->getMsgQueue()->send(entryGuard::ID_CAR_ENTERED_IND); | |
| osapi::sleep(rand()%2000); | |
| cout << "Car #" << carID_ << " has arrived to the exit" << endl; | |
| //Create message | |
| exitGuardOpenReq* req = new exitGuardOpenReq; | |
| req->mq_ = getMsgQueue(); | |
| //Send message to entrygate | |
| exitGuard_->getMsgQueue()->send(exitGuard::ID_EXIT_OPEN_REQ, req); | |
| break; | |
| } | |
| case ID_EXIT_OPEN_CFM: | |
| { | |
| cout << "Car #" << carID_ << " has exited the parking lot" << endl; | |
| exitGuard_->getMsgQueue()->send(exitGuard::ID_CAR_EXITED_IND); | |
| break; | |
| } | |
| } | |
| } | |
| car::~car(){} | |
| void car::run() | |
| { | |
| for(;;) | |
| { | |
| unsigned long id; | |
| osapi::Message* msgPtr = getMsgQueue()->receive(id); //Receive message; | |
| handler(id, msgPtr); | |
| delete msgPtr; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment