|
| 1 | +#include "ofApp.h" |
| 2 | + |
| 3 | +#define USE_EXPLICIT_FUNCTION 1 |
| 4 | + |
| 5 | +#define INSTANTLY_OUT_OF_SCOPE 1 |
| 6 | + |
| 7 | +void ofApp::setup(){ |
| 8 | + for (size_t i = 0; i < testers_.size(); i++) { |
| 9 | + testers_[i] = std::make_shared<Tester>(); |
| 10 | + } |
| 11 | + ofSetVerticalSync(false); |
| 12 | + |
| 13 | +#ifdef INSTANTLY_OUT_OF_SCOPE |
| 14 | + ofLogNotice("will hang"); |
| 15 | + ofxOscReceiver r; |
| 16 | +#endif |
| 17 | + ofLogNotice("ready to roll"); |
| 18 | + |
| 19 | +} |
| 20 | + |
| 21 | +void ofApp::update(){ |
| 22 | + |
| 23 | + if (mode_ == STOP) { |
| 24 | + if (ofGetFrameNum()%6==0) { |
| 25 | + for (const auto & tester: testers_) { |
| 26 | + tester->receiver_->stop(); |
| 27 | + } |
| 28 | + } if (ofGetFrameNum()%6==3) { |
| 29 | + for (const auto & tester: testers_) { |
| 30 | + tester->receiver_->start(); |
| 31 | + } |
| 32 | + } |
| 33 | + } else if (mode_ == DYNA) { |
| 34 | + if (ofGetFrameNum()%3==0) { |
| 35 | + for (const auto & tester: testers_) { |
| 36 | + tester->receiver_ = std::make_shared<ofxOscReceiver>(port_); |
| 37 | + if (tester->receiver_->isListening()) { |
| 38 | + tester->sender_ = std::make_shared<ofxOscSender>("127.0.0.1", port_); |
| 39 | + receivers_++; |
| 40 | + } |
| 41 | + if (port_++ > high_port_) port_ = low_port_; |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + for (const auto & tester: testers_) { |
| 47 | + for (size_t i=0; i<10; i++) { |
| 48 | +#if defined(USE_EXPLICIT_FUNCTION) |
| 49 | + ofxOscMessage m; |
| 50 | + m.setAddress("/test"); |
| 51 | + m.addIntArg(std::int32_t(i)); |
| 52 | + m.addIntArg(port_); |
| 53 | + m.addIntArg(i); |
| 54 | + tester->sender_->sendMessage(m); |
| 55 | +#else |
| 56 | + ofxOscMessage m{"/test"}; |
| 57 | + m.add(i); |
| 58 | + tester->sender_->sendMessage(m.add(port_).add(i)); |
| 59 | +#endif |
| 60 | + msgs_out_++; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + for (const auto & tester: testers_) { |
| 65 | + while (auto m = tester->receiver_->getMessage()) { |
| 66 | + msgs_++; |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +void ofApp::draw() { |
| 72 | + for (int i = 0; i < 1000; i++) { |
| 73 | + ofSetColor(ofRandom(0,255), ofRandom(0, 255), ofRandom(0, 255)); |
| 74 | + ofDrawRectangle(i *15, 10, 10, 50); |
| 75 | + } |
| 76 | + |
| 77 | + ofSetColor(ofColor::white); |
| 78 | + ofDrawBitmapString("receivers destroyed/created: " + ofToString(receivers_), 10, 80); |
| 79 | + ofDrawBitmapString("sent: " + ofToString(msgs_out_), 10, 100); |
| 80 | + ofDrawBitmapString("received: " + ofToString(msgs_), 10, 120); |
| 81 | + auto miss = msgs_out_-msgs_; |
| 82 | + ofDrawBitmapString("= lost: " + ofToString(miss) + " (" + ofToString((float(miss)/(msgs_out_))*100.0f, 2)+"%)", 10, 140); |
| 83 | + |
| 84 | + ofDrawBitmapString("mode 1: dynamic reallocation every 3 frames (<5% loss)", 10, 200); |
| 85 | + ofDrawBitmapString("mode 2: open/close every 3 frames (normal to have 50% loss)", 10, 220); |
| 86 | + ofDrawBitmapString("mode 3: stable (should have 0% loss)", 10, 240); |
| 87 | + ofDrawBitmapString("current mode: "+ofToString(mode_+1), 10, 260); |
| 88 | + |
| 89 | + ofDrawBitmapString("FPS (unsync'ed to stress things): " + ofToString(ofGetFrameRate(), 2), 10, ofGetHeight()-30); |
| 90 | +} |
| 91 | + |
| 92 | +void ofApp::keyPressed(int key){ |
| 93 | + if (key=='1' ) mode_ = DYNA; |
| 94 | + if (key=='2' ) mode_ = STOP; |
| 95 | + if (key=='3' ) mode_ = STABLE; |
| 96 | + msgs_ = 0; |
| 97 | + msgs_out_ = 0; |
| 98 | +} |
| 99 | + |
0 commit comments