Advertisement
synthnassizer

customUdpReceiver

Nov 29th, 2014
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.48 KB | None | 0 0
  1. //receiver ofApp.h
  2. #pragma once
  3.  
  4. #include "ofMain.h"
  5. #include "ofxNetwork.h"
  6.  
  7. class ofApp : public ofBaseApp{
  8.  
  9.     public:
  10.  
  11.         struct edges_t
  12.         {
  13.             ofPoint leftMost;
  14.             ofPoint rightMost;
  15.             ofPoint topMost;
  16.             ofPoint bottomLeftMost;
  17.             ofPoint bottomRightMost;
  18.         };
  19.         struct blob_t
  20.         {
  21.             int id;
  22.             ofPoint centroid;
  23.             edges_t edges;
  24.             ofPolyline blobOutline;
  25.         };
  26.         struct mark_t
  27.         {
  28.             int id;
  29.             ofPoint centroid;
  30.         };
  31.  
  32.         void setup();
  33.         void update();
  34.         void draw();
  35.  
  36.         void keyPressed(int key);
  37.         void keyReleased(int key);
  38.         void mouseMoved(int x, int y );
  39.         void mouseDragged(int x, int y, int button);
  40.         void mousePressed(int x, int y, int button);
  41.         void mouseReleased(int x, int y, int button);
  42.         void windowResized(int w, int h);
  43.         void dragEvent(ofDragInfo dragInfo);
  44.         void gotMessage(ofMessage msg);
  45.  
  46.         ofxUDPManager udpConnection;
  47.  
  48.         int extractBlobParts(const vector<string>& vecOfPartStrings, blob_t& aBlob);
  49.         int extractMarkParts(const vector<string>& vecOfPartStrings, mark_t& aMark);
  50.         int extractPoints(const string& pointsString, vector<ofPoint>& pts);
  51.         int extractPoint(const string pointString , ofPoint& aPt);
  52.  
  53.         blob_t bl;
  54.         mark_t mk;
  55.         int msgLen;
  56.  
  57.  
  58. };
  59.  
  60. //receiver ofApp.cpp
  61. #include "ofApp.h"
  62.  
  63. #define CONTENT_SEPARATOR   "[/b]"
  64.  
  65. //--------------------------------------------------------------
  66. void ofApp::setup(){
  67.     //we run at 60 fps!
  68.     ofSetVerticalSync(true);
  69.     ofSetFrameRate(60);
  70.     ofSetLogLevel(OF_LOG_NOTICE);
  71.  
  72.     msgLen = 0 ;
  73.  
  74.     //create the socket and bind to port 11999
  75.     udpConnection.Create();
  76.     udpConnection.Bind(11999);
  77.     udpConnection.SetNonBlocking(true);
  78.  
  79.     //ofSetBackgroundAuto(false);
  80.     ofBackground(255, 255, 255);
  81. }
  82.  
  83. //--------------------------------------------------------------
  84. void ofApp::update(){
  85.  
  86.     char udpMessage[1000000];
  87.     int receivedBytesNum = udpConnection.Receive(udpMessage,1000000);
  88.     if (receivedBytesNum >= 1000000)
  89.         ofLog(OF_LOG_NOTICE, "maxed out udp buffer %d!",receivedBytesNum);
  90.     if (receivedBytesNum == SOCKET_TIMEOUT)
  91.         ofLog(OF_LOG_ERROR, "socket_timeout");
  92.  
  93.     string message=udpMessage;
  94.  
  95.     if(message!="")
  96.     {
  97.         msgLen = message.length();
  98.         //cout << "msgLen=" << msgLen << " receivedBytes=" << receivedBytesNum << endl;
  99.  
  100.         //I may receive many messages in one read. splitting them...
  101.         vector<string> msgs = ofSplitString(message,"[/EOM]");
  102.         if (msgs.size()-1 > 1)
  103.             ofLog(OF_LOG_NOTICE, "received %d messages!",msgs.size()-1);
  104.  
  105.         //the -1 just above and just below are used because of the way ofSplitString works
  106.         //it creates vector entries for both sides of a "separator" even if there are no
  107.         //chars after the separator (ie if it is a terminator)
  108.         for (unsigned int i = 0 ; i < msgs.size()-1 ; i++ )
  109.         {
  110.             cout << "-------------------pass[" << i << "]---------------------" << endl;
  111.             vector<string> msgParts = ofSplitString(msgs[i],CONTENT_SEPARATOR);
  112.             if ( msgParts[0] == "blob1" )
  113.             {
  114.                 blob_t aBlob;
  115.                 if ( extractBlobParts(msgParts, aBlob) >= 0 )
  116.                 {
  117.                     bl = aBlob ;
  118.                     cout << "bl.id[" << i << "]=" << bl.id << endl;
  119.                 }
  120.                 else
  121.                 {
  122.                     ofLog(OF_LOG_ERROR, "blob extraction error. did not alter values %d!",i);
  123.                 }
  124.             }
  125.             else if ( msgParts[0] == "mark1" )
  126.             {
  127.                 mark_t aMark;
  128.                 if ( extractMarkParts(msgParts, aMark) >= 0 )
  129.                 {
  130.                     mk = aMark;
  131.                     cout << "mk.id[" << i << "]=" << mk.id << endl;
  132.                 }
  133.                 else
  134.                 {
  135.                     ofLog(OF_LOG_ERROR, "mark extraction error. did not alter values %d!",i);
  136.                 }
  137.             }
  138.             else
  139.             {
  140.                 ofLog(OF_LOG_ERROR, "shouldn't be here");
  141.             }
  142.         }//end for
  143.     } //end if not empty message
  144. }
  145.  
  146. //--------------------------------------------------------------
  147. void ofApp::draw(){
  148.     ofFill();
  149.     ofSetHexColor(0xFFFFFF);
  150.     ofRect(0,0,200,30);
  151.     ofSetHexColor(0x101010);
  152.     ofDrawBitmapString("UDP Receiver Example ", 10, 20);
  153.  
  154.     stringstream ss;
  155.     ss << "msgLen " << msgLen << "\n";
  156.     ss << "blob id " << bl.id << "\n";
  157.     ss << "blob centroid " << bl.centroid << "\n";
  158.     ss << "leftmost " << bl.edges.leftMost << "\n";
  159.     ss << "rightMost " << bl.edges.rightMost << "\n";
  160.     ss << "topMost " << bl.edges.topMost << "\n";
  161.     ss << "bottomLeftMost " << bl.edges.bottomLeftMost << "\n";
  162.     ss << "bottomRightMost " << bl.edges.bottomRightMost << "\n";
  163.     ss << "outline size = " << bl.blobOutline.size() << "\n";
  164.     for (unsigned int i = 0 ; i < bl.blobOutline.size() ; i++ )
  165.         ss << bl.blobOutline[i] ;
  166.     ss << "\n";
  167.  
  168.     ss << "mark id " << mk.id << "\n";
  169.     ss << "mark centroid " << mk.centroid << "\n";
  170.     ofDrawBitmapString(ss.str(), 10, 40);
  171. }
  172.  
  173. //--------------------------------------------------------------
  174. void ofApp::keyPressed(int key){
  175.  
  176. }
  177.  
  178. //--------------------------------------------------------------
  179. void ofApp::keyReleased(int key){
  180.  
  181. }
  182.  
  183. //--------------------------------------------------------------
  184. void ofApp::mouseMoved(int x, int y ){
  185. }
  186.  
  187. //--------------------------------------------------------------
  188. void ofApp::mouseDragged(int x, int y, int button){
  189.  
  190. }
  191.  
  192. //--------------------------------------------------------------
  193. void ofApp::mousePressed(int x, int y, int button){
  194.  
  195. }
  196.  
  197. //--------------------------------------------------------------
  198. void ofApp::mouseReleased(int x, int y, int button){
  199.  
  200. }
  201.  
  202. //--------------------------------------------------------------
  203. void ofApp::windowResized(int w, int h){
  204.  
  205. }
  206.  
  207. //--------------------------------------------------------------
  208. void ofApp::gotMessage(ofMessage msg){
  209.  
  210. }
  211.  
  212. //--------------------------------------------------------------
  213. void ofApp::dragEvent(ofDragInfo dragInfo){
  214.  
  215. }
  216.  
  217. //==============================================================
  218.  
  219.  
  220. int ofApp::extractBlobParts(const vector<string>& vecOfPartStrings, blob_t& aBlob)
  221. {
  222.     int returnValue = 0 ;
  223.     if (vecOfPartStrings.size() < 4)
  224.     {
  225.         ofLog(OF_LOG_ERROR, "less than 4 parts in blob msg!");
  226.         returnValue = -1;
  227.     }
  228.     else if (vecOfPartStrings.size() >= 4)
  229.     {
  230.         if (vecOfPartStrings.size() > 4)
  231.             ofLog(OF_LOG_WARNING, "more than 4 parts in blob msg!");
  232.  
  233.         aBlob.id = atoi(vecOfPartStrings[1].c_str());
  234.  
  235.         //the 10 * is just to make clear which of the 2 return values that follow
  236.         //cuased the
  237.         vector<ofPoint> featuresPts ;
  238.         returnValue = 10 * extractPoints(vecOfPartStrings[2], featuresPts);
  239.  
  240.         vector<ofPoint> polylinePts ;
  241.         returnValue += extractPoints(vecOfPartStrings[3], polylinePts);
  242.         if ( returnValue < 0 )
  243.         {
  244.  
  245.         }
  246.         else
  247.         {
  248.             if (featuresPts.size() > 6 )
  249.             {
  250.                 ofLog(OF_LOG_WARNING, "edges points vector has more than 6 points!");
  251.                 cout << vecOfPartStrings[2] << "!" << endl;
  252.                 for (unsigned int i = 0 ; i < featuresPts.size() ; i++ )
  253.                     cout << featuresPts[i] <<endl;
  254.             }
  255.  
  256.             aBlob.centroid = featuresPts[0];
  257.             aBlob.edges.leftMost = featuresPts[1];
  258.             aBlob.edges.rightMost = featuresPts[2];
  259.             aBlob.edges.topMost = featuresPts[3];
  260.             aBlob.edges.bottomLeftMost = featuresPts[4];
  261.             aBlob.edges.bottomRightMost = featuresPts[5];
  262.  
  263.             if (aBlob.blobOutline.size() > 0)
  264.                 aBlob.blobOutline.clear();
  265.             aBlob.blobOutline.addVertices( polylinePts );
  266.         }
  267.     }
  268.  
  269.     return returnValue;
  270. }
  271.  
  272. int ofApp::extractMarkParts(const vector<string>& vecOfPartStrings, mark_t& aMark)
  273. {
  274.     int returnValue = 0 ;
  275.  
  276.     if (vecOfPartStrings.size() < 3)
  277.     {
  278.         ofLog(OF_LOG_ERROR, "less than 3 parts in mark msg!");
  279.         returnValue = -1;
  280.     }
  281.     else if (vecOfPartStrings.size() >= 3)
  282.     {
  283.         if (vecOfPartStrings.size() > 3)
  284.             ofLog(OF_LOG_WARNING, "more than 3 parts in mark msg!");
  285.  
  286.         aMark.id = atoi(vecOfPartStrings[1].c_str());
  287.  
  288.         vector<ofPoint> markPts ;
  289.         returnValue = extractPoints(vecOfPartStrings[2] , markPts);
  290.         if ( returnValue < 0 )
  291.         {
  292.  
  293.         }
  294.         else
  295.         {
  296.             if ( markPts.size() >= 1 )
  297.             {
  298.                 if ( markPts.size() > 1 )
  299.                     ofLog(OF_LOG_WARNING, "mark points vector has more than 1 points!");
  300.  
  301.                 aMark.centroid = markPts[0];
  302.             }
  303.             else
  304.             {
  305.  
  306.             }
  307.         }
  308.     }
  309.  
  310.     return returnValue;
  311. }
  312.  
  313. int ofApp::extractPoints(const string& pointsString, vector<ofPoint>& pts)
  314. {
  315.     vector<string> points = ofSplitString(pointsString,"[/p]");
  316.  
  317.     //the -1 is here because the last point has a "[/p]" terminator as all the previous points
  318.     //this leads ofSplitString() to create an extra entry in the vector that has 0,0,0 coordinates.
  319.     //it can be safely ignored
  320.     for (unsigned int i = 0 ; i < points.size()-1 ; i++ )
  321.     {
  322.         ofPoint pt ;
  323.         if ( extractPoint(points[i],pt) > 0 )
  324.         {
  325.             pts.push_back( pt );
  326.         }
  327.         else
  328.         {
  329.             break;
  330.         }
  331.     }
  332.  
  333.     int vecPointsSize = pts.size() - ( points.size()-1 );
  334.     return vecPointsSize;
  335. }
  336.  
  337. int ofApp::extractPoint(const string pointString , ofPoint& aPt)
  338. {
  339.     vector<string> coordinate = ofSplitString(pointString,"|");
  340.     int pointSize = coordinate.size();
  341.  
  342.     if (coordinate.size() >= 3 )
  343.     {
  344.         if (coordinate.size() > 3 )
  345.         {
  346.             ofLog(OF_LOG_WARNING, "point string has more than 3 points!");
  347.             cout << pointString << "!" << endl;
  348.         }
  349.  
  350.         aPt.x = atof(coordinate[0].c_str());
  351.         aPt.y = atof(coordinate[1].c_str());
  352.         aPt.z = atof(coordinate[2].c_str());
  353.     }
  354.     else if (coordinate.size() < 3 )
  355.     {
  356.         ofLog(OF_LOG_ERROR, "point string has less than 3 points!");
  357.         aPt = ofPoint(-1,-1,-1) ;
  358.         pointSize = coordinate.size() - 3 ;
  359.     }
  360.  
  361.     return pointSize;
  362. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement