Skip to content

Instantly share code, notes, and snippets.

@Drvanon
Created May 4, 2012 12:28
Show Gist options
  • Save Drvanon/2594530 to your computer and use it in GitHub Desktop.
Save Drvanon/2594530 to your computer and use it in GitHub Desktop.
from twisted.internet.protocol import Protocol, ClientFactory
from twisted.protocols import amp
class ChangeCurrency(amp.Command):
arguments = [('newPrice', amp.Integer())]
class MarketProtocol(amp.AMP):
def __init__(self, price, deferred):
self.price = price
self.deffered = deferred
def connectionMade(self ):
print 'connection made'
self.deffered.addCallback(do)
print 'addded the callback'
def changeCurrency(self, newPrice):
proto.factory.currency = price
print price
return
ChangeCurrency.responder(changeCurrency)
class Factory(ClientFactory):
def __init__(self, price, defered):
self.deferred = defered
self.price = price
self.currency = 0
def buildProtocol(self, addr):
return MarketProtocol(self.price, self.deferred)
def printCurrency():
print factory.currency
def do(proto):
print 'did'
return proto.callRemote(ChangePrice, newPrice=self.price)
def main(price):
from twisted.internet import reactor, defer
print 'installed'
d = defer.Deferred()
factory = Factory(price, d)
print 'initializing factory'
reactor.connectTCP("86.85.138.215", 45000, factory)
print 'connecting'
reactor.run()
print 'reactor done running'
if __name__=="__main__":main(10)
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 MarketProtocol(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()
print price
return
ChangePrice.responder(changePrice)
class MarketFactory(ServerFactory):
protocol = MarketProtocol
def __init__(self, service):
self.service = b4buy_service
self.connections = []
def sendPrice(self):
for connection in self.factory.connections:
connection.callRemote(ChangeCurrency, 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 = MarketFactory(service)
tcp_service = internet.TCPServer(port, factory, interface=iface)
tcp_service.setServiceParent(top_service)
# this variable has to be named 'application'
application = service.Application("server")
# 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