Ejemplo n.º 1
0
char* getCurrentTime( )
{
  time_t now;
  struct tm cNow;
  static char timeStr[100];

  time( &now );
  localtime_r( &now, &cNow );

  sprintf( timeStr, "%02d:%02d:%02d %s",
    cNow.tm_hour,
    cNow.tm_min,
    cNow.tm_sec,
    getUpTime( ));

  return timeStr;
}
Ejemplo n.º 2
0
// Main
int main(int argc, char *argv[])
{
    /////////////////////////////////////////////
    //Definition of structures and variables.
    int sock, length, n;
    socklen_t fromlen;
    struct sockaddr_in server; 
    struct sockaddr_in from;
    char buf[BUFFSIZE+1];

   if (argc < 2) {
      fprintf(stderr, "ERROR: No port provided\n");
      exit(0);
   }
   
   /////////////////////////////////////////////
   //Create socket with protocol UDP
   sock=socket(AF_INET, SOCK_DGRAM, 0);
   if (sock < 0)
   {
        printf("Error: Opening socket.\n");
        exit(1);
   }
   
   ////////////////////////////////////////////
   //Theese lines initializes server to zeros.
   length = sizeof(server);
   bzero(&server,length);
   
   ////////////////////////////////////////////
   //Use INTERNET domain (internet sockets).
   server.sin_family=AF_INET;
   //As server, use own IP(From all interfaces)
   server.sin_addr.s_addr=INADDR_ANY;
   //Convert port number to network byte order and save it to structure.
   server.sin_port=htons(atoi(argv[1]));
   
   ///////////////////////////////////////////////
   //The bind() system calls binds a socket to an address (address of the current host and port number).
   if (bind(sock,(struct sockaddr *)&server,length)<0) 
    {
		printf("Error: Binding.\n");
        exit(1);
    }
       
   fromlen = sizeof(struct sockaddr_in);
   
   printf("Server started with port %s\n", argv[1]);
   
   //Continuesly run and accept commands.
   while (1) {
        memset(&buf, 0, sizeof(buf));
        
        //Recieve data from client
		puts("Waiting for client...\n");
        n = recvfrom(sock,buf,BUFFSIZE,0,(struct sockaddr *)&from,&fromlen);
        if (n < 0)
		{
		printf("Error: Recieving.\n");
		exit(1);
		}
		
       //Write what was recieved.
       write(1,"Received a datagram: ",21);
       write(1,buf,n); 
       
       char* tmpBuf;
       // Send data back if ASCII 'u' or 'U'
       if(*buf == 'u' || *buf == 'U')
       {
            memset(&buf, 0, sizeof(buf));
            tmpBuf = getUpTime();
            
            strcpy(buf, tmpBuf);
            
            n = sendto(sock,buf,strlen(buf) + 1,0,(struct sockaddr *)&from,fromlen); // Add +1 because of 0-termination
            if (n < 0)
			{
				printf("Error: Sending.\n");
				exit(1);
			}
                    
            free(tmpBuf); 
        }
        // Send data back if ASCII 'L' or 'l'
        else if(*buf == 'l' || *buf == 'L')
        {
            memset(&buf, 0, sizeof(buf));
            tmpBuf = getLoadAvg();
            
            strcpy(buf, tmpBuf);
            
            n = sendto(sock,buf,strlen(buf) + 1, 0,(struct sockaddr *)&from,fromlen);
			if (n < 0)
			{
				printf("Error: Sending.\n");
				exit(1);
			}
            
            free(tmpBuf); 
        }
        else
        {
			strcpy(buf, "Screw you\n");
			sendto(sock,buf,strlen(buf) + 1, 0,(struct sockaddr *)&from,fromlen);
		}

   }
   return 0;
 }
Ejemplo n.º 3
0
void broadCastKeyPressed(char well){
	NextRe = well;
	timeStamp = getUpTime();
}
QString ConnectionThread::processLine(QTcpSocket & sock,
                                      const QString & line)
{
    Debug() << "Got Line: " << line;
    QStringList toks = line.split(QRegExp("\\s+"), QString::SkipEmptyParts);
    if (toks.size() < 1) return QString::null;
    QString cmd = toks.front();
    toks.pop_front();
    cmd = cmd.toUpper();
    if (cmd == "NOOP") {
        return ""; // noop command alwasy prints OK -- used to test server connectivity and for keepaliveage, etc
    } else if (cmd == "GETTIME") {
        return QString::number(getTime(), 'f', 9);
    } else if (cmd == "GETFRAMENUM") {
        StimPlugin *p = stimApp()->glWin()->runningPlugin();
        if (p) {
            return QString::number(p->getFrameNum());
        } else {
            Error() << "GETFRAMENUM command received but no plugin was running.";
        }
    } else if (cmd == "GETHWFRAMENUM") {
        GetHWFrameCountEvent *e = new GetHWFrameCountEvent();
        GetSetData *d = e->d;
        stimApp()->postEvent(this, e);
        unsigned hwfc = d->getReply<unsigned>();
        delete d;
        return QString::number(hwfc);
    } else if (cmd == "GETREFRESHRATE") {
        unsigned rate = stimApp()->refreshRate();
        return QString::number(rate);
    } else if (cmd == "GETCURRENTRSEED") {
        int s = stimApp()->glWin() 
			    ?  (stimApp()->glWin()->runningPlugin() 
				    ? stimApp()->glWin()->runningPlugin()->currentRSeed() 
				    : 0)
				: 0;
        return QString::number(s);
    } else if (cmd == "GETWIDTH") {
        return QString::number(stimApp()->glWin()->width());
    } else if (cmd == "GETHEIGHT") {
        return QString::number(stimApp()->glWin()->height());
    } else if (cmd == "GETFRAME" && toks.size()) {
        bool ok;
        unsigned framenum = toks[0].toUInt(&ok), numFrames = 1;
		Vec2i co, cs, ds; // params 3,4,5,6,7,8 are crop-origin-x, crop-origin-y, crop-size-width, crop-size-height, downsample-factor-x, downsample-factor-y
        toks.pop_front();
		if (toks.size()) {
			bool ok2;
			numFrames = toks.front().toUInt(&ok2);
			if (ok2) toks.pop_front();
			if (!ok2 || numFrames < 1) numFrames = 1;
			Vec2i *vp[] = { &co, &cs, &ds, 0 };
			for (Vec2i **vcur = vp; *vcur; ++vcur) {				
				Vec2i & v = **vcur;
				v.x = toks.size() ? toks.front().toUInt(&ok2) : 0;
				if (ok2) toks.pop_front();
				if (!ok2 || v.x < 0) v.x = 0;
				v.y = toks.size() ? toks.front().toUInt(&ok2) : 0;
				if (ok2) toks.pop_front();
				if (!ok2 || v.y < 0) v.y = 0;
			}
		}
		if (!ds.x) ds.x = 1;
		if (!ds.y) ds.y = 1;
        int datatype = GL_UNSIGNED_BYTE;
        if (toks.size()) {
            QString s = toks.join(" ").toUpper().trimmed();
            if (s == "BYTE") datatype = GL_BYTE;
            else if (s == "UNSIGNED BYTE") datatype = GL_UNSIGNED_BYTE;
            else if (s == "SHORT") datatype = GL_SHORT;
            else if (s == "UNSIGNED SHORT") datatype = GL_UNSIGNED_SHORT;
            else if (s == "INT") datatype = GL_INT;
            else if (s == "UNSIGNED INT") datatype = GL_UNSIGNED_INT;
            else if (s == "FLOAT") datatype = GL_FLOAT;
            else {
                Error() << "GETFRAME command invalid datatype `" << s << "'.";
                return QString::null;
            }
        }
        if (ok) {
            GetFrameEvent *e = new GetFrameEvent(framenum, numFrames, co, cs, ds, datatype);
            GetSetData *d = e->d;
            stimApp()->postEvent(this, e);
			const double tgen0 = getTime();
            QList<QByteArray> frames (d->getReply<QList<QByteArray> >());
            delete d;
            if (!frames.isEmpty()) {
				const unsigned long fbytes = frames.count()*frames.front().size();
				Debug() << "Generating " << frames.count() << " frames (" << fbytes << " bytes) took " << getTime()-tgen0 << " secs";
                sock.write((QString("BINARY DATA ") + QString::number(fbytes) + "\n").toUtf8());
				const double t0 = getTime();
				for (QList<QByteArray>::const_iterator it = frames.begin(); it != frames.end(); ++it)
					sock.write(*it);
				Debug() << "Sending " << numFrames << " frames (" << fbytes << " bytes) took " << getTime()-t0 << " secs";
                return "";
            }
        }
    } else if (cmd == "LIST") {
        QList<QString> lst = stimApp()->glWin()->plugins();
        QString ret;
        QTextStream ts(&ret, QIODevice::WriteOnly|QIODevice::Truncate);
        for(QList<QString>::const_iterator it = lst.begin(); it != lst.end(); ++it) {
            ts << (*it) << "\n";
        }
        ts.flush();
        return ret;
	} else if (cmd == "GETFRAMEVARS") { 
			QVector<double> data;
			int nrows, ncols;
			FrameVariables::readAllFromLast(data, &nrows, &ncols);
			sock.write(QString().sprintf("MATRIX %d %d\n", nrows, ncols).toUtf8());
			if (data.size()) sock.write(QByteArray::fromRawData(reinterpret_cast<char *>(&data[0]),data.size()*sizeof(double)));
			return "";
	} else if (cmd == "GETFRAMEVARNAMES") {
			QString ret = "";
			QTextStream ts(&ret);
			QStringList hdr = FrameVariables::readHeaderFromLast();
					
			int i = 0;
			for (QStringList::iterator it = hdr.begin(); it != hdr.end(); ++it, ++i) {
					ts << *it << "\n";
			}
			ts.flush();
			return ret;		
    } else if (cmd == "GETSTATS") {
        QString theStr("");
        QTextStream strm(&theStr, QIODevice::WriteOnly/*|QIODevice::Text*/);
        GetSetEvent *e = new GetHWFrameCountEvent();
        GetSetData *d = e->d;
        stimApp()->postEvent(this, e);
        unsigned hwfc = d->getReply<unsigned>();
        delete d;
        e = new IsConsoleHiddenEvent();
        d = e->d;
        stimApp()->postEvent(this, e);
        bool isConsoleHidden = d->getReply<bool>();
        delete d;
        StimPlugin *p = stimApp()->glWin()->runningPlugin();

        strm.setRealNumberPrecision(3);
        strm.setRealNumberNotation(QTextStream::FixedNotation);
        strm << "runningPlugin = " << (p ? p->name() : "") << "\n"
             << "isPaused = " << (stimApp()->glWin()->isPaused() ? "1" : "0") << "\n"
			 << "isInitialized = " << (p ? (p->isInitialized() ? 1 : 0) : 0) << "\n" 
             << "isConsoleWindowHidden = " << (isConsoleHidden ? "1" : "0") << "\n"
             << "statusBarString = " << (p ? p->getSBString() : "") << "\n"
             << "currentTime = " << QDateTime::currentDateTime().toString() << "\n"
             << "beginTime = " << (p ? p->getBeginTime().toString() : "") << "\n"
             << "width = " << stimApp()->glWin()->width() << "\n"
             << "height = " << stimApp()->glWin()->height() << "\n"
             << "fpsAvg = " << (p ? p->getFps() : 0.) << "\n"
             << "fpsMin = " << (p ? p->getFpsMin() : 0.) << "\n"
             << "fpsMax = " << (p ? p->getFpsMax() : 0.) << "\n"
             << "fpsLast = " << (p ? p->getFpsCur() : 0.) << "\n"
             << "frameNum = " << (p ? p->getFrameNum() : -1) << "\n"
             << "hardwareFrameCount = " << hwfc << "\n"
             << "haAccurateHWFrameCount = " << (hasAccurateHWFrameCount() ? "1" : "0") << "\n"
             << "calibraredRefreshRate = " << stimApp()->refreshRate() << "\n"
             << "hwRefreshRate = " << getHWRefreshRate() << "\n"
             << "hasAccurateHWRefreshRate = " << (hasAccurateHWRefreshRate() ? 1 : 0) << "\n"
             << "numMissedFrames = " << (p ? p->getNumMissedFrames() : 0) << "\n";
        QDateTime now(QDateTime::currentDateTime()), beginTime(p ? p->getBeginTime() : now);
        double secs = beginTime.secsTo(now), fskipsPerSec = 0.;
        if (p && secs > 0.) {
            fskipsPerSec = p->getNumMissedFrames() / secs;
        }
        strm << "missedFramesPerSec = " << fskipsPerSec << "\n"
             << "saveDirectory = " << stimApp()->outputDirectory() << "\n"
             << "pluginList = ";
        QList<QString> plugins = stimApp()->glWin()->plugins();
        for (QList<QString>::const_iterator it = plugins.begin(); it != plugins.end(); ++it) {
            if (it != plugins.begin()) strm << ", ";
            strm << *it;
        }
        strm << "\n"
             << "programUptime = " << getTime() << "\n"
             << "nProcessors = " << getNProcessors() << "\n"
             << "hostName = " << getHostName() << "\n"
             << "uptime = " << getUpTime() << "\n";
        
        strm.flush();
        return theStr;
    } else if (cmd == "GETPARAMS" && toks.size()) {
        QString pluginName = toks.join(" ");
        StimPlugin *p;
        if ( (p = stimApp()->glWin()->pluginFind(pluginName)) ) {
            return p->getParams().toString();
        }
    } else if (cmd == "GETPARAMHISTORY" && toks.size()) {
        QString pluginName = toks.join(" ");
        StimPlugin *p;
        if ( (p = stimApp()->glWin()->pluginFind(pluginName)) ) {
            return p->paramHistoryToString() + "\n";
        }
    } else if (cmd == "SETPARAMHISTORY" && toks.size()) {
        QString pluginName = toks.join(" ");
        StimPlugin *p = stimApp()->glWin()->pluginFind(pluginName);
		if (!p) {
			Error() << "SETPARAMHISTORY issued on a non-existant plugin";
		} else if (stimApp()->glWin()->runningPlugin() == p) {
			Error() << "SETPARAMHISTORY cannot be issued on a plugin that is running";
		} else {
            Debug() << "Sending: READY";
            sock.write("READY\n");
            QString paramstr ("");
            QTextStream paramts(&paramstr, QIODevice::WriteOnly/*|QIODevice::Text*/);
            QString line;
            while ( sock.canReadLine() || sock.waitForReadyRead() ) {
                line = sock.readLine().trimmed();
                if (!line.length()) break;
                Debug() << "Got Line: " << line;
                paramts << line << "\n";
            }
            paramts.flush();
			p->setPendingParamHistoryFromString(paramstr);
			p->setSaveParamHistoryOnStopOverride(true); // also tell plugin to save this param history, since it came from an external source
            return "";
		}
	} else if (cmd == "NUMPARAMSQUEUED" && toks.size()) {
        QString pluginName = toks.join(" ");
        StimPlugin *p = stimApp()->glWin()->pluginFind(pluginName);
		if (!p) {
			Error() << "NUMPARAMSQUEUED issued on a non-existant plugin";
		} else {
			return QString::number(p->pendingParamsHistorySize());
		}	
	} else if (cmd == "SETPARAMQUEUE") {
        QString pluginName = toks.join(" ");
        StimPlugin *p = stimApp()->glWin()->pluginFind(pluginName);
		if (!p) {
			Error() << "SETPARAMQUEUE issued on a non-existant plugin";
		} else if (stimApp()->glWin()->runningPlugin() == p) {
			Error() << "SETPARAMQUEUE cannot be issued on a plugin that is running";
		} else {
            Debug() << "Sending: READY";
            sock.write("READY\n");
            QString paramstr ("");
            QTextStream paramts(&paramstr, QIODevice::WriteOnly/*|QIODevice::Text*/);
            QString line;
            while ( sock.canReadLine() || sock.waitForReadyRead() ) {
                line = sock.readLine().trimmed();
                if (!line.length()) break;
                Debug() << "Got Line: " << line;
                paramts << line << "\n";
            }
            paramts.flush();
			if (p->enqueueParamsForPendingParamsHistoryFromString(paramstr)) {
				p->setSaveParamHistoryOnStopOverride(false); // also tell plugin to NOT save this param history, since presumably it can be generated again from calling client code..
				return "";
			} else {
				Error() << "SETPARAMQUEUE failed to enqueue params from specified param-queue";
			}
		}
    } else if (cmd == "SETPARAMS" && toks.size()) {
        QString pluginName = toks.join(" ");
        StimPlugin *p;
        if ( (p = stimApp()->glWin()->pluginFind(pluginName)) ) {
            Debug() << "Sending: READY";
            sock.write("READY\n");
            QString paramstr ("");
            QTextStream paramts(&paramstr, QIODevice::WriteOnly/*|QIODevice::Text*/);
            QString line;
            while ( sock.canReadLine() || sock.waitForReadyRead() ) {
                line = sock.readLine().trimmed();
                if (!line.length()) break;
                Debug() << "Got Line: " << line;
                paramts << line << "\n";
            }
            paramts.flush();
            p->setParams(paramstr, p == stimApp()->glWin()->runningPlugin());
            return "";
        } else if (!p) {
            Error() << "SETPARAMS issued on a non-existant plugin";
        }
    } else if (cmd == "RUNNING") {
        StimPlugin *p = stimApp()->glWin()->runningPlugin();
        if (p) {
            return p->name();
        }
        return "";
    } else if (cmd == "ISPAUSED") {
        return QString::number(stimApp()->glWin()->isPaused());
    } else if (cmd == "ISINITIALIZED") {
        StimPlugin *p = stimApp()->glWin()->runningPlugin();
        if (p) {
			return QString::number(p->isInitialized());
        }
        return "0";
    } else if (cmd == "PAUSE") {
        if (!stimApp()->glWin()->isPaused())
            stimApp()->glWin()->pauseUnpause();
        return "";
    } else if (cmd == "UNPAUSE") {
        if (stimApp()->glWin()->isPaused())
            stimApp()->glWin()->pauseUnpause();
        return "";
    } else if (cmd == "ISCONSOLEHIDDEN") {
        GetSetEvent *e = new IsConsoleHiddenEvent;
        GetSetData *d = e->d;
        stimApp()->postEvent(this, e);
        QString ret = QString::number(int(d->getReply<bool>()));
        delete d;
        return ret;
    } else if (cmd == "CONSOLEHIDE") {
        stimApp()->postEvent(this, new ConsoleHideEvent());
        return "";
    } else if (cmd == "CONSOLEUNHIDE") {
        stimApp()->postEvent(this, new ConsoleUnHideEvent());
        return "";
    } else if (cmd == "ISVSYNCDISABLED") {
        return QString::number(stimApp()->isVSyncDisabled() ? 1 : 0);
    } else if (cmd == "SETVSYNCDISABLED") {
		const bool disabled = toks.join("").toInt();
		stimApp()->postEvent(this, new SetVSyncDisabledEvent(disabled));
		return "";
    } else if (cmd == "START" && toks.size()) {
        // this commands needs to be executed in the main thread
        // to avoid race conditions and also to have a valid opengl context
        QString pluginName = toks.front();
        bool startUnpaused = toks.size() > 1 && toks[1].toInt();
        if ( (stimApp()->glWin()->pluginFind(pluginName)) ) {
            stimApp()->postEvent(this, new StartStopPluginEvent(pluginName, startUnpaused));
            return "";
        }
    } else if (cmd == "STOP") {
        // this commands needs to be executed in the main thread
        // to avoid race conditions and also to have a valid opengl context
        bool doSave = toks.join("").toInt();
        if (stimApp()->glWin()->runningPlugin())
            stimApp()->postEvent(this, new StartStopPluginEvent(doSave)); 
        return "";
    } else if (cmd == "GETSAVEDIR") {
        return stimApp()->outputDirectory();
    } else if (cmd == "SETSAVEDIR") {
        QString dir = toks.join(" ");
        return stimApp()->setOutputDirectory(dir) ? QString("") : QString::null;
    } else if (cmd == "GETVERSION") {
        return VERSION_STR;
    } else if (cmd == "BYE") {
        sock.close();
    } 
    // add more cmds here
    return QString::null;
}
Ejemplo n.º 5
0
char getch(){
	while(timeStamp < getUpTime()){
	}
	return NextRe;
}