Skip to content

Instantly share code, notes, and snippets.

@Drvanon
Created May 4, 2012 09:54
Show Gist options
  • Save Drvanon/2593694 to your computer and use it in GitHub Desktop.
Save Drvanon/2593694 to your computer and use it in GitHub Desktop.
from twisted.application import internet, service
from twisted.internet.protocol import ServerFactory, Protocol
from twisted.protocols import amp
from twisted.python import log
import optparse
# Normally we would import these classes from another module.
class ChangePrice(amp.Command):
arguments = [('newPrice', amp.Integer())]
class Protocol(Protocol):
def connectionMade(self):
self.factory.connections.append(self)
def changePrice(self, price):
self.factory.service.price = self.factory.service.price + price
self.factory.sendPrice()
class Factory(ServerFactory):
protocol = Protocol
def __init__(self, service):
self.service = b4buy_service
self.connections = []
def sendPrice(self):
for connection in self.factory.connections:
connection.callRemote(ChangePrice, newPrice=price)
class Service(service.Service):
def __init__(self): # the in-game parameters will be here
self.products = 0
def startService(self):
service.Service.startService(self)
# configuration parameters
port = 45000
iface = '0.0.0.0'
# this will hold the services that combine to form the server
top_service = service.MultiService()
# the service holds the parameters. it will load the poem when it is
# started
b4buy_service = Service()
b4buy_service.setServiceParent(top_service)
# the tcp service connects the factory to a listening socket. it will
# create the listening socket when it is started
factory = Factory(service)
tcp_service = internet.TCPServer(port, factory, interface=iface)
tcp_service.setServiceParent(top_service)
# this variable has to be named 'application'
application = service.Application("client")
# this hooks the collection we made to the application
top_service.setServiceParent(application)
# at this point, the application is ready to go. when started by
# twistd it will start the child services, thus starting up the
# server
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment