Esempio n. 1
0
class Chat(threading.Thread):
    def __init__(self, user, varName, wsName, **opt):
        threading.Thread.__init__(self, name="Chat")
        self._user, self._varName, self._wsName = user, varName, wsName
        self._ws = NetWorkSpace(wsName, persistent=True, **opt)
        self._ws.declare(varName, FIFO)
        self._queue = Queue.Queue()
        recvWs = NetWorkSpace(wsName, **opt)
        self._ifind = recvWs.ifind(varName)
        self.setDaemon(True)
        self.start()

    def sendMessage(self, ent):
        msg = ent.get().strip()
        if len(msg) > 0:
            self._ws.store(self._varName, "%s: %s" % (self._user, msg))
        ent.delete(0, END)

    def checkForNewMessages(self, txt):
        txt.config(state=NORMAL)
        while not self._queue.empty():
            txt.insert(END, self._queue.get() + "\n")
            txt.see(END)
        txt.config(state=DISABLED)
        txt.after(1000, self.checkForNewMessages, txt)

    def run(self):
        for val in self._ifind:
            self._queue.put(val)
Esempio n. 2
0
    def run(self):
        while True:
            try:
                try:
                    ws = NetWorkSpace(self.wsName, self.nwsHost, self.nwsPort,
                            persistent=True)
                    ws.declare(self.varName, FIFO)

                    while True:
                        s = ws.fetch(self.varName)
                        try:
                            # split the request using the first character
                            x = s[1:].split(s[0])
                            r = {'PythonSleighNwsHost': self.nwsHost,
                                 'PythonSleighNwsPort': str(self.nwsPort)}
                            r['PythonSleighNwsName'], r['PythonSleighWorkerCount'], \
                                r['PythonSleighID'], r['PythonSleighWorkingDir'], \
                                r['PythonSleighWorkerOut'], r['PythonSleighLogDir'], \
                                r['PythonSleighUserName'], r['PythonSleighModulePath'] = x
                            self.gotRequest(r)
                        except:
                            # bad request: ignore it
                            pass
                except NwsServerException:
                    # server is probably down.  sleep a bit, and try again
                    time.sleep(10)
                except NwsOperationException:
                    # maybe someone deleted my variable to signal a shutdown?
                    return
            finally:
                # ws may not be defined
                try:ws.server.close()
                except: pass
                ws = None
Esempio n. 3
0
class Provider:
    def __init__(self, ws_name='Estoque', time_sleep=1, batch_size=10):
        self.time_sleep = time_sleep
        self.batch_size = batch_size
        #self.rmi=RMIManager()
        self.workspace = NetWorkSpace(ws_name,
                                      cfg.ts_machine_ip,
                                      cfg.ts_machine_port,
                                      persistent=True)

    #Método para gerar lotes de um produto
    # product_amount (int): quantidade requisitada do produto.
    def generateBatch(self, product_amount):
        n_batchs = int(ceil((product_amount * 1.0) / self.batch_size))
        delivery = 0
        for i in range(n_batchs):
            sleep(self.time_sleep)
            delivery += self.batch_size
        return delivery

    #Método que fornece uma quantidade do produto no espaço de tuplas
    # product_name (string): nome do produto;
    # product_amount (int): quantidade requisitada do produto
    def supply(self, product_name, product_amount):
        print 'Processando requisição para (' + str(product_name) + ',' + str(
            product_amount) + ')'
        sleep(self.time_sleep)
        delivery = self.generateBatch(product_amount)
        self.workspace.store(product_name, delivery)
        print 'Requisição entregue'
Esempio n. 4
0
class TSManager:

    def __init__(self):
        self.ws = NetWorkSpace('Estoque', cfg.ts_machine_ip, cfg.ts_machine_port, persistent=True)

    def put(self, item_name, quantity):
        self.ws.store(item_name, quantity)

    def get(self, item_name):
        available = self.ws.fetchTry(item_name)
        return available

    def checkStock(self, content):
        new_order = content
        for item in new_order["items"]:
            left =  item["need"] - item["have"]
            while item["need"] > item["have"]:
                in_stock = self.get(item["name"])
                if in_stock == None:
                    break
                got = min(in_stock, left)
                item["have"] += got
                in_stock -= got
                if in_stock > 0:
                    self.put(item["name"], in_stock)

        return new_order
Esempio n. 5
0
 def __init__(self, ws_name='Estoque', time_sleep=1, batch_size=10):
     self.time_sleep = time_sleep
     self.batch_size = batch_size
     #self.rmi=RMIManager()
     self.workspace = NetWorkSpace(ws_name,
                                   cfg.ts_machine_ip,
                                   cfg.ts_machine_port,
                                   persistent=True)
Esempio n. 6
0
def espetaculos():
    page = '<link rel="stylesheet" type="text/css" media="screen" href="../static/index.css" />'
    page += '<h1>Eventos</h1>'
    ts = NetWorkSpace("database", HOST_TS, PORT_TS)
    for i in ts.listVars(wsName='database', format='dict'):
        page += "<button class=\"show\"><a href=\"/consulta/<" + i + ">\">" + i + "</a></button><br>"
    page += '<br><br><button class="show"><a href="/">Home</a></button>'
    return page
def main():
    # initialize variables from environment
    modulePath = Env.get('PythonSleighModulePath', '')
    logDir = Env.get('PythonSleighLogDir')
    if not logDir or not os.path.isdir(logDir):
        logDir = _TMPDIR
    outfile = Env.get('PythonSleighWorkerOut')
    if outfile:
        outfile = os.path.join(logDir, os.path.split(outfile)[1])
    else:
        outfile = _NULFILE
    Env['PythonSleighLogFile'] = outfile
    verbose = Env.has_key('PythonSleighWorkerOut') and 1 or 0
    nwsName = Env['PythonSleighNwsName']
    nwsHost = Env.get('PythonSleighNwsHost', 'localhost')
    nwsPort = int(Env.get('PythonSleighNwsPort', '8765'))
    if Env.has_key('PythonSleighName'):
        name = Env['PythonSleighName']
        if Env.has_key('PythonSleighID'):
            name = '%s@%s' % (name, Env['PythonSleighID'])
        logger.setName(name)
    logger.logDebug(nwsName, nwsHost, nwsPort)

    nws = NetWorkSpace(nwsName, nwsHost, nwsPort, useUse=True, create=False)
    newProtocol = nws.findTry('version') is not None
    if newProtocol:
        worker_id = nws.fetch('worker_ids')
        Env['PythonSleighRank'] = worker_id

    # create the script file for the worker to execute
    script = '''\
import sys, os
try: os.setpgid(0, 0)
except: pass
try: sys.stdout = sys.stderr = open(%s, 'w', 0)
except: pass
sys.path[1:1] = %s.split(os.pathsep)
from nws.sleigh import cmdLaunch
print "entering worker loop"
cmdLaunch(%d)
''' % (repr(outfile), repr(modulePath), verbose)
    fd, tmpname = tempfile.mkstemp(suffix='.py', prefix='__nws', text=True)
    tmpfile = os.fdopen(fd, 'w')
    tmpfile.write(script)
    tmpfile.close()

    logger.logDebug("executing Python worker")
    argv = [sys.executable, tmpname]
    out = open(outfile, 'w')

    try:
        p = subprocess.Popen(argv, stdin=open(tmpname), stdout=out,
                stderr=subprocess.STDOUT)
    except OSError, e:
        logger.logError("error executing command:", argv)
        raise e
Esempio n. 8
0
 def __init__(self, user, varName, wsName, **opt):
     threading.Thread.__init__(self, name="Chat")
     self._user, self._varName, self._wsName = user, varName, wsName
     self._ws = NetWorkSpace(wsName, persistent=True, **opt)
     self._ws.declare(varName, FIFO)
     self._queue = Queue.Queue()
     recvWs = NetWorkSpace(wsName, **opt)
     self._ifind = recvWs.ifind(varName)
     self.setDaemon(True)
     self.start()
Esempio n. 9
0
def consulta(espetaculo):

    form = FormCartao(request.form)

    print form.errors
    if request.method == 'POST':
        cartao = request.form['cartao']
        print cartao

    espetaculo = espetaculo.split('<')[1]
    espetaculo = espetaculo.split('>')[0]

    port = PORT_TS
    tsname = espetaculo
    host = HOST_TS

    ts = NetWorkSpace(tsname, host, port)

    seats = ""
    br = 0
    try:
        seats = '<link rel="stylesheet" type="text/css" media="screen" href="../static/index.css" />'
        lista = ts.listVars(wsName=tsname, format='dict')
        if not (lista):
            seats += "<h1> Sinto muito! Evento esgotado. </h1>"
        else:
            seats += '<form action="" method="post">'
            seats += '{{ form.csrf }}'
            seats += '<div class="input text">'
            seats += '{{ form.cartao.label }} {{ form.cartao }}'
            seats += '</div><br>'
            seats += '<button type="submit">Confirmar cartao</button>'
            seats += '</form>'

            if form.validate():
                seats += "<h1> Assentos </h1>"
                for i in sorted(lista):
                    seats += "<a href=\"/compra/<" + i + ">/<" + espetaculo + ">/<" + cartao + ">\"><button>"
                    seats += i
                    seats += "</button></a>"
                    br += 1
                    if (br == 10):
                        seats += "<br>"
                        br = 0

        seats += '<br><br><button class="show"><a href="/">Home</a></button>'
    except:
        raise
        seats = "There's no more seats. Sorry :("

    return render_template_string(seats, form=form)
Esempio n. 10
0
def babelfish(host, port):
    ws = NetWorkSpace('Python babelfish', host, port)

    while 1:
        try:
            v = ws.fetch('food')
            t = str(v)
            if len(t) > MAXLEN:
                t = t[:MAXLEN] + '[WARNING: output truncated]'
            elif not t:
                t = repr(v)
        except NwsUnpicklingException, e:
            t = 'Unpickling error: ' + str(e.exception)
        ws.store('doof', t)
Esempio n. 11
0
 def __init__(self, nwsHost, nwsPort, nwsName, gotShutdown):
     threading.Thread.__init__(self, name='Sentinel:' + nwsName)
     self.nwsHost = nwsHost
     self.nwsPort = nwsPort
     self.nwsName = nwsName
     self.gotShutdown = gotShutdown
     self.ws = NetWorkSpace(self.nwsName, self.nwsHost, self.nwsPort,
             useUse=True, create=False)
Esempio n. 12
0
def compra(assento, espetaculo, cartao):

    assento = assento.split('<')[1]
    assento = assento.split('>')[0]

    espetaculo = espetaculo.split('<')[1]
    espetaculo = espetaculo.split('>')[0]

    cartao = cartao.split('<')[1]
    cartao = cartao.split('>')[0]

    host = HOST_TS
    port = PORT_TS
    tsname = espetaculo

    ts = NetWorkSpace(tsname, host, port)

    try:
        seat = ts.fetchTry(assento)
        ts.deleteVar(seat)

        if (seat == None):
            exit(0)

        #Envio para fila de mensagens
        connection = pika.BlockingConnection(
            pika.ConnectionParameters(host='localhost'))
        channel = connection.channel()

        channel.queue_declare(queue='pendentes')

        channel.basic_publish(exchange='',
                              routing_key='pendentes',
                              body=cartao + '-' + seat)
        print(" [x] Mensagem recebida'")

        connection.close()

        aprovado = fila(assento)

        if aprovado == 1:
            retorno = '<link rel="stylesheet" type="text/css" media="screen" href="../static/index.css" />'
            retorno += "<h1>Compra aprovada! Obrigado por comprar conosco :D</h1>"
            retorno += '<br><br><button class="show"><a href="/">Home</a></button>'
            return retorno
        elif aprovado == 0:
            ts.store(seat, assento)
            retorno = '<link rel="stylesheet" type="text/css" media="screen" href="../static/index.css" />'
            retorno += "<h1>Compra reprovada :( Tente com outra forma de pagamento.</h1>"
            retorno += '<br><br><button class="show"><a href="/">Home</a></button>'
            return retorno
        else:
            retorno = '<link rel="stylesheet" type="text/css" media="screen" href="../static/index.css" />'
            retorno += "<h1>Falha na compra</h1>"
            retorno += '<br><br><button class="show"><a href="/">Home</a></button>'
            return retorno

    except:
        raise
        return "Except: Falha na compra."
Esempio n. 13
0
class Chat:
    def __init__(self, user, varName, wsName, **opt):
        self._user, self._varName, self._wsName = user, varName, wsName
        self._ws = NetWorkSpace(wsName, persistent=True, **opt)
        self._ws.declare(varName, FIFO)
        self._ifindTry = self._ws.ifindTry(varName)

    def sendMessage(self, ent):
        msg = ent.get().strip()
        if len(msg) > 0:
            self._ws.store(self._varName, "%s: %s" % (self._user, msg))
        ent.delete(0, END)

    def checkForNewMessages(self, txt):
        txt.config(state=NORMAL)
        self._ifindTry.restart()
        for msg in self._ifindTry:
            txt.insert(END, msg + '\n')
            txt.see(END)
        txt.config(state=DISABLED)
        txt.after(1000, self.checkForNewMessages, txt)
Esempio n. 14
0
class Sentinel(threading.Thread):
    def __init__(self, nwsHost, nwsPort, nwsName, gotShutdown):
        threading.Thread.__init__(self, name='Sentinel:' + nwsName)
        self.nwsHost = nwsHost
        self.nwsPort = nwsPort
        self.nwsName = nwsName
        self.gotShutdown = gotShutdown
        self.ws = NetWorkSpace(self.nwsName, self.nwsHost, self.nwsPort,
                useUse=True, create=False)

    def __str__(self):
        return "%s@%s:%d" % (self.nwsName, self.nwsHost, self.nwsPort)

    def run(self):
        try:
            try:
                self.ws.find('Sleigh ride over')
                self.ws.store('bye', 'Sleigh ride over')
            except Exception, e:
                try: self.ws.store('bye', str(sys.exc_info()[1]))
                except: pass
        finally:
            try: self.gotShutdown(self)
            except: pass
            try: self.ws.server.close()
            except: pass
Esempio n. 15
0
if len(sys.argv) in [2, 3]:
    try:
        loops = int(sys.argv[1])
    except:
        print >> sys.stderr, "Usage: %s [<loops> [<wsname>]]" % sys.argv[0]
        sys.exit(1)

    if len(sys.argv) == 3:
        wsname = sys.argv[2]
elif len(sys.argv) != 1:
    print >> sys.stderr, "Usage: %s [<loops> [<wsname>]]" % sys.argv[0]
    sys.exit(1)

try:
    from nws.client import NetWorkSpace
    nws = NetWorkSpace(wsname, host, port, useUse=True)
    game = nws.fetch("game")
    nws.store("game", game + 1)
    pong = "pong_%d" % game
    request = {'replyto': pong}

    print "Starting a ping-pong game", game
    for i in xrange(loops):
        request['i'] = i
        nws.store("ping", request)
        reply = nws.fetch(pong)
        print reply
        if i != reply['i']:
            print "Error: expected %d, received %d" % (i, reply['i'])

    nws.deleteVar(pong)
Esempio n. 16
0
#!/usr/bin/env python

import sys

try:
    from nws.client import NetWorkSpace
except ImportError, e:
    print >> sys.stderr, "unable to import the nws.client python module"
    print >> sys.stderr, "make sure you have nwsclient installed on this machine"
    sys.exit(1)

try:
    ws = NetWorkSpace('snake pit')
except ImportError, e:
    print >> sys.stderr, "make sure you're running the NWS server on this machine"
    print >> sys.stderr, str(e)
    sys.exit(1)

print 'connected, listing contents of netWorkSpace (should be nothing there).'
print ws.listVars()

ws.store('x', 1)
print 'should now see x.'
print ws.listVars()

print 'find (but don\'t consume) x.'
print ws.find('x')
print 'check that it is still there.'
print ws.listVars()

print 'associate another value with x.'
Esempio n. 17
0
#!/usr/bin/env python

import sys, socket

host = "localhost"
port = 8765
wsname = "hello"

try:
    from nws.client import NetWorkSpace
    nws = NetWorkSpace(wsname, host, port)

    count = int(raw_input("Number of times to loop? "))
    print "hello: iterations:", count
    nws.store("hello example", count)

    for i in range(count):
        nws.store("hello", i)
        j = nws.fetch("hello")

    nws.fetch("hello example")
    print "Success"

except KeyboardInterrupt:
    print "hello exiting"
except ImportError:
    print "Unable to import the nwsClient module"
    print "You may need to set or correct your PYTHONPATH"
except socket.error:
    print "NWS server not running on %s, port %d" % (host, port)
except:
Esempio n. 18
0
#start tuple space - twistd -y /etc/nws.tac
#deleteVar() - deleta
#find() - return a value
#store() - insere no espaco 
#fetch() - retira do espaco
#fetchTry() - retira nao bloqueante

port = 8765
host = sys.argv[1]

shows = ["Red Hot Chili Peppers", "ACDC", "Linkin Park", "Iron Maiden"]

for tsname in shows:

    data = NetWorkSpace("database", host, port, persistent=True)
    data.declare(tsname, 'fifo')
    data.store(tsname, tsname)

    ts = NetWorkSpace(tsname, host, port, persistent=True)

    alpha = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

    try:
        for i in alpha[:10]:
            for j in range(10):
                ts.declare("%s%d" % (i, j), 'fifo')
                ts.store("%s%d" % (i, j), "%s%d" % (i, j))
        print("Creation successful!")
    except:
        print ("Creation failed.")
Esempio n. 19
0
 def __init__(self):
     self.ws = NetWorkSpace('Estoque', '192.168.1.102', persistent=True)
Esempio n. 20
0
 def __init__(self):
     self.ws = NetWorkSpace('Estoque', cfg.ts_machine_ip, cfg.ts_machine_port, persistent=True)
Esempio n. 21
0
from nws.client import NetWorkSpace
import sys

#twistd -y /etc/nws.tac

port = 8765

try:
    ts = NetWorkSpace(sys.argv[1], sys.argv[2], port, persistent=True)

    print("Creation successful!")
except:
    print("Creation failed.")
    print("start_tuplespace.py <tuplespace_name> <host>")
Esempio n. 22
0
            elif opt == '-p':
                nkw['serverPort'] = int(arg)
            else:
                raise 'internal error: out-of-sync with getopt'

        if len(args) < 1:
            op = 'ls'  # default operation
        elif args[0] not in ['list', 'ls', 'dir',
                             'put', 'store', 'write',
                             'get', 'find', 'read',
                             'delete', 'del', 'remove', 'rm']:
            usage()
        else:
            op = args.pop(0)

        ws = NetWorkSpace('NWS Copy', **nkw)

        if op in ['list', 'ls', 'dir']:
            vars = listVars(ws)
            vars.sort()
            for var in vars:
                print var
        elif op in ['put', 'store', 'write']:
            if not args: usage()
            for n in args:
                try:
                    f = open(n, 'rb')
                except IOError:
                    print >> sys.stderr, "error reading file", n
                    continue
Esempio n. 23
0
#!/usr/bin/env python

from nwss.local import NwsLocalServer
from nws.client import NetWorkSpace
from nws.sleigh import Sleigh
from math import sqrt

# start the local nws server
srv = NwsLocalServer(daemon=False)
srv.wait_until_started()

# try an nws example
ws = NetWorkSpace('foo', serverPort=srv.port)
print ws
for i in range(10):
    ws.store('x', 'Value %d' % i)
print list(ws.ifetchTry('x'))

# try a sleigh example
s = Sleigh(nwsPort=srv.port)
print s
for i in xrange(10):
    print s.eachElem(sqrt, range(10))

# shutdown the local nws server
print 'shutting it down'
srv.shutdown()

assert not srv.isAlive()

try:
Esempio n. 24
0
 def __init__(self, user, varName, wsName, **opt):
     self._user, self._varName, self._wsName = user, varName, wsName
     self._ws = NetWorkSpace(wsName, persistent=True, **opt)
     self._ws.declare(varName, FIFO)
     self._ifindTry = self._ws.ifindTry(varName)
Esempio n. 25
0
import sys, socket, time

host = "localhost"
port = 8765
wsname = "ping-pong"

if len(sys.argv) == 2:
    wsname = sys.argv[1]
elif len(sys.argv) != 1:
    print >> sys.stderr, "Usage: %s [<wsname>]" % sys.argv[0]
    sys.exit(1)

try:
    from nws.client import NetWorkSpace

    nws = NetWorkSpace(wsname, host, port)
    nws.store("game", 0)

    print "Ping-pong server '%s' starting" % wsname
    while 1:
        request = nws.fetch("ping")
        pong = request["replyto"]
        request["time"] = time.asctime()
        print "Got a ping from", pong
        nws.store(pong, request)

except KeyboardInterrupt:
    print "Ping-pong server '%s' exiting" % wsname
except ImportError:
    print "Unable to import the nwsClient module"
    print "You may need to set or correct your PYTHONPATH"
Esempio n. 26
0
def main():
    # initialize variables from environment
    modulePath = Env.get("PythonSleighModulePath", "")
    logDir = Env.get("RSleighLogDir")
    if not logDir or not os.path.isdir(logDir):
        logDir = _TMPDIR
    verbose = Env.has_key("RSleighWorkerOut") and "TRUE" or "FALSE"
    nwsName = Env["RSleighNwsName"]
    nwsHost = Env.get("RSleighNwsHost", "localhost")
    nwsPort = int(Env.get("RSleighNwsPort", "8765"))
    print nwsName, nwsHost, nwsPort

    # create the script file for the worker to execute
    script = (
        """\
# use RSleighScriptDir to add the directory that contains
# the nws package to the library path if possible
# (and without modifying the worker's global environment)
local({
    scriptDir <- Sys.getenv('RSleighScriptDir')
    if (basename(scriptDir) == 'bin') {
        nwsDir <- dirname(scriptDir)
        nwsPkg <- basename(nwsDir)
        if (nwsPkg == 'Rbig') {
            libDir <- dirname(nwsDir)
            oldPaths <- .libPaths()
            newPaths <- c(libDir, oldPaths)
            cat("setting library paths to", newPaths, "\n")
            .libPaths(newPaths)
        }
    }
})

library(Rbig)
cmdLaunch(%s)
"""
        % verbose
    )
    fd, tmpname = tempfile.mkstemp(suffix=".R", prefix="__nws", dir=_TMPDIR, text=True)
    tmpfile = os.fdopen(fd, "w")
    tmpfile.write(script)
    tmpfile.close()

    print "executing R worker(s)"
    rprog = Env.get("RProg", "R")
    argv = [rprog, "--vanilla", "--slave"]

    if NetWorkSpace:
        nws = NetWorkSpace(nwsName, nwsHost, nwsPort, useUse=True, create=False)
        newProtocol = nws.findTry("version") is not None
    else:
        nws = None
        newProtocol = False

    numProcs = int(Env.get("RSleighNumProcs", "1"))
    workers = []
    sleighID = int(Env["RSleighID"])  # only used for non-sleigh plugin
    for i in xrange(numProcs):
        outfile = Env.get("RSleighWorkerOut")
        if outfile:
            prefix = "sleigh_ride_%d_" % i
            fd, outfile = tempfile.mkstemp(suffix=".txt", prefix=prefix, dir=logDir, text=True)
            out = os.fdopen(fd, "w")
        else:
            outfile = _NULFILE
            out = open(outfile, "w")

        Env["RSleighLogFile"] = outfile

        if newProtocol:
            worker_id = nws.fetch("worker_ids")
        else:
            worker_id = str(i + sleighID)

        Env["RSleighRank"] = worker_id
        Env["RSleighLocalID"] = str(i)

        try:
            if sys.platform.startswith("win"):
                p = subprocess.Popen(argv, stdin=open(tmpname), stdout=out, stderr=subprocess.STDOUT)
                wpid = int(p._handle)
            else:
                p = subprocess.Popen(argv, stdin=open(tmpname), stdout=out, stderr=subprocess.STDOUT, preexec_fn=setpg)
                wpid = p.pid

                # attempt to make the child process a process group leader
                try:
                    os.setpgid(wpid, 0)
                except:
                    pass
        except OSError, e:
            print >> sys.stderr, "error executing command:", argv
            if not os.path.exists(rprog):
                print >> sys.stderr, rprog, "does not exist"
            raise e

        print "started worker %s, pid: %s" % (worker_id, wpid)
        workers.append((p, wpid, worker_id))