Example #1
0
 def Start(self, mapQueueConn, rootQueueName='', ta=0):
     self._CheckConnQueueName_(mapQueueConn)
     n = 0
     secure = False
     all = len(self._m_mapQueueConn_)
     if all > 1:
         all += 1
     if rootQueueName is None:
         rootQueueName = ''
     rootQueueName = rootQueueName.strip()
     if len(rootQueueName) == 0:
         appName = os.path.basename(__file__)
         dot = appName.rindex('.')
         if dot == -1:
             rootQueueName = appName
         else:
             rootQueueName = appName[0:dot]
     vDbFullName = []
     mcc = [[]]
     for key in self._m_mapQueueConn_.keys():
         mcc[0].append(self._m_mapQueueConn_[key])
         if not secure and mcc[0][
                 n].EncrytionMethod == tagEncryptionMethod.TLSv1:
             secure = True
         vDbFullName.append(self._m_rs_.QueueDir + key)
         n += 1
     if all > 1:
         last = CConnectionContext('127.0.0.1', 0xffff, 'UReplication', '',
                                   tagEncryptionMethod.NoEncryption)
         if secure:
             last.EncrytionMethod = tagEncryptionMethod.TLSv1
         mcc[0].append(last)
         vDbFullName.append(self._m_rs_.QueueDir + rootQueueName)
     self._EndProcess_(vDbFullName, secure, mcc, ta)
     return self._m_pool_.ConnectedSockets > 0
Example #2
0
 def Start(self, mapQueueConn, rootQueueName='', ta=0):
     self._CheckConnQueueName_(mapQueueConn)
     n = 0
     secure = False
     all = len(self._m_mapQueueConn_)
     if all > 1:
         all += 1
     if rootQueueName is None:
         rootQueueName = ''
     rootQueueName = rootQueueName.strip()
     if len(rootQueueName) == 0:
         appName = os.path.basename(__file__)
         dot = appName.rindex('.')
         if dot == -1:
             rootQueueName = appName
         else:
             rootQueueName = appName[0:dot]
     vDbFullName = []
     mcc =[[]]
     for key in self._m_mapQueueConn_.keys():
         mcc[0].append(self._m_mapQueueConn_[key])
         if not secure and mcc[0][n].EncrytionMethod == tagEncryptionMethod.TLSv1:
             secure = True
         vDbFullName.append(self._m_rs_.QueueDir + key)
         n += 1
     if all > 1:
         last = CConnectionContext('127.0.0.1', 0xffff, 'UReplication', '', tagEncryptionMethod.NoEncryption)
         if secure:
             last.EncrytionMethod = tagEncryptionMethod.TLSv1
         mcc[0].append(last)
         vDbFullName.append(self._m_rs_.QueueDir + rootQueueName)
     self._EndProcess_(vDbFullName, secure, mcc, ta)
     return self._m_pool_.ConnectedSockets > 0
Example #3
0
    def getConfig():
        if CConfig._config_:
            return CConfig._config_
        config = CConfig()

        # load the following settings from a configuration file
        config.m_main_threads = 4

        # master
        config.m_master_default_db = 'sakila.db'
        config.m_nMasterSessions = 1  # one session enough for SQLite
        config.m_ccMaster = CConnectionContext('localhost', 20901, 'root',
                                               'Smash123')

        config.m_slave_default_db = 'sakila.db'
        cc = CConnectionContext('104.154.160.127', 20901, 'root', 'Smash123')
        config.m_vccSlave.append(cc)

        # treat master as last salve
        config.m_vccSlave.append(config.m_ccMaster)

        config.m_slave_queue_name = 'db_sakila'
        config.m_sessions_per_host = 3
        config.m_slave_threads = 2

        # middle tier
        # test certificate and private key files are located at the directory ../socketpro/bin
        if CUQueue.DEFAULT_OS == tagOperationSystem.osWin:
            config.m_store_or_pfx = 'intermediate.pfx'
        else:
            config.m_cert = 'intermediate.cert.pem'
            config.m_key = 'intermediate.key.pem'
        config.m_password_or_subject = 'mypassword'

        # a list of cached tables for front applications
        config.m_vFrontCachedTable.append("actor")
        config.m_vFrontCachedTable.append("language")
        config.m_vFrontCachedTable.append("country")
        config.m_vFrontCachedTable.append("film_actor")

        CConfig._config_ = config
        return config
Example #4
0
from loading_balance.lb_worker.piworker import PiWorker
from spa.clientside import CConnectionContext, CSocketPool
import sys, multiprocessing

print('Worker: tell me load balance host address:')
cc = CConnectionContext(sys.stdin.readline().strip(), 20901, "lb_worker",
                        "pwdForlbworker")
with CSocketPool(PiWorker) as spPi:
    if not spPi.StartSocketPool(cc, 1, multiprocessing.cpu_count()):
        print('No connection to ' + cc.Host)
    print('Press ENTER key to kill the demo ......')
    line = sys.stdin.readline()
Example #5
0
from spa import CServerError as Se
from spa import Pair
from spa.clientside import CSocketPool, CConnectionContext, CMysql, CUQueue, CSocketError
import datetime

# prepare two large texts
g_wstr = u''
while len(g_wstr) < 128 * 1024:
    g_wstr += u'近日,一则极具震撼性的消息,在中航工业的干部职工中悄然流传:中航工业科技委副主任、总装备部先进制造技术专家组组长、原中航工业制造所所长郭恩明突然失联。老郭突然失联,在中航工业和国防科技工业投下了震撼弹,也给人们留下了难以解开的谜团,以正面形象示人的郭恩明,为什么会涉足谍海,走上不归路,是被人下药被动失足?还是没能逃过漂亮“女间谍“的致命诱惑?还是仇视社会主义,仇视航空工业,自甘堕落与国家与人民为敌?'
g_astr = ''
while len(g_astr) < 256 * 1024:
    g_astr += 'The epic takedown of his opponent on an all-important voting day was extraordinary even by the standards of the 2016 campaign -- and quickly drew a scathing response from Trump.'

with CSocketPool(CMysql) as spMysql:
    print('Remote async mysql server host: ')
    cc = CConnectionContext(sys.stdin.readline(), 20902, 'root', 'Smash123')
    ok = spMysql.StartSocketPool(cc, 1)
    mysql = spMysql.AsyncHandlers[0]
    if not ok:
        print('No connection error code = ' + str(mysql.Socket.ErrorCode))
        exit(0)

    def TestCreateTables():
        return [
            mysql.execute(
                'CREATE DATABASE IF NOT EXISTS mysqldb character set utf8 collate utf8_general_ci;USE mysqldb'
            ),
            mysql.execute(
                'CREATE TABLE IF NOT EXISTS company(ID bigint PRIMARY KEY NOT NULL,name CHAR(64)'
                'NOT NULL,ADDRESS varCHAR(256)not null,Income decimal(15,2)not null)'
            ),
Example #6
0
from hello_world.client.asynchelloworld import CHelloWorld
from spa import CScopeUQueue as Sb, CServerError
from spa.clientside import CSocketPool, CConnectionContext, CSocketError
from consts import hwConst
from msstruct import CMyStruct
import sys

with CSocketPool(CHelloWorld) as sp:
    cc = CConnectionContext('localhost', 20901, 'PythonUser', 'TooMuchSecret')
    # sp.QueueName = 'pqueue'  # turn on client message queue for backing up requests
    if not sp.StartSocketPool(cc, 1):
        print('Cannot connect to server with error message: ' +
              sp.Sockets[0].ErrMsg)
    else:
        hw = sp.Seek()
        ms = CMyStruct.MakeOne()  # make a complex structure
        print(ms)
        # process requests one by one synchronously -- three round trips
        try:
            print(hw.say_hello(u'Jack', u'Smith'))
            hw.sleep(4000)
            print(hw.echo(ms))
        except CServerError as ex:  # an exception from remote server
            print(ex)
        except CSocketError as ex:  # a communication error
            print(ex)
        except Exception as ex:
            # invalid parameter, bad de-serialization, and so on
            print('Unexpected error: ' + str(ex))

        print('')
Example #7
0
from spa.clientside import CConnectionContext, CClientSocket, UFuture

from sharedstruct import *
from webasynchandler import CWebAsyncHandler

# CA file is located at the directory ../socketpro/bin
if CUQueue.DEFAULT_OS != tagOperationSystem.osWin:
    CClientSocket.SSL.SetVerifyLocation('ca.cert.pem')

with CMasterPool(CWebAsyncHandler, '', False) as sp:
    cc = CConnectionContext(input('Remote middle tier host: '), 20911,
                            'PythonUser', 'TooMuchSecret',
                            tagEncryptionMethod.TLSv1)

    def ssl_server_authentication(pool, cs):
        cert = cs.UCert
        errCode, errMsg = cert.Verify()
        return errCode == 0

    sp.DoSslServerAuthentication = ssl_server_authentication

    ok = sp.StartSocketPool(cc, 4, 1)
    if not ok:
        input(
            'Failed in connecting to remote middle tier server, and press any key to close the application ......'
        )
        exit(-1)
    cache = sp.Cache
    filter = input('Sakila.payment filter: ')
    handler = sp.Seek()
    ok = handler.GetMasterSlaveConnectedSessions(
Example #8
0
from piworker import PiWorker
from consts import piConst
from spa.clientside import CConnectionContext, CSocketPool
import sys

cc = CConnectionContext("localhost", 20901, "lb_worker", "pwdForlbworker")
with CSocketPool(PiWorker) as spPi:
    ok = spPi.StartSocketPool(cc, 1)
    print('Press ENTER key to shutdown the demo application ......')
    line = sys.stdin.readline()
Example #9
0
        return False #not processed
    aq.ResultReturned = cbResultReturned
    def cbDequeue(aq, messageCount, fileSize, messages, bytes):
        s = 'Total message count=' + str(messageCount) + ', queue file size=' + str(fileSize) + ', messages dequeued=' + str(messages) + ', message bytes dequeued=' + str(bytes)
        print(s)
        if messageCount > 0:
            # there are more messages left at server queue, we re-send a request to dequeue
            aq.Dequeue(TEST_QUEUE_KEY, aq.LastDequeueCallback)
    print('Going to dequeue messages ......')
    aq.Dequeue(TEST_QUEUE_KEY, cbDequeue)

    #optionally, add one extra to improve processing concurrency at both client and server sides for better performance and through-output
    aq.Dequeue(TEST_QUEUE_KEY, cbDequeue)

with CSocketPool(CAsyncQueue) as spAq:
    print('Remote async queue server host: ')
    cc = CConnectionContext(sys.stdin.readline(), 20901, 'PythonUser', 'TooMuchSecret')
    ok = spAq.StartSocketPool(cc, 1, 1)
    aq = spAq.AsyncHandlers[0]
    if not ok:
        print('No connection error code = ' + str(aq.AttachedClientSocket.ErrorCode))
        exit(0)

    #Optionally, you can enqueue messages with transaction style by calling the methods StartQueueTrans and EndQueueTrans in pair
    #aq.StartQueueTrans(TEST_QUEUE_KEY, lambda errCode: print('errCode=' + str(errCode)))
    TestEnqueue(aq)
    #aq.EndQueueTrans()
    TestDequeue(aq)

    print('Press any key to close the application ......')
    sys.stdin.readline()
Example #10
0
    def cbDequeue(sender, messageCount, fileSize, messages, bytes):
        s = 'Total message count=' + str(messageCount) + ', queue file size=' + str(fileSize) + ', messages dequeued='\
            + str(messages) + ', message bytes dequeued=' + str(bytes)
        print(s)
        if messageCount > 0:
            # there are more messages left at server queue, keep on dequeuing
            sender.Dequeue(TEST_QUEUE_KEY, sender.LastDequeueCallback)
    print('Going to dequeue messages ......')
    # optionally, add one extra to improve processing concurrency at both client and server sides
    # for better performance and through-output
    if not (aq.Dequeue(TEST_QUEUE_KEY, cbDequeue) and aq.Dequeue(TEST_QUEUE_KEY, cbDequeue)):
        aq.throw(CAsyncQueue.idDequeue)

with CSocketPool(CAsyncQueue) as spAq:
    print('Remote async queue server host: ')
    cc = CConnectionContext(sys.stdin.readline(), 20901, 'SomeUserId', 'APassword')
    ok = spAq.StartSocketPool(cc, 1)
    aq = spAq.AsyncHandlers[0]
    if not ok:
        print('No connection error code = ' + str(aq.Socket.ErrorCode))
        exit(0)
    try:
        # Optionally, you can enqueue messages with transaction style by calling the methods
        # StartQueueTrans and EndQueueTrans in pair
        f0 = aq.startQueueTrans(TEST_QUEUE_KEY)
        TestEnqueue(aq)
        f1 = aq.endQueueTrans()
        fut0 = aq.flushQueue(TEST_QUEUE_KEY)
        print('StartQueueTrans return error code: ' + str(f0.result()))
        print('EndQueueTrans return error code: ' + str(f1.result()))
        print('Server queue info: ' + str(fut0.result()))
Example #11
0
with CSocketPool(CSqlite) as sp:
    cycles = 10000
    sessions_per_host = 2
    threads = 1
    vHost = ['localhost', 'ws-yye-1']
    channels = sessions_per_host * len(vHost)
    sp.QueueName = 'ar_python'  # set a local queue to backup requests for auto fault recovery
    mcc = [[0 for i in range(channels)] for i in range(threads)]
    m = 0
    while m < threads:
        n = 0
        while n < sessions_per_host:
            j = 0
            while j < len(vHost):
                mcc[m][n * sessions_per_host + j] = CConnectionContext(
                    vHost[j], 20901, 'root', 'Smash123')
                j += 1
            n += 1
        m += 1
    ok = sp.StartSocketPoolEx(mcc)
    if not ok:
        s = input(
            'No connection to SQL stream server and press ENTER key to shut down the application ......'
        )
        exit(-1)
    handler = sp.SeekByQueue()
    sql = 'SELECT max(amount), min(amount), avg(amount) FROM payment'
    filter = input('Input a filter for payment_id')
    if len(filter) > 0:
        sql += (' WHERE ' + filter)
    v = sp.AsyncHandlers