Skip to content

Instantly share code, notes, and snippets.

@stephanschulz
Created November 9, 2019 17:06
Show Gist options
  • Select an option

  • Save stephanschulz/ce11d7e6e5734f74688d969f9c9fe86f to your computer and use it in GitHub Desktop.

Select an option

Save stephanschulz/ce11d7e6e5734f74688d969f9c9fe86f to your computer and use it in GitHub Desktop.
#include "threadedPing.h"
threadedPing pingTest;
bool checkInternet(){
string serverIP = "www.google.com";
// string result = ofSystem("ping -c1 www.google.com");
// return result.find("received",0) != string::npos;
pingTest.serverIP = serverIP;
pingTest.start();
if(pingTest.bServerConnected == true){
cout<<"YES connect to timing server at IP "<<serverIP<<endl;
return true;
}else{
cout<<"NO could not connect to timing server at IP "<<serverIP<<endl;
return false;
}
}
#ifndef _THREADED_OBJECT
#define _THREADED_OBJECT
#include "ofMain.h"
class threadedPing : public ofThread{
public:
bool bServerConnected; // threaded fucntions that share data need to use lock (mutex)
// and unlock in order to write to that data
// otherwise it's possible to get crashes.
//
// also no opengl specific stuff will work in a thread...
// threads can't create textures, or draw stuff on the screen
// since opengl is single thread safe
string serverIP;
threadedPing(){
bServerConnected = false;
}
void start(){
startThread(); // blocking, verbose
}
void stop(){
stopThread();
}
//--------------------------
void threadedFunction(){
while(isThreadRunning()){
if( lock() ){
string pingStr = (string)"ping -c 1 -t 1 " + serverIP ;
// int flag = system(pingStr.c_str()); //look at coco40 code
string result = ofSystem(pingStr.c_str());
cout<<"threaded ping result "<<result;
bool flag = result.find("received",0) != string::npos;
if(flag == 0){
// bServerConnected = true;
bServerConnected = false;
cout<<"cloud not connect to timing server at IP "<<serverIP<<endl;
}else{
// bServerConnected = false;
bServerConnected = true;
// cout<<ofGetTimestampString()<<endl;
// cout<<"cloud not connect to timing server at IP "<<serverIP<<endl;
}
unlock();
stop();
}
}
}
//--------------------------
void draw(){
}
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment