def get_fast_transport(endpoint, timeout=5000): """ 采用cython实现的transport :param endpoint: :param timeout: :return: """ global _fast_transport if not _fast_transport: if endpoint.find(":") != -1: hostport = endpoint.split(":") host = hostport[0] port = int(hostport[1]) unix_socket = None else: host = None port = None unix_socket = endpoint socket = TSocket(host=host, port=port, unix_socket=unix_socket) socket.setTimeout(timeout) socket.open() _fast_transport = TCyFramedTransport( socket, maxIdleTime=1200) # 20分钟没有写数据,则重新打开transport return _fast_transport
def create_client( client_klass, host=None, port=None, client_type=None, path=None, timeout=None, ): """ Given a thrift client class, and a host/port return a client using HeaderTransport """ from thrift.transport.TSocket import TSocket from thrift.protocol.THeaderProtocol import THeaderProtocol sock = TSocket(host=host, port=port, unix_socket=path) sock.setTimeout(timeout) protocol = THeaderProtocol( sock, client_types=[client_type] if client_type else None, # We accept the same as our inital send_ client_type=client_type, # Used for the inital send_ ) sock.open() return client_klass(protocol)
def get_fast_transport(endpoint, timeout=5000): """ 采用cython实现的transport :param endpoint: :param timeout: :return: """ global _fast_transport if not _fast_transport: if endpoint.find(":") != -1: hostport = endpoint.split(":") host = hostport[0] port = int(hostport[1]) unix_socket = None else: host = None port = None unix_socket = endpoint socket = TSocket(host=host, port=port, unix_socket=unix_socket) socket.setTimeout(timeout) socket.open() _fast_transport = TCyFramedTransport(socket, maxIdleTime=1200) # 20分钟没有写数据,则重新打开transport return _fast_transport
def connection_to_lb(self): if self.unix_socket: info_logger.info("Prepare open a socket to lb: %s, pid: %s", self.unix_socket, self.pid) else: info_logger.info("Prepare open a socket to lb: %s:%s, pid: %s", self.host, self.port, self.pid) # 1. 创建一个到lb的连接,然后开始读取Frame, 并且返回数据 socket = TSocket(host=self.host, port=self.port, unix_socket=self.unix_socket) try: if not socket.isOpen(): socket.open() socket.setTimeout(5000) # 出现异常,会自己重启 except TTransportException: info_logger.info("Sleep %ds for another retry, pid: %s", self.reconnect_interval, self.pid) time.sleep(self.reconnect_interval) print_exception(info_logger) if self.reconnect_interval < 4: self.reconnect_interval *= 2 return # 2. 连接创建成功 self.reconnect_interval = 1 self.socket = socket # 每次建立连接都重新构建 self.queue = gevent.queue.Queue() self.connection_ok = True info_logger.info("Begin request loop....") # 3. 在同一个transport上进行读写数据 transport = TCyFramedTransportEx(socket) # # 关注 transport的接口: # flush_frame_buff # read_frame # g1 = gevent.spawn(self.loop_reader, transport, self.queue) g2 = gevent.spawn(self.loop_writer, transport, self.queue) g3 = gevent.spawn(self.loop_hb_detect, transport) gevent.joinall([g1, g2, g3]) # 4. 关闭连接 try: # 什么情况下会关闭呢? 连接断开了, print time.strftime(ISOTIMEFORMAT, time.localtime()), "Trans Closed, queue size: ", self.queue.qsize(), ", pid: ", self.pid self.queue = None self.socket = None transport.close() # 关闭transport(而且transport也不会继续复用) except: print_exception(info_logger) pass
def _create_conn(self): self._host_index += 1 self._host_index %= len(self._host_list) host = self._host_list[self._host_index] parts = host.split(':') host = parts[0] port = int(parts[1]) conn = TSocket(host, port) conn.setTimeout(self._time_out) conn.open() return conn
def open(self): TSocket.open(self) self.write("CONNECT %s:%d HTTP/1.1\r\nHost: %s:%d\r\n\r\n" % (self.remote_host, self.remote_port, self.remote_host, self.remote_port)) res = self.read(4096) try: status = res.split()[1] if status != '200': self.close() raise TTransportException( TTransportException.NOT_OPEN, "Error response from proxy server: %s" % res) except IndexError: self.close() raise TTransportException( TTransportException.NOT_OPEN, "Error response from proxy server: %s" % res)
def test_isOpen_checks_for_readability(self): # https://docs.python.org/3/library/socket.html#notes-on-socket-timeouts # https://docs.python.org/3/library/socket.html#socket.socket.settimeout timeouts = [ None, # blocking mode 0, # non-blocking mode 1.0, # timeout mode ] for timeout in timeouts: acc = ServerAcceptor(TServerSocket(port=0)) acc.start() sock = TSocket(host="localhost", port=acc.port) self.assertFalse(sock.isOpen()) sock.open() sock.setTimeout(timeout) # the socket shows as open immediately after connecting self.assertTrue(sock.isOpen()) # and remains open during usage sock.write(b"hello") self.assertTrue(sock.isOpen()) while True: try: sock.read(5) except TTransportException as exc: if exc.inner.errno == errno.EAGAIN: # try again when we're in non-blocking mode continue raise break self.assertTrue(sock.isOpen()) # once the server side closes, it no longer shows open acc.client.close( ) # this also blocks until the other thread is done acc.close() self.assertFalse(sock.isOpen()) sock.close()
def test_it_works(self): processor_factory = mock.Mock() mock_processor = mock.Mock() processor_factory.get_processor.return_value = mock_processor proto_factory = FProtocolFactory(TJSONProtocolFactory()) server_trans = TServerSocket(host='localhost', port=5536) server = FSimpleServer(processor_factory, server_trans, proto_factory) thread = Thread(target=lambda: server.serve()) thread.start() time.sleep(0.1) transport = TSocket(host='localhost', port=5536) transport.open() transport.write(bytearray([0, 0, 0, 3, 1, 2, 3])) transport.flush() time.sleep(0.1) server.stop() processor_factory.get_processor.assert_called_once_with(mock.ANY) mock_processor.process.assert_called_with(mock.ANY, mock.ANY)
def getP13n(self, timeout=2, useCurlIfAvailable=True): if self.socketHost != None: transport = TSocket(self.socketHost, self.socketPort) # transport.setSendTimeout(self.socketSendTimeout) # transport.setRecvTimeout(self.socketRecvTimeout) client = P13nService.Client(TBinaryProtocol(transport)) transport.open() return client _transport = THttpClient.THttpClient(self.uri) #base64.b64encode(bytes('your string', 'utf-8')) _transport.setCustomHeaders({ 'Authorization': "Basic " + base64.b64encode( bytes((self.p13n_username + ':' + self.p13n_password).encode('utf-8'))) }) _client = P13nService.Client( TCompactProtocol.TCompactProtocol(_transport)) _transport.open() return _client
class ClientEx: def __init__(self, address): self.tr = TSocket(address[0], address[1]) self.protocol = TBinaryProtocol(self.tr) self.client = Client(self.protocol) self.tr.open() def close(self): self.tr.close() def wallet_balance_get(self, pub_key_bytes): return self.client.WalletBalanceGet(pub_key_bytes) def __fee(self, value): sign = 0 if value < 0.0: value = 1 value = abs(value) expf = 0 if value != 0.0: expf = math.log10(value) if expf >= 0: expf = expf + .5 else: expf = expf - .5 expi = int(expf) value /= math.pow(10, expi) if value >= 1.0: value *= 0.1 expi = expi + 1 exp = expi + 18 if exp < 0 or exp > 28: print('exponent value {0} out of range [0, 28]'.format(exp)) return -1 frac = round(value * 1024) return sign * 32768 + exp * 1024 + frac def transfer_coins(self, integral, fraction, fee, keys): res = self.client.TransactionFlow(self.create_transaction(integral, fraction, fee, keys)) print(res) def create_transaction(self, integral, fraction, fee, keys): tr = Transaction() tr.id = self.client.WalletTransactionsCountGet(keys.public_key_bytes).lastTransactionInnerId + 1 tr.source = keys.public_key_bytes tr.target = keys.target_public_key_bytes tr.amount = Amount() tr.amount.integral = integral tr.amount.fraction = fraction tr.currency = 1 tr.fee = AmountCommission() tr.fee.commission = self.__fee(fee) serial_transaction = pack('=6s32s32slqhbb', # '=' - without alignment' bytearray(tr.id.to_bytes(6, 'little')), # 6s - 6 byte InnerID (char[] C Type) tr.source, # 32s - 32 byte source public key (char[] C Type) tr.target, # 32s - 32 byte target pyblic key (char[] C Type) tr.amount.integral, # i - 4 byte integer(int C Type) tr.amount.fraction, # q - 8 byte integer(long long C Type) tr.fee.commission, # h - 2 byte integer (short C Type) tr.currency, # b - 1 byte integer (signed char C Type) 0 # b - 1 byte userfield_num ) signing_key = ed25519.SigningKey(keys.private_key_bytes) sign = signing_key.sign(serial_transaction) tr.signature = sign return tr def deploy_smart_contract(self, code, fee, keys): res = self.client.TransactionFlow(self.create_transaction_with_smart_contract(code, fee, keys)) print(res) def create_transaction_with_smart_contract(self, code, fee, keys): if code == "": code = 'import com.credits.scapi.annotations.*; import com.credits.scapi.v0.*; public class ' \ 'MySmartContract extends SmartContract { public MySmartContract() {} public String hello2(String ' \ 'say) { return \"Hello\" + say; } }'; tr = Transaction() tr.id = self.client.WalletTransactionsCountGet(keys.public_key_bytes).lastTransactionInnerId + 1 tr.source = keys.public_key_bytes tr.target = keys.target_public_key_bytes tr.amount = Amount() tr.amount.integral = 0 tr.amount.fraction = 0 tr.currency = 1 tr.fee = AmountCommission() tr.fee.commission = self.__fee(fee) serial_transaction = pack('=6s32s32slqhbb', # '=' - without alignment' bytearray(tr.id.to_bytes(6, 'little')), # 6s - 6 byte InnerID (char[] C Type) tr.source, # 32s - 32 byte source public key (char[] C Type) tr.target, # 32s - 32 byte target pyblic key (char[] C Type) tr.amount.integral, # i - 4 byte integer(int C Type) tr.amount.fraction, # q - 8 byte integer(long long C Type) tr.fee.commission, # h - 2 byte integer (short C Type) tr.currency, # b - 1 byte integer (signed char C Type) 1 # b - 1 byte userfield_num ) target = pack('=6s', bytearray(tr.id.to_bytes(6, 'little'))) byte_code = self.client.SmartContractCompile(code) if byte_code.status.code == 0: for bco in byte_code.byteCodeObjects: target = target + bco.byteCode else: print(byte_code.Status.Message) return 'compile error' tr.smartContract = SmartContractInvocation() tr.smartContract.smartContractDeploy = SmartContractDeploy() tr.smartContract.smartContractDeploy.sourceCode = code tr.smartContract.ForgetNewState = False tr.target = hashlib.blake2s(target).hexdigest() uf = bytearray(b'\x11\x00\x01\x00\x00\x00\x00\x015\x00\x02\x12\x00\x00\x00\x00\x15\x00\x03\x11\x00\x00\x00\x00\x02\x00\x04\x00\x12\x00\x05\x11\x00\x01') uf = uf + pack('=6s', self.reverse(len(code))) uf = uf + bytearray(code.encode()) uf = uf + bytearray(b'\x15\x00\x02\x12') uf = uf + self.reverse(len(byte_code.byteCodeObjects)) for bco in byte_code.byteCodeObjects: uf = uf + b'1101' uf = uf + self.reverse(len(bco.name)) uf = uf + bytearray(bco.name.encode()) uf = uf + b'1102' uf = uf + self.reverse(len(bco.byteCode)) uf = uf + bco.byteCode nbco = ByteCodeObject() nbco.name = bco.name nbco.byteCode = bco.byteCode tr.smartContract.smartContractDeploy.byteCodeObjects = [nbco] uf = uf + b'\x00' uf = uf + b'\x11\x00\x03\x00\x00\x00\x00\x08\x00\x04\x00\x00\x00\x00\x00' uf = uf + b'\x00' serial_transaction = serial_transaction + self.reverse(len(uf)) serial_transaction = serial_transaction + uf signing_key = ed25519.SigningKey(keys.private_key_bytes) sign = signing_key.sign(serial_transaction) tr.signature = sign return tr def reverse(self, a): a = a.to_bytes(6, 'little') a = bytearray(a) a.reverse() return a
import sys sys.path.append( os.path.abspath(os.path.dirname(os.path.abspath( os.path.dirname(__file__))))) sys.path.append( os.path.abspath( os.path.abspath(os.path.dirname(__file__)) + os.sep + "gen-py")) import time from example.Example import Client from thrift import Thrift from thrift.transport.TSocket import TSocket from thrift.transport.TTransport import TBufferedTransport from thrift.protocol.TBinaryProtocol import TBinaryProtocolAccelerated try: transport = TSocket('127.0.0.1', 20000) transport = TBufferedTransport(transport) protocol = TBinaryProtocolAccelerated(transport) client = Client(protocol) transport.open() start = time.time() for i in range(10000): result = client.add(0, i) print(time.time() - start) except Thrift.TException as ex: print("%s" % (ex.message))
class ClientEx: def __init__(self, address): self.tr = TSocket(address[0], address[1]) self.protocol = TBinaryProtocol(self.tr) self.client = Client(self.protocol) self.tr.open() def close(self): self.tr.close() def wallet_balance_get(self, pub_key_bytes): return self.client.WalletBalanceGet(pub_key_bytes) def double_to_fee(self, value): fee_comission = 0 a = True if value < 0.: fee_comission += 32768 else: fee_comission += (32768 if value < 0. else 0) value = math.fabs(value) expf = (0. if value == 0. else math.log10(value)) expi = int(expf + 0.5 if expf >= 0. else expf - 0.5) value /= math.pow(10, expi) if value >= 1.: value *= 0.1 expi += 1 fee_comission += int(1024 * (expi + 18)) fee_comission += int(value * 1024) return fee_comission def __fee(self, value): sign = 0 if value < 0.0: value = 1 value = abs(value) expf = 0 if value != 0.0: expf = math.log10(value) if expf >= 0: expf = expf + .5 else: expf = expf - .5 expi = int(expf) value /= math.pow(10, expi) if value >= 1.0: value *= 0.1 expi = expi + 1 exp = expi + 18 if exp < 0 or exp > 28: print('exponent value {0} out of range [0, 28]'.format(exp)) return -1 frac = round(value * 1024) return sign * 32768 + exp * 1024 + frac def transfer_coins(self, integral, fraction, fee, keys): res = self.client.TransactionFlow( self.create_transaction(integral, fraction, fee, keys)) print(res) def create_transaction(self, integral, fraction, fee, keys): tr = Transaction() tr.id = self.client.WalletTransactionsCountGet( keys.public_key_bytes).lastTransactionInnerId + 1 tr.source = keys.public_key_bytes tr.target = keys.target_public_key_bytes tr.amount = Amount() tr.amount.integral = integral tr.amount.fraction = fraction tr.currency = 1 tr.fee = AmountCommission() tr.fee.commission = self.__fee(fee) serial_transaction = pack( '=6s32s32slqhbb', # '=' - without alignment' bytearray(tr.id.to_bytes( 6, 'little')), # 6s - 6 byte InnerID (char[] C Type) tr.source, # 32s - 32 byte source public key (char[] C Type) tr.target, # 32s - 32 byte target pyblic key (char[] C Type) tr.amount.integral, # i - 4 byte integer(int C Type) tr.amount.fraction, # q - 8 byte integer(long long C Type) tr.fee.commission, # h - 2 byte integer (short C Type) tr.currency, # b - 1 byte integer (signed char C Type) 0 # b - 1 byte userfield_num ) signing_key = ed25519.SigningKey(keys.private_key_bytes) sign = signing_key.sign(serial_transaction) tr.signature = sign return tr def deploy_smart_contract(self, code, fee, keys): res = self.client.TransactionFlow( self.create_transaction_with_smart_contract(code, fee, keys)) print(res) def createContractAddress(self, source, tId, contract): tmpBytes = bytearray() tmpBytes.extend(source) tmpBytes.extend(tId) for a in contract.smartContractDeploy.byteCodeObjects: tmpBytes.extend(a.byteCode) res = hashlib.blake2s() res.update(tmpBytes) return res.digest() def normalizeCode(self, javaText): javaText = javaText.replace('\r', ' ').replace('\t', ' ').replace('{', ' {') while ' ' in javaText: javaText = javaText.replace(' ', ' ') return javaText def compile_smart(self, contract_body): if self.client == None: return None res = self.client.SmartContractCompile(contract_body) return res def create_transaction_with_smart_contract(self, code, fee, keys): tr = Transaction() contract = SmartContractInvocation() contract.smartContractDeploy = SmartContractDeploy() if code == "": code = 'import com.credits.scapi.annotations.*; import com.credits.scapi.v0.*; public class ' \ 'MySmartContract extends SmartContract { public MySmartContract() {} public String hello2(String ' \ 'say) { return \"Hello\" + say; } }' contractText = self.normalizeCode(code) result = self.compile_smart(contractText) contract.smartContractDeploy.byteCodeObjects = result.byteCodeObjects tr.smartContract = contract tr.smartContract.smartContractDeploy.sourceCode = contractText tr.source = keys.public_key_bytes w = self.client.WalletTransactionsCountGet(tr.source) lastInnerId = bytearray( (w.lastTransactionInnerId + 1).to_bytes(6, 'little')) tr.id = int.from_bytes(lastInnerId, byteorder='little', signed=False) tr.target = self.createContractAddress(tr.source, lastInnerId, contract) tr.amount = Amount() tr.amount.integral = 0 tr.amount.fraction = 0 tr.balance = Amount() tr.balance.integral = 0 tr.balance.fraction = 0 tr.currency = 1 tr.fee = AmountCommission() tr.fee.commission = self.double_to_fee(fee) tr.userFields = "" ufNum1 = bytearray(b'\x01') contract.smartContractDeploy.hashState = "" contract.smartContractDeploy.tokenStandard = 0 contract.method = "" contract.params = [] contract.usedContracts = [] contract.forgetNewState = False transportOut = TMemoryBuffer() protocolOut = TBinaryProtocol(transportOut) contract.write(protocolOut) scBytes = transportOut.getvalue() sMap = '=6s32s32slqhb1s4s' + str( len(scBytes) ) + 's' #4s' + str(scriptLength) + 's4s' + str(codeNameLength) + 's4s' + str(codeLength) + 's' #len(userField_bytes) serial_transaction_for_sign = pack( sMap, #'=' - without alignment lastInnerId, #6s - 6 byte InnerID (char[] C Type) tr.source, #32s - 32 byte source public key (char[] C Type) tr.target, #32s - 32 byte target pyblic key (char[] C Type) tr.amount.integral, #i - 4 byte integer(int C Type) tr.amount.fraction, #q - 8 byte integer(long long C Type) tr.fee.commission, #h - 2 byte integer (short C Type) tr.currency, #b - 1 byte integer (signed char C Type) ufNum1, bytes(len(scBytes).to_bytes(4, byteorder="little")), scBytes) signing_key = ed25519.SigningKey( keys.private_key_bytes) # Create object for calulate signing tr.signature = signing_key.sign(serial_transaction_for_sign) return tr def reverse(self, a): a = a.to_bytes(6, 'little') a = bytearray(a) a.reverse() return a
from thrift.protocol.TBinaryProtocol import TBinaryProtocol from thrift.transport.TSocket import TSocket import base58 from api.API import Client publicKey = '5B3YXqDTcWQFGAqEJQJP3Bg1ZK8FFtHtgCiFLT5VAxpe' publicKeyBytes = base58.b58decode(publicKey) try: tr = TSocket('169.38.89.217', 9090) protocol = TBinaryProtocol(tr) client = Client(protocol) tr.open() balance = client.WalletBalanceGet(publicKeyBytes) print(balance) transactionGetResult = client.WalletTransactionsCountGet(publicKeyBytes) print(transactionGetResult) except: print("Oops. Unexpected error.")
class Dino(Dinosaur.Client, threading.Thread): def __init__(self, eggID=None, coords = Coordinate(0, 0)): self.eggID = eggID name = eggID is None and str(0) or str(DINO_COUNTER.next()) self.position = coords self.transport = TSocket(THRIFT_SERVER, THRIFT_PORT) self.protocol = TBinaryProtocol(self.transport) self.logger = logging.getLogger("Dino %s" % name) self.logger.setLevel(LOG_LEVEL) ch = logging.StreamHandler() ch.setLevel(LOG_LEVEL) formatter = logging.Formatter(LOG_FORMAT) ch.setFormatter(formatter) self.logger.addHandler(ch) self.counters = {'actions': 0, 'moves': 0, 'calories_found': 0, 'calories_burnt': 0, 'eggs': 0, 'looks': 0} self.logger.info("New Dino. eggID=%s, name=%s, position=%s" % (self.eggID, name, str(self.position))) Dinosaur.Client.__init__(self, self.protocol) threading.Thread.__init__(self, name=name) def look(self, direction): self.counters['actions'] += 1 self.counters['looks'] += 1 self.counters['calories_burnt'] += self.state.lookCost lr = Dinosaur.Client.look(self, direction) self.logger.debug(lr) if lr.succeeded: self.state = lr.myState if lr.succeeded and len(lr.thingsSeen) != 0: distances = [i.coordinate.distance(self.position) for i in lr.thingsSeen] closest, farest = minmax(distances) MAP_MANAGER.addSightings(self.position, direction, farest, lr.thingsSeen) self.logger.info("LOOK OK (%s): %d things seen. Closest/Farest %d/%d," % (Direction._VALUES_TO_NAMES[direction], len(lr.thingsSeen), closest, farest)) else: self.logger.warning("LOOK KO (%s): seen nothing." % Direction._VALUES_TO_NAMES[direction]) return lr.succeeded def move(self, dir): mr = Dinosaur.Client.move(self, dir) self.logger.debug(mr) if mr.succeeded: self.logger.info("MOVE OK: %s. %s" % (Direction._VALUES_TO_NAMES[dir], mr.message)) t = Direction._RELATIVE_COORDINATES[dir] self.position += Coordinate(t[0], t[1]) self.state = mr.myState self.counters['actions'] += 1 self.counters['moves'] += 1 self.counters['calories_burnt'] += self.state.moveCost return True else: self.logger.warning("MOVE KO: %s. %s" % (Direction._VALUES_TO_NAMES[dir], mr.message)) direction = choice(list(set(Direction._VALUES_TO_NAMES.keys()) - set([dir,]))) self.move(direction) self.look(direction) return False def moveTo(self, coords): old_pos = deepcopy(self.position) old_cal = self.state.calories directions = vectorToDirections(deepcopy(coords) - self.position) self.logger.info("Will move to %s. Directions: %s" % (coords, [Direction._VALUES_TO_NAMES[i] for i in directions])) moves = list() for d in directions: moves.append(self.move(d)) moveto_successful = reduce(lambda x, y: x and y, moves) if not moveto_successful: break cal_found = (self.state.calories - old_cal) - (len(directions) * self.state.moveCost) # Bilan - Known losses msg = "MOVETO %%s. %s -> %s. Calories gained %d." % (old_pos, self.position, self.state.calories - old_cal) if cal_found > 0: self.counters['calories_found'] += cal_found if cal_found > 0 and moveto_successful: self.logger.info(msg % "OK") else: self.logger.warning(msg % ("%s (m=%s, f=%s)" % ("KO", moveto_successful and "OK" or "KO", cal_found > 0 and "OK" or "KO"),)) return cal_found > 0 and moveto_successful def growIfWise(self): if self.state.size < 2 \ or self.state.growCost < 0.3 * self.state.calories: gs = self.grow() self.logger.debug(gs) msg = "GROW %%s: %s" % gs.message if gs.succeeded: self.state = gs.myState self.counters['actions'] += 1 self.counters['calories_burnt'] += self.state.growCost self.logger.info(msg % "OK") else: self.logger.warning(msg % "KO") self.logger.info("STATE: %s" % self.state) def layIfWise(self): expected_calories_cost = self.state.eggCost + 1.2*OFFSPRING_DONATION + 5*self.state.moveCost + self.state.lookCost if self.state.size > 5 and expected_calories_cost < 0.5 * self.state.calories: self.logger.info("Laying offspring!") direction = choice([0, 2, 4, 6]) er = self.egg(direction, OFFSPRING_DONATION) if er.succeeded: self.logger.info("Successfully layed an egg!") self.state = er.parentDinoState self.logger.info("STATE: %s" % self.state) rc = Direction._RELATIVE_COORDINATES[direction] p = self.position + Coordinate(rc[1], rc[0]) EGG_POOL.add((er.eggID, p.column, p.row)) new_dir = choice(list(set([0, 2, 4, 6]) - set([direction,]))) # Stupidly go away... for i in range(5): self.move(new_dir) self.look(new_dir) self.counters['actions'] += 1 self.counters['eggs'] += 1 self.counters['calories_burnt'] += expected_calories_cost self.logger.info("LAY: %s" % er.message) def showPMReport(self): self.logger.info("PMR: size=%d" % self.state.size) self.logger.info("PMR: calories=%d, burnt=%d, found=%d, ratio=%f" % (self.state.calories, self.counters['calories_burnt'], self.counters['calories_found'], self.counters['calories_found'] != 0 and self.counters['calories_burnt']/self.counters['calories_found'] or -1)) self.logger.info("PMR: moves=%d, looks=%d, eggs=%d, actions=%d" % (self.counters['moves'], self.counters['looks'], self.counters['eggs'], self.counters['actions'])) def run(self): self.logger.info("Dino %s starting..." % self.name) self.transport.open() self.logger.info("Transport open.") if self.eggID is None: self.logger.info("I am the first egg. Registering.") rcr = self.registerClient(EMAIL, SCORE_NAME, ENTITY) self.logger.debug(rcr) for l in rcr.message.split("*"): self.logger.info("MESSAGE: %s" % l) self.species = rcr.species self.eggID = rcr.eggID self.logger.info("Got an eggID: %s" % self.eggID) self.state = self.hatch(self.eggID) self.logger.info("STATE: %s" % self.state) # Real algo here... try: while True: self.growIfWise() # Looking around direction = choice(range(8)) self.logger.info("Looking %s" % Direction._VALUES_TO_NAMES[direction]) self.look(direction) candidates = MAP_MANAGER.findClosest(self.position, EntityType.PLANT) if candidates is not None and len(candidates) > 0: if candidates[0].coordinate.distance(self.position)*self.state.moveCost > 0.5*self.state.calories: self.look(choice(list(set(range(8)) - getCone(direction, 2)))) candidates = MAP_MANAGER.findClosest(self.position, EntityType.PLANT) else: self.logger.warning("No candidates found. Moving on...") continue while candidates is not None and len(candidates) > 0: a = candidates.pop(0) self.logger.info("FOUND closest at %d, species='%s', size=%d" % (a.coordinate.distance(self.position), a.species, a.size)) if a.size > self.state.size + 2: self.logger.info("Seems big! Discarding") continue if not self.moveTo(a.coordinate): self.look(choice(range(8))) elif self.counters['moves'] % 10 == 0: self.logger.info("Random look") self.look(choice(range(8))) self.layIfWise() self.growIfWise() candidates = MAP_MANAGER.findClosest(self.position, EntityType.PLANT) if candidates is not None and len(candidates) > 0: if candidates[0].coordinate.distance(self.position)*self.state.moveCost > 0.5*self.state.calories: self.look(choice(list(set(range(8)) - getCone(direction, 2)))) candidates = MAP_MANAGER.findClosest(self.position, EntityType.PLANT) else: self.logger.warning("No candidates found. Moving on...") break except YouAreDeadException, e: self.logger.debug(e) self.logger.warning("DEAD: %s" % e.description) except GameOverException, e: global END_SCORE, BEST_SCORE self.logger.debug(e) self.logger.error("GameOver! won=%s, score=%d" % (e.wonGame, e.score)) self.logger.info("HighScoreTable:") for l in e.highScoreTable.splitlines(): self.logger.info(l) END_SCORE = e.score BEST_SCORE = [int(l.split()[-1]) for l in e.highScoreTable.splitlines() if SCORE_NAME in l][0]
def connection_to_lb(self): if self.unix_socket: info_logger.info("Prepare open a socket to lb: %s, pid: %s", self.unix_socket, self.pid) else: info_logger.info("Prepare open a socket to lb: %s:%s, pid: %s", self.host, self.port, self.pid) # 1. 创建一个到lb的连接,然后开始读取Frame, 并且返回数据 socket = TSocket(host=self.host, port=self.port, unix_socket=self.unix_socket) try: if not socket.isOpen(): socket.open() socket.setTimeout(5000) # 出现异常,会自己重启 except TTransportException: info_logger.info("Sleep %ds for another retry, pid: %s", self.reconnect_interval, self.pid) time.sleep(self.reconnect_interval) print_exception(info_logger) if self.reconnect_interval < 4: self.reconnect_interval *= 2 return # 2. 连接创建成功 self.reconnect_interval = 1 self.socket = socket # 每次建立连接都重新构建 self.queue = gevent.queue.Queue() self.connection_ok = True info_logger.info("Begin request loop....") # 3. 在同一个transport上进行读写数据 transport = TCyFramedTransportEx(socket) # # 关注 transport的接口: # flush_frame_buff # read_frame # g1 = gevent.spawn(self.loop_reader, transport, self.queue) g2 = gevent.spawn(self.loop_writer, transport, self.queue) g3 = gevent.spawn(self.loop_hb_detect, transport) gevent.joinall([g1, g2, g3]) # 4. 关闭连接 try: # 什么情况下会关闭呢? 连接断开了, print time.strftime(ISOTIMEFORMAT, time.localtime( )), "Trans Closed, queue size: ", self.queue.qsize( ), ", pid: ", self.pid self.queue = None self.socket = None transport.close() # 关闭transport(而且transport也不会继续复用) except: print_exception(info_logger) pass
# 14-8-19 # create by: snower import os import sys sys.path.append(os.path.abspath(os.path.dirname(os.path.abspath(os.path.dirname(__file__))))) sys.path.append(os.path.abspath(os.path.abspath(os.path.dirname(__file__))+os.sep+"gen-py")) import time from example.Example import Client from thrift import Thrift from thrift.transport.TSocket import TSocket from thrift.transport.TTransport import TBufferedTransport from thrift.protocol.TBinaryProtocol import TBinaryProtocolAccelerated try: transport = TSocket('127.0.0.1', 20000) transport = TBufferedTransport(transport) protocol = TBinaryProtocolAccelerated(transport) client = Client(protocol) transport.open() start = time.time() for i in range(10000): result = client.add(0,i) print(time.time()-start) except Thrift.TException as ex: print("%s" % (ex.message))
def main(): args = parser.parse_args() INT_RE = re.compile("^\d+$") FLOAT_RE = re.compile("^\d+?\.\d+$") EXPS = [ "==", "!=", ">=", "<=", ">", "<", "=regexp", ] FORMATS = ["int", "float"] exps = [] for aexp in args.exps: for exp in EXPS: if exp in aexp: aexp = aexp.split(exp) if len(aexp) == 2: if exp == "=regexp": if aexp[1][0] == '(' and aexp[1][-1] == ')': vtype = "string" value = aexp[1][1:-1] try: re.compile(value) except: break exp = "regexp" else: break elif (aexp[1][0] == '"' and aexp[1][-1] == '"') or (aexp[1][0] == "'" and aexp[1][-1] == "'"): vtype = "string" value = aexp[1][1:-1] elif FLOAT_RE.match(aexp[1]): vtype = "float" value = aexp[1] elif INT_RE.match(aexp[1]): vtype = "int" value = aexp[1] else: vtype = "string" value = aexp[1] exps.append(FilterExpression(aexp[0], exp, vtype, value)) break fields = {} for field in args.fields: fields[field] = 1 formats = {} for format in args.formats: format = format.split(":") if len(format) != 2: continue if format[1] in FORMATS: formats[format[0]] = format[1] name = "tail_" + "".join([ random.choice(string.digits + string.ascii_letters) for _ in range(16) ]) filter = Filter(args.collection, name, exps=exps, fields=fields, formats=formats, expried_time=5) transport = TSocket(args.host, args.port) transport = TBufferedTransport(transport) protocol = TBinaryProtocolAccelerated(transport) client = Client(protocol) transport.open() result = client.register_filter(filter) if result.result != 0: print("register error", name, result.msg) exit() print("register", name, filter) try: cursor = client.pull(name) while True: log = cursor.next() if not log: break if args.fields: flogs = [] try: log = json.loads(log) except: print(log.encode("utf-8")) continue for field in args.fields: if field in args.timefields: ts = log.get(field, 0) try: ts = int(ts) except: pass if isinstance(ts, str): flogs.append(ts) else: flogs.append( datetime.datetime.fromtimestamp( ts).isoformat()) elif formats and field in formats: if formats[field] not in ("int", "float"): flogs.append("'%s'" % log.get(field, "")) else: flogs.append(str(log.get(field, 0))) else: flogs.append("'%s'" % log.get(field, "")) print(" ".join(flogs).encode("utf-8")) else: print(log.encode("utf-8")) except KeyboardInterrupt: pass finally: result = client.unregister_filter(name) print("unregister", name, result.msg) transport.close()