-
Notifications
You must be signed in to change notification settings - Fork 0
/
nonOSCnat.py
executable file
·318 lines (238 loc) · 10.7 KB
/
nonOSCnat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Based on pynsmclient by Nils Gey.
The goal : list all NSM clients and route externals OSC messages to it.
... good luck to myself
"""
import sys
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtCore import qDebug, QObject, QThread
import logging
import time
from imports import nsmclient
from imports.pyNonPeer import OurNonClient
from imports.sighandler import SignalWakeupHandler
appTitle = "Non-OSC-NAT"
#QT thead worker
class NsmClientWorker(QObject):
"""Worker for NsmClient, maybe with his own eventloop"""
### NSM signals ###
nsmReadySign = QtCore.pyqtSignal(int)
ourNsmClientSign= QtCore.pyqtSignal(object)
openSessSign = QtCore.pyqtSignal(str, str)
saveSessSign = QtCore.pyqtSignal(str, str)
exitAppSign = QtCore.pyqtSignal()
### NON signals ###
updateListSign = QtCore.pyqtSignal(str, object)
def __init__(self):
QObject.__init__(self)
qDebug("Start Init")
# if thread is None:
# thread = QThread()
# self.moveToThread(thread)
""" NSM stuffs """
#All capabilities default to False. Just change one value to True if you program can do that.
capabilities = {
"switch" : False, #client is capable of responding to multiple `open` messages without restarting
"dirty" : False, #client knows when it has unsaved changes
"progress" : False, #client can send progress updates during time-consuming operations
"message" : False, #client can send textual status updates
"optional-gui" : False, #client has an optional GUI
}
requiredFunctions = {
"function_open" : self.openOrNewCallback, #Accept two parameters. Return two values. A bool and a status string. Otherwise you'll get a message that does not help at all: "Exception TypeError: "'NoneType' object is not iterable" in 'liblo._callback' ignored"
"function_save" : self.saveCallback, #Accept one parameter. Return two values. A bool and a status string. Otherwise you'll get a message that does not help at all: "Exception TypeError: "'NoneType' object is not iterable" in 'liblo._callback' ignored"
}
optionalFunctions = {
"function_quit" : self.exitCallback, #Accept zero parameters. Return True or False
"function_showGui" : None, #Accept zero parameters. Return True or False
"function_hideGui" : None, #Accept zero parameters. Return True or False
"function_sessionIsLoaded" : None, #No return value needed.
}
"""
Non-stuffs stuffs
callbacks should be a dict with the form :
dict{
addpeer: addPeerCallback, # retrun the added peer object.
delpeer: delPeerCallback,
addsignal: addSignalCallback,
delsignal: delSignalCallback
}
"""
## Displaced in OurNSMClient
#self.ourNonClient = OurNonClient(self.ourNsmClient, self.nonCallbacks)
self.ourNsmClient, self.nsmProcess = nsmclient.init(prettyName = appTitle,
capabilities = capabilities,
requiredFunctions = requiredFunctions,
optionalFunctions = optionalFunctions,
sleepValueMs = 0
)
self.ourNsmClientSign.emit(self.ourNsmClient)
nonCallbacks = {
"addpeer" : self.addpeerCallback,
"delpeer" : self.delpeerCallback
}
nonPeersParms = {
"baseport" : 16000, # Start port for externalized client. 16000 by default
}
self.ourNonPeer = OurNonClient(self.ourNsmClient, nonCallbacks, nonPeersParms)
qDebug("Init Finished")
### NSM callbacks ###
def saveCallback(self,pathBeginning):
qDebug("Got a SaveCallback")
returnStr = "\n Not realy implemented \n" + pathBeginning
return False, returnStr
def openOrNewCallback(self,pathBeginning, clientId):
qDebug("Got a OpenOrNewCallback")
self.openSessSign.emit(pathBeginning, clientId)
self.ourNonPeer.loadSession()
return True, " Not realy implemented"
def exitCallback(self):
qDebug("Got a ExitCallback")
#self.quit()
exit()
return True
### Non Callbacks ###
def addpeerCallback(self, peer):
self.updateListSign.emit("add", peer)
def delpeerCallback(self, peerId):
self.updateListSign.emit("del", peerId)
def run(self):
qDebug("Enter in event loop")
self.eventLoop = QtCore.QTimer()
self.eventLoop.start(10) #10ms-20ms is smooth for "real time" feeling. 100ms is still ok.
self.eventLoop.timeout.connect(self.rolling)
def rolling(self):
#qDebug("Looped !")
self.nsmProcess()
self.ourNonPeer.process()
self.ourNsmClientSign.emit(self.ourNsmClient)
### GUI class
class Gui(QtWidgets.QMainWindow):
def __init__(self, qtApp):
super().__init__()
self.qtApp = qtApp
self.setGeometry(300,300,350,250)
# Define some attributs
self.timers = {}
# Define all GUI elements
self.mainLayout = QtWidgets.QVBoxLayout()
self.wraperBox = QtWidgets.QGroupBox()
self.peerSelect = QtWidgets.QVBoxLayout()
self.peersList = QtWidgets.QHBoxLayout()
self.peersList.setAlignment(QtCore.Qt.AlignTop | QtCore.Qt.AlignLeft)
self.peerPort = QtWidgets.QSpinBox()
self.peerPort.setMinimum(1025)
self.peerPort.setMaximum(65000)
self.peerPort.setValue(16000)
self.peerPortApply = QtWidgets.QPushButton("Apply")
self.peerPortForm = QtWidgets.QVBoxLayout()
self.listHead = QtWidgets.QLabel("NON Clients :")
self.listSelPeer = QtWidgets.QListWidget()
#self.infoAppId = QtWidgets.QLabel
#self.spacer = QtWidgets.QSpacerItem()
self.status = QtWidgets.QMainWindow.statusBar(self)
self.status.showMessage("Don't read that !'")
# Define QT's main widget
self.mainWidget = QtWidgets.QWidget(self)
self.mainWidget.setLayout(self.mainLayout)
self.setCentralWidget(self.mainWidget)
# Connect gui widgets to functions
self.listSelPeer.currentItemChanged.connect(self.selPeerItem)
self.peerPortApply.clicked.connect(self.applyPort)
def showPeersSel(self):
# Define elements places
#self.setLayout(self.wraper)
self.mainLayout.addWidget(self.wraperBox)
self.wraperBox.setLayout(self.peersList)
self.peersList.addLayout(self.peerSelect)
self.peersList.addStretch(50)
self.peersList.addLayout(self.peerPortForm)
# Define widgets in peerSelect
self.peerSelect.addWidget(self.listHead)
self.peerSelect.addWidget(self.listSelPeer)
# Define widgets in peerPortForm
self.peerPortForm.addWidget(self.peerPort)
self.peerPortForm.addWidget(self.peerPortApply)
def displayStatus(self, msg):
self.status.showMessage(str(msg))
def updatePeerList(self, action, peer):
if action == "add":
newPeerItem = QtWidgets.QListWidgetItem(peer.clientId)
newPeerItem.setData(QtCore.Qt.UserRole, peer)
#self.peerDict[newPeerItem] = peer
self.listSelPeer.addItem(newPeerItem)
self.displayStatus("Discovered new peer : " + peer.clientId)
elif action == "remove":
pass
def selPeerItem(self, curr, prev):
nonPeer = curr.data(QtCore.Qt.UserRole)
print(nonPeer.extPort)
self.peerPort.setValue(nonPeer.extPort)
def applyPort(self):
selPeerInList = self.listSelPeer.selectedItems()
selNonPeer = selPeerInList[0].data(QtCore.Qt.UserRole)
#selNonPeer = selPeerData.toPyObject()
selNonPeer.extPort = self.peerPort.value()
# def displayIncomeOsc(self):
# all_items = self.listSelPeer.findItems('', QtCore.Qt.MatchRegExp)
#
# for item in all_items:
# peer = item.data(QtCore.Qt.UserRole)
# if peer.oscIncome
# def testGui(self):
# self.status.addPermanentWidget(QtWidgets.QLabel(self.ourNsmClient.states.clientId))
### QT main
class Main(QObject):
def __init__(self, qtApp, gui):
self.qtApp = qtApp
self.gui = gui
# Try to thead
self.nsmClientThread = QThread()
self.nsmClientWorker = NsmClientWorker()
self.nsmClientWorker.moveToThread(self.nsmClientThread)
self.gui.displayStatus("Init...")
## Connect alls signals
# NSM
self.nsmClientWorker.nsmReadySign.connect(self.nsmClientRdy)
self.nsmClientWorker.exitAppSign.connect(self.quit)
self.nsmClientWorker.ourNsmClientSign.connect(self.nsmClientGet)
self.nsmClientWorker.openSessSign.connect(gui.showPeersSel)
# NON
self.nsmClientWorker.updateListSign.connect(gui.updatePeerList)
self.gui.displayStatus("Worker connected")
self.nsmClientThread.started.connect(self.nsmClientWorker.run)
self.nsmClientThread.finished.connect(self.quit)
self.nsmClientThread.start()
self.gui.displayStatus("Worker thread started")
#self.nsmClientThread.exec()
self.gui.displayStatus("Worker thread exec")
qDebug("Worker in place, running...")
def nsmClientRdy(self, loops):
#self.title.setText("<b>" + str(loops) + "</b>")
pass
def nsmClientGet(self, ourNsmClient):
self.ourNsmClient = ourNsmClient
gui.testGui()
def quit(self):
qDebug("quit called")
self.gui.displayStatus("Request to quit")
#self.nsmClientThread.exit()
#self.qtApp.exit()
return "Ready to quit"
#self.title.setText(str(self.ourNsmClient.states.prettyNSMName))
if __name__ == '__main__':
qtApp = QtWidgets.QApplication(sys.argv)
handler = SignalWakeupHandler(qtApp)
nonNatUi = Gui(qtApp)
nonNatUi.show()
nonNat = Main(qtApp, nonNatUi)
# timer = QTimer()
# timer.start(100)
# timer.timeout.connect(nonNatUi.showIncomeOsc())
"""
signal.signal(signal.SIGTERM, Main.quit)
"""
##################
#Start everything
qtApp.exec_()