C++ in a UNIX environment Part1 - Create a one-directional circular buffer (ring
ID: 665576 • Letter: C
Question
C++ in a UNIX environment
Part1 - Create a one-directional circular buffer (ring) of pipes for N processes (N>=1) for the ring (one-directional pipe) of process sending and receiving message
Part2 - implement Part1 for thread (a ring of thread).
Part3 - implement Part1 for process communicating with socket to each other.
You may run N processes in one host.
Each process has two designated ports - one for sending and the other for receiving.
Each program for each process will be run with an argument telling which process (Nth process)
You may assume that each process has a table of process with 2 ports as following:
Process i
IP address
Port# to send
Port# to receive
0
127.0.0.1
9000
9001
1
127.0.0.1
9100
9101
2
127.0.0.1
9200
9201
and so on
When N processes (or threads) in a ring are up and ready, Process 0 sends a message to be received by Process 1, and so on, to back to Process 0 and done. Whenever a process sends or receives a message, it prints its process # (N), "the message", and "sent" or "received".
Process i
IP address
Port# to send
Port# to receive
0
127.0.0.1
9000
9001
1
127.0.0.1
9100
9101
2
127.0.0.1
9200
9201
and so on
Explanation / Answer
#include #include #include #include using namespace irr; using namespace core; using namespace scene; using namespace video; using namespace io; using namespace gui; const int WINDOW_WIDTH = 800; const int WINDOW_HEIGHT = 600; bool g_running = true; // Process keyboard event class MyEventReceiver : public IEventReceiver { public: // This is the one method that we have to implement virtual bool OnEvent(const SEvent& event) { if (event.EventType == irr::EET_KEY_INPUT_EVENT) { if(event.KeyInput.Key == KEY_ESCAPE) { g_running = false; } } return false; } }; void videoCaptureFunc( cv::Mat& _frame ) { cv::VideoCapture capture(0); while( true ) { capture >> _frame; if( not _frame.empty() ) { std::cout setWindowCaption(L"Irrlicht Rendering Test "); // /* // Get a pointer to the VideoDriver, the SceneManager and the graphical // user interface environment, so that we do not always have to write // device->getVideoDriver(), device->getSceneManager(), or // device->getGUIEnvironment(). // */ // IVideoDriver* driver = device->getVideoDriver(); // ISceneManager* smgr = device->getSceneManager(); // IGUIEnvironment* guienv = device->getGUIEnvironment(); // // /* // We add a hello world label to the window, using the GUI environment. // The text is placed at the position (10,10) as top left corner and // (260,22) as lower right corner. // */ // guienv->addStaticText(L"This is a Macaca!", // rect(10,10,242,26), true); // // /* // Add texture to store camera frames // */ // ITexture* frame_tex = // driver->addTexture(vector2d(WINDOW_WIDTH, WINDOW_HEIGHT), "video_stream"); // // /* // Add a dull scene node to put a point light with a circular animation. // It would be better to put a directional one, but I was not able to find one // in the documentation. // If I create a custom scene node, as the tutorial 4 explains, it is possible // to create a scene node, anyway. // */ // ISceneNode* light_node = 0; // light_node = smgr->addLightSceneNode(0, vector3df(0,10,0), // SColorf(1.0f, 0.6f, 0.7f, 1.0f), 800.0f); // // ISceneNodeAnimator* anim = 0; // anim = smgr->createFlyCircleAnimator (vector3df(0,150,0),250.0f); // light_node->addAnimator(anim); // anim->drop(); // // /* // Create a camera controlled like a FPS camera. // */ // ISceneNode* camera_node = 0; // camera_node = smgr->addCameraSceneNodeFPS(); // camera_node->setPosition( vector3df(-50.0f, 80.0f, 80.0f) ); // static_cast(camera_node)->setTarget( vector3df(0.0f) ); // // /* // Adding a simple .obj model. // */ // IAnimatedMesh* mesh = smgr->getMesh("models/suzanne.obj"); // if (!mesh) // { // device->drop(); // return 1; // } // IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode( mesh ); // /* // Scale suzanne mesh to make it look bigger. // */ // smgr->getMeshManipulator()->scale( mesh, vector3df(40.0f)); // /* // Playing with suzanne material... // */ // node->setMaterialFlag(EMF_ANTI_ALIASING, true); // // /* // The model needs to look better. Now I will add a simple light. // */ // if (node) // { // node->setMaterialFlag(EMF_LIGHTING, true); // } /* Create an OpenCV object to capture camera frames and an image to store it. We also create a simple std thread to execute this action. */ // cv::VideoCapture capture(0); cv::Mat camera_frame; std::thread video_capture_thread( videoCaptureFunc, std::ref(camera_frame) ); /* Start rendering loop and video capture thread */ video_capture_thread.detach(); MyEventReceiver receiver; IrrlichtDevice* device = createDevice( video::EDT_OPENGL, dimension2d(WINDOW_WIDTH,WINDOW_HEIGHT), 16, false, false, false, &receiver); // while( camera_frame.empty() ) // capture >> camera_frame; // while(device->run()) while( true && g_running ) { // if( cv::waitKey(50) >= 0 ) break; // if( !capture.grab() ) // { // std::cout > camera_frame; // // if( ! camera_frame.empty() ) // { // std::cout lock(); // unsigned char *frame_buf = camera_frame.data; // // Convert from RGB to RGBA // for(int y=0; y beginScene(true, true, SColor(255,100,101,140)); // // /* // Draws camera image on screen. // */ // driver->draw2DImage(frame_tex, core::rect(0,0,WINDOW_WIDTH,WINDOW_HEIGHT), // core::rect(0,0,WINDOW_WIDTH,WINDOW_HEIGHT)); // // smgr->drawAll(); // guienv->drawAll(); // // driver->endScene(); } // device->drop(); return 0; }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.