Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / editors / sc-ide / core / sc_server.cpp
bloba3b41203dc96b740577df00c4bb5266b30452f86
1 /*
2 SuperCollider Qt IDE
3 Copyright (c) 2012 Jakob Leben & Tim Blechmann
4 http://www.audiosynth.com
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <QDebug>
23 #include "sc_server.hpp"
24 #include "sc_process.hpp"
25 #include "main.hpp"
27 #include "scsynthsend.h"
28 #include "sc_msg_iter.h"
30 namespace ScIDE {
32 ScServer::ScServer(QObject * parent):
33 QObject(parent), mPort(0)
35 mUdpSocket = new QUdpSocket(this);
36 for (int port = 57140; port != 57150; ++port) {
37 bool success = mUdpSocket->bind(port);
38 if (success)
39 break;
41 startTimer(333);
44 void ScServer::boot()
46 if (isRunning())
47 return;
49 Main::scProcess()->evaluateCode( "ScIDE.defaultServer.boot", true );
52 void ScServer::quit()
54 if (!isRunning())
55 return;
57 Main::scProcess()->evaluateCode( "ScIDE.defaultServer.quit", true );
60 void ScServer::reboot()
62 Main::scProcess()->evaluateCode( "ScIDE.defaultServer.reboot", true );
65 void ScServer::queryAllNodes(bool dumpControls)
67 QString arg = dumpControls ? QString("true") : QString("false");
69 Main::scProcess()->evaluateCode( QString("ScIDE.defaultServer.queryAllNodes(%1)").arg(arg), true );
72 void ScServer::timerEvent(QTimerEvent * event)
74 if (mUdpSocket->hasPendingDatagrams()) {
75 size_t datagramSize = mUdpSocket->pendingDatagramSize();
76 QByteArray array(datagramSize, 0);
77 mUdpSocket->readDatagram(array.data(), datagramSize);
79 if (!mPort)
80 return;
82 if (array[0]) {
83 char *addr = array.data();
84 const char * data = OSCstrskip(array.data());
85 int size = datagramSize - (data - addr);
87 if (strcmp(addr, "/status.reply") == 0) {
88 sc_msg_iter reply(size, data);
89 int unused = reply.geti();
90 int ugenCount = reply.geti();
91 int synthCount = reply.geti();
92 int groupCount = reply.geti();
93 int defCount = reply.geti();
94 float avgCPU = reply.getf();
95 float peakCPU = reply.getf();
96 double srNom = reply.getd();
97 double srAct = reply.getd();
99 emit updateServerStatus(ugenCount, synthCount, groupCount, defCount, avgCPU, peakCPU);
104 if (mPort) {
105 small_scpacket packet;
106 packet.BeginMsg();
107 packet.adds_slpre("status");
108 packet.maketags(1);
109 packet.addtag(',');
110 packet.EndMsg();
112 mUdpSocket->writeDatagram(packet.data(), packet.size(), mServerAddress, mPort);