[zeromq-dev] twisted/coherence and zmq

Nicholas Lagaros nicholas at lagaros.com
Tue Nov 19 17:43:30 CET 2013


 

I am trying to create simple publisher that will send media player
updates to another process. I understand there are event loop conflicts
between zmq and twisted. Can anyone recommend a better way to code this?


#! /usr/bin/python2.5

import sys
sys.path.insert(1, './lib')

import cPickle as pickle

from twisted.internet import reactor
from coherence.base import Coherence
from coherence.upnp.devices.control_point import ControlPoint
from coherence.upnp.core import DIDLLite

import zmq

ZonePlayers = []
Son = [0,0,0,0,0,0,0,0,0,0,0,0]
SonArray = [{}]
sck = None

class SonosPlayer:
 SonosCount = 0

 def __init__(self, uuid):
 global SonArray
 self.uuid = uuid

 self.index = SonosPlayer.SonosCount
 SonosPlayer.SonosCount += 1
 SonArray[self.index] = {'uuid':self.uuid}

 def returnSonosCount(self):
 return SonosPlayer.SonosCount

 def state_variable_change(self, variable):
 global sck
 global SonArray
 print "variable: %s" % variable.name
 #print "variable value: %s" % variable.value
 print "update from: %s" % self.uuid
 #print SonArray

 if variable.name == 'CurrentTrackMetaData':
 if variable.value != None and len(variable.value)>0:
 try:
 elt = DIDLLite.DIDLElement.fromString(variable.value)
 for item in elt.getItems():
 SonArray[self.index]['creator'] = item.creator
 SonArray[self.index]['albumArtist'] = item.albumArtist
 SonArray[self.index]['artist'] = item.artist
 SonArray[self.index]['albumArtURI'] = item.albumArtURI
 SonArray[self.index]['title'] = item.title
 SonArray[self.index]['album'] = item.album
 except SyntaxError:
 print "current error"
 return
 elif variable.name == 'NextTrackMetaData':
 if variable.value != None and len(variable.value)>0:
 try:
 elt = DIDLLite.DIDLElement.fromString(variable.value)
 for item in elt.getItems():
 SonArray[self.index]['next_creator'] = item.creator
 SonArray[self.index]['next_albumArtist'] = item.albumArtist
 SonArray[self.index]['next_artist'] = item.artist
 SonArray[self.index]['next_albumArtURI'] = item.albumArtURI
 SonArray[self.index]['next_title'] = item.title
 SonArray[self.index]['next_album'] = item.album
 except SyntaxError:
 print "next error"
 return
 elif variable.name == 'TransportState':
 SonArray[self.index]['TransportState'] = variable.value

 f = open("SonosEvents.pkl", "wb")
 pickle.dump(SonArray, f, protocol=2)
 f.close()

 msg = self.uuid + " " + SonArray[self.index]['title']
 print msg
 sck.send(msg)

def media_renderer_found(client,udn):
 global ZonePlayers
 global Son
 Xudn = udn[5:-3]
 print ("%s:%s:%s" % (udn, Xudn, ZonePlayers))
 if Xudn in ZonePlayers:
 print "media_renderer_found", udn
 Son[SonosPlayer.SonosCount] = SonosPlayer(Xudn)
 client.av_transport.subscribe_for_variable('CurrentTrackMetaData',
Son[SonosPlayer.SonosCount].state_variable_change)
 client.av_transport.subscribe_for_variable('NextTrackMetaData',
Son[SonosPlayer.SonosCount].state_variable_change)
 client.av_transport.subscribe_for_variable('LastChange',
Son[SonosPlayer.SonosCount].state_variable_change)
 client.av_transport.subscribe_for_variable('TransportState',
Son[SonosPlayer.SonosCount].state_variable_change)

def media_renderer_removed(udn):
 print "media_renderer_removed", udn

def start():
 control_point = ControlPoint(Coherence({'logmode':'warn'}),
auto_client=['MediaRenderer'])
 control_point.connect(media_renderer_found,
'Coherence.UPnP.ControlPoint.MediaRenderer.detected')
 control_point.connect(media_renderer_removed,
'Coherence.UPnP.ControlPoint.MediaRenderer.removed')

f = open("ZonePlayers.pkl", "rb")
ZonePlayers = pickle.load(f)
f.close()

context = zmq.Context()
sck = context.socket(zmq.PUB)
sck.bind('tcp://127.0.0.1:9999')

reactor.callWhenRunning(start)
reactor.run()

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20131119/288863af/attachment.htm>


More information about the zeromq-dev mailing list