void on_surface_changed(int width, int height) { Message* m = MESSAGE(Message::MSG_RESIZE); m->AddData(DAT(width)); m->AddData(DAT(height)); gCore->SendMessage(m); }
status_t ConvertFromAMessage(const os::Message & from, Message & to) { to.Clear(); to.what = from.GetCode(); int numNames = from.GetNumNames(); for (int32 i=0; i<numNames; i++) { int type; int count; std::string name = from.GetName(i); if (from.GetNameInfo(name.c_str(), &type, &count) == B_NO_ERROR) { for (int j=0; j<count; j++) { const void * nextItem; size_t itemSize; if (from.FindData(name.c_str(), type, &nextItem, &itemSize, j) != B_NO_ERROR) return B_ERROR; // do any necessary translation from the AtheOS data types to Muscle data types switch(type) { case os::T_POINT: { const os::Point * p = static_cast<const os::Point *>(nextItem); Point pPoint(p->x, p->y); if (to.AddPoint(name.c_str(), pPoint) != B_NO_ERROR) return B_ERROR; } break; case os::T_RECT: { const os::Rect * r = static_cast<const os::Rect *>(nextItem); Rect pRect(r->left, r->top, r->right, r->bottom); if (to.AddRect(name.c_str(), pRect) != B_NO_ERROR) return B_ERROR; } break; case os::T_MESSAGE: { os::Message amsg; if (amsg.Unflatten(static_cast<const uint8 *>(nextItem)) != B_NO_ERROR) return B_ERROR; Message * newMsg = newnothrow Message; if (newMsg) { MessageRef msgRef(newMsg); if (ConvertFromAMessage(amsg, *newMsg) != B_NO_ERROR) return B_ERROR; if (to.AddMessage(name.c_str(), msgRef) != B_NO_ERROR) return B_ERROR; } else {WARN_OUT_OF_MEMORY; return B_ERROR;} } break; default: if (to.AddData(name.c_str(), type, nextItem, itemSize) != B_NO_ERROR) return B_ERROR; break; } } } } return B_NO_ERROR; }
int main(int argc, char **argv) { // Testing standard message passing with short message {{{ { string shortmsg="Hello World"; Channel_FIFO *c1; try { c1=new Channel_FIFO(); } catch (string s) { cerr << "Error in channel creation." << endl << "Message was: " << s << endl; return 1; } try // {{{ { Message msg; msg.AddData(shortmsg.c_str(),shortmsg.size()+1); c1->SingleSend(msg); cout << "Short message sent." << endl; } catch (string s) { cerr << "Error sending message." << endl << "Message was: " << s << endl; return 1; } // }}} try // {{{ { Message msg; c1->SingleReceive(msg); string msgstr=msg.GetData(); if (msgstr==shortmsg) cout << "Received short message correctly." << endl; else throw (string)"Received wrong message:\n" + "Original Message: " + shortmsg + "\n" + "Received Message: " + msgstr; } catch (string s) { cerr << "Error receiving message." << endl << "Message was: " << s << endl; return 1; } // }}} try // {{{ { Message msg; msg.AddData(shortmsg.c_str(),shortmsg.size()+1); c1->SingleSend(msg); cout << "Short message sent." << endl; } catch (string s) { cerr << "Error sending message." << endl << "Message was: " << s << endl; return 1; } // }}} try // {{{ { Message msg; c1->SingleReceive(msg); string msgstr=msg.GetData(); if (msgstr==shortmsg) cout << "Received short message correctly." << endl; else throw (string)"Received wrong message:\n" + "Original Message: " + shortmsg + "\n" + "Received Message: " + msgstr; } catch (string s) { cerr << "Error receiving message." << endl << "Message was: " << s << endl; return 1; } // }}} c1->Unlink(); delete c1; } // }}} return 0; }
int main(int argc, char **argv) { system("mkfifo ./ch1"); system("mkfifo ./ch2"); pid_t pid=fork(); switch (pid) { case -1: // Error {{{ cerr << "Error during fork!" << endl; return 1; // }}} case 0: // child process {{{ try { vector<Channel*> link; link.push_back(new Channel_FIFO("./ch1")); link.push_back(new Channel_FIFO("./ch2")); Session_FIFO s(link,0,3); Message msg; string strmsg1="P0->P1"; msg.AddData(strmsg1.c_str(),strmsg1.size()+1); s.Send(1,msg); cout << "Sent: " << strmsg1 << " to participant 1" << endl; msg.Clear(); string strmsg2="P0->P2"; msg.AddData(strmsg2.c_str(),strmsg2.size()+1); s.Send(2,msg); cout << "Sent: " << strmsg2 << " to participant 2" << endl; msg.Clear(); s.Receive(1,msg); cout << "Participant 0 received message: " << msg.GetData() << endl; msg.Clear(); s.Receive(2,msg); cout << "Participant 0 received message: " << msg.GetData() << endl; } catch (string s) { cerr << "Error in child (0) process." << endl << "Message was: " << s << endl; return 1; } break; // }}} default: // parent {{{ pid=fork(); switch (pid) { case -1: // Error {{{ cerr << "Error during fork!" << endl; return 1; // }}} case 0: // child process // {{{ try { vector<Channel*> link; link.push_back(new Channel_FIFO("./ch1")); link.push_back(new Channel_FIFO("./ch2")); cout << "PGM: Linking as participant 1" << endl; Session_FIFO s(link,1,3); Message msg; string strmsg0="P1->P0"; msg.AddData(strmsg0.c_str(),strmsg0.size()+1); s.Send(0,msg); cout << "Sent. " << strmsg0 << " to participant 0" << endl; msg.Clear(); string strmsg2="P1->P2"; msg.AddData(strmsg2.c_str(),strmsg2.size()+1); s.Send(2,msg); cout << "Sent. " << strmsg2 << " to participant 2" << endl; msg.Clear(); s.Receive(0,msg); cout << "Participant 1 received message: " << msg.GetData() << endl; msg.Clear(); s.Receive(2,msg); cout << "Participant 1 received message: " << msg.GetData() << endl; } catch (string s) { cerr << "Error in child (1) process." << endl << "Message was: " << s << endl; return 1; } break; // }}} default: // parent {{{ try { vector<Channel*> link; link.push_back(new Channel_FIFO("./ch1")); link.push_back(new Channel_FIFO("./ch2")); link[0]->Unlink(); link[1]->Unlink(); Session_FIFO s(link,2,3); Message msg; string strmsg0="P2->P0"; msg.AddData(strmsg0.c_str(),strmsg0.size()+1); s.Send(0,msg); cout << "Sent. " << strmsg0 << " to participant 0" << endl; msg.Clear(); string strmsg1="P2->P1"; msg.AddData(strmsg1.c_str(),strmsg1.size()+1); s.Send(1,msg); cout << "Sent. " << strmsg1 << " to participant 2" << endl; msg.Clear(); s.Receive(0,msg); cout << "Participant 2 received message: " << msg.GetData() << endl; msg.Clear(); s.Receive(1,msg); cout << "Participant 2 received message: " << msg.GetData() << endl; delete link[0]; delete link[1]; } catch (string s) { cerr << "Error in parrent (2) process." << endl << "Message was: " << s << endl; return 1; } break; // }}} } break; // }}} } return 0; }