| | 53 | |
|---|
| | 54 | |
|---|
| | 55 | if __name__ == '__main__': |
|---|
| | 56 | import sys |
|---|
| | 57 | app = QtCore.QCoreApplication(sys.argv) |
|---|
| | 58 | |
|---|
| | 59 | from Axon.background import background |
|---|
| | 60 | from Axon.Component import component |
|---|
| | 61 | |
|---|
| | 62 | class Forward(component): |
|---|
| | 63 | Inboxes = {"inbox" : "", |
|---|
| | 64 | "control" : ""} |
|---|
| | 65 | |
|---|
| | 66 | Outboxes = {"outbox" : "", |
|---|
| | 67 | "signal" : "Shutdown signal"} |
|---|
| | 68 | |
|---|
| | 69 | def __init__(self): |
|---|
| | 70 | super(Forward, self).__init__() |
|---|
| | 71 | |
|---|
| | 72 | def setQObject(self, qobj): |
|---|
| | 73 | self.qobj = qobj |
|---|
| | 74 | |
|---|
| | 75 | def main(self): |
|---|
| | 76 | while 1: |
|---|
| | 77 | if self.dataReady("control"): |
|---|
| | 78 | mes = self.recv("control") |
|---|
| | 79 | |
|---|
| | 80 | if isinstance(mes, shutdownMicroprocess) or \ |
|---|
| | 81 | isinstance(mes, producerFinished): |
|---|
| | 82 | self.send(producerFinished(), "signal") |
|---|
| | 83 | break |
|---|
| | 84 | |
|---|
| | 85 | if self.dataReady("inbox"): |
|---|
| | 86 | message = self.recv("inbox") |
|---|
| | 87 | print "# ", message |
|---|
| | 88 | self.qobj.emit(QtCore.SIGNAL("forwardingMessage(PyQt_PyObject)"), message) |
|---|
| | 89 | |
|---|
| | 90 | if not self.anyReady(): |
|---|
| | 91 | self.pause() |
|---|
| | 92 | |
|---|
| | 93 | yield 1 |
|---|
| | 94 | |
|---|
| | 95 | |
|---|
| | 96 | class SenderObject(QAxonObject): |
|---|
| | 97 | def __init__(self, parent=None): |
|---|
| | 98 | QAxonObject.__init__(self, parent) |
|---|
| | 99 | QtCore.QObject.connect(self, QtCore.SIGNAL("sendMessage(PyQt_PyObject)"), |
|---|
| | 100 | self.pushMessage) |
|---|
| | 101 | |
|---|
| | 102 | def setComponent(self, component): |
|---|
| | 103 | QAxonObject.setComponent(self, component) |
|---|
| | 104 | self.emit(QtCore.SIGNAL("setupOneMapping(PyQt_PyObject)"), 'inbox') |
|---|
| | 105 | |
|---|
| | 106 | def pushMessage(self, message): |
|---|
| | 107 | print "## ", message |
|---|
| | 108 | self._deliver(message, 'inbox') |
|---|
| | 109 | |
|---|
| | 110 | class ReceiverObject(QtCore.QObject): |
|---|
| | 111 | def __init__(self, parent=None): |
|---|
| | 112 | QtCore.QObject.__init__(self, parent) |
|---|
| | 113 | |
|---|
| | 114 | def displayMessage(self, message): |
|---|
| | 115 | print message |
|---|
| | 116 | app.exit() |
|---|
| | 117 | |
|---|
| | 118 | |
|---|
| | 119 | senderObj = SenderObject() |
|---|
| | 120 | receiverObj = ReceiverObject() |
|---|
| | 121 | |
|---|
| | 122 | QtCore.QObject.connect(senderObj, QtCore.SIGNAL("forwardingMessage(PyQt_PyObject)"), |
|---|
| | 123 | receiverObj.displayMessage) |
|---|
| | 124 | |
|---|
| | 125 | senderObj.setComponent(Forward()) |
|---|
| | 126 | senderObj.emit(QtCore.SIGNAL("sendMessage(PyQt_PyObject)"), 'hello world') |
|---|
| | 127 | |
|---|
| | 128 | # start Kamaelia and Axon in the background |
|---|
| | 129 | background(slowmo=0.01).start() |
|---|
| | 130 | senderObj.emit(QtCore.SIGNAL("activateComponent()")) |
|---|
| | 131 | sys.exit(app.exec_()) |
|---|
| | 132 | |
|---|
| | 133 | |
|---|
| | 134 | |
|---|