Skip to content

Commit

Permalink
! Ticket #13 - ServerNotification
Browse files Browse the repository at this point in the history
  • Loading branch information
jarus committed Jul 16, 2010
1 parent 84f5b6a commit 6a0327e
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 19 deletions.
58 changes: 39 additions & 19 deletions PyTS3.py
Expand Up @@ -34,6 +34,7 @@ def __str__(self):
return "ID %s (%s)" % (self.code, self.msg)

class ServerQuery():
TSRegex = re.compile(r"(\w+)=(.*?)(\s|$|\|)")
def __init__(self, ip='127.0.0.1', query=10011):
"""
This class contains functions to connecting a TS3 Query Port and send command.
Expand All @@ -45,7 +46,6 @@ def __init__(self, ip='127.0.0.1', query=10011):
self.IP = ip
self.Query = int(query)
self.Timeout = 5.0
self.TSRegex = re.compile(r"(\w+)=(.*?)(\s|$|\|)")

def connect(self):
"""
Expand Down Expand Up @@ -168,43 +168,63 @@ def __init__(self, ip='127.0.0.1', query=10011):
self.IP = ip
self.Query = int(query)
self.Timeout = 5.0
self.TSRegex = re.compile(r"(\w+)=(.*?)(\s|$|\|)")
self.LastCommand = 0

self.Lock = thread.allocate_lock()
self.WorkerFunc = []
self.RegistedNotifys = []
self.RegistedEvents = []
thread.start_new_thread(self.worker, ())

def worker(self):
while True:
self.Lock.acquire()
WorkerFunc = self.WorkerFunc
RegistedNotifys = self.RegistedNotifys
LastCommand = self.LastCommand
self.Lock.release()
if len(WorkerFunc) == 0:
if len(RegistedNotifys) == 0:
continue

if LastCommand < time.time() - 180:
self.command('version')
self.Lock.acquire()
self.LastCommand = time.time()
self.Lock.release()
telnetResponse = self.telnet.read_until("\n", 0.1)
if telnetResponse.startswith('notify'):
notifyName = telnetResponse.split(' ')[0]
ParsedInfo = self.TSRegex.findall(telnetResponse)
notifyData = {}
for ParsedInfoKey in ParsedInfo:
notifyData[ParsedInfoKey[0]] = self.escaping2string(ParsedInfoKey[1])
for func in WorkerFunc:
func(notifyName, notifyData)
for RegistedNotify in RegistedNotifys:
if RegistedNotify['notify'] == notifyName:
RegistedNotify['func'](notifyName, notifyData)
time.sleep(0.2)

def register(self, func, events=[]):
for event in events:
self.command('servernotifyregister', event)

def registerNotify(self, notify, func):
notify2func = {'notify': notify, 'func': func}

self.Lock.acquire()
self.WorkerFunc.append(func)
self.RegistedNotifys.append(notify2func)
self.LastCommand = time.time()
self.Lock.release()

def unregister(self, func, events=[]):
for event in events:
self.command('servernotifyunregister', event)

def unregisterNotify(self, notify, func):
notify2func = {'notify': notify, 'func': func}

self.Lock.acquire()
self.WorkerFunc.remove(func)
self.Lock.release()
self.RegistedNotifys.remove(notify2func)
self.LastCommand = time.time()
self.Lock.release()

def registerEvent(self, eventName, parameter={}, option=[]):
parameter['event'] = eventName
self.RegistedEvents.append(eventName)
self.command('servernotifyregister', parameter, option)
self.Lock.acquire()
self.LastCommand = time.time()
self.Lock.release()

def unregisterEvent(self):
self.command('servernotifyunregister')


74 changes: 74 additions & 0 deletions examples/servernotify.py
@@ -0,0 +1,74 @@
#!/usr/bin/python2.5
# -*- coding: utf-8 -*-
# Copyright (c) 2008 Christoph Heer (Christoph.Heer@googlemail.com)
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the \"Software\"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

## --- CONFIG AREA ----
TS3ServerIP = "thelabmill.de"
TS3QueryPort = "10011"
ServerID = "1"
## --- CONFIG AREA ----

import time
import PyTS3

## First we create a Function, it is called at a notify.
## A NotifyPrinter Func has 2 args:
## 1. NotifyName (str)
## 2. NotifyData (dict)
## In this example the function show the name and the data
def notifyPrinter(name, data):
print "New Notify: %s" % (name)
print data

## Now we create the Main Application
def main():
print "Example of the PyTS3 ServerNotification Class"
## We create a instance of PyTS3.ServerNotification with IP and QueryPort
ts3 = PyTS3.ServerNotification(TS3ServerIP, TS3QueryPort)
## After the Init, we has a new thread, the worker, it read the input form the server
## ServerNotification has all functions of ServerQuery but use only this Class for Notifycations

## We connect
ts3.connect()
## Now we select a server
ts3.command('use', {'sid': ServerID})

## After this we register events, which we can do with the function
## registerEvent(self, eventName, parameter={}, option=[])
ts3.registerEvent('server')
## You can add later other events too

## Now we register the notifiy and the function which is work with this
ts3.registerNotify('notifycliententerview', notifyPrinter)
## the first para is the name of the notify the secound is the function
## which get the notify data

## Now we start a loop and wait for events or other task
while True:
time.sleep(0.5)

## When a the worker found a new notify than he call the function notifyPrinter and
## we can see the NotifyName and the Data


if __name__ == '__main__':
main()

0 comments on commit 6a0327e

Please sign in to comment.