def main():

	myapp = thrift.App(ap_name="iperf", binary_path="/usr/bin/iperf", description="Hello World Application")
	myapp.parameters =  ([thrift.Parameter(name="server", cmd='-s', type= "Flag", default_value="False"), 
		thrift.Parameter(name="client", cmd='-c', type= "String")] )


	send('def_application', TSerialization.serialize(myapp))
	send('def_group', "Servers", "kali")


	app_inst = thrift.AppInstance()
	app_inst.inst_name = "iperf_client"
	app_inst.app_name = "iperf"
	# app_inst.arguments = {"server": ""}
	app_inst.arguments = {"client": "192.168.1.37"}
	# app_inst.environment =
	# app_inst.clean_env =					
	# app_inst.map_err_to_out =

	# my_iface = thrift.Iface(if_name= "wlan0") 	# MOCKUP: This is obviously incomplete

	send('add_application', "kali", TSerialization.serialize(app_inst))
	# api_server.add_application("kali", app_inst)

	event_trigger = thrift.EventTrigger(name="kali_installed" ,enabled=True)
	event_trigger.conditions = {'kali.iperf_client': 'Installed'}

	send('def_event',TSerialization.serialize(event_trigger))


	send('start_experiment')

	# 
	time.sleep(60)
示例#2
0
def playing_idol_on_event_actor_killed(message_type, channel, channel_type,
                                       serialize):
    # 事件消息
    message = ccevent.role.ttypes.EventRoleKilled()

    # 消息反序列化
    TSerialization.deserialize(message, serialize)

    # 主角类型只能是玩家
    if message.type_ != ccentity.entity.ttypes.EntityType.TYPE_ACTOR:
        return None

    # 根据玩家ID获取所在副本ID
    playing_id = idol_types.PlayingManager.get_actor_playing(message.id_)
    if playing_id == None or playing_id == 0:
        return None

    # 获取副本对象
    playing = idol_types.PlayingManager.get(playing_id)
    if playing == None:
        return None

    # 获取玩家对象
    actor = playing.get_actor(message.id_)
    if actor == None:
        return None

    # 玩家死亡次数增加
    actor.inc_dead_count()

    log.log_debug("idol副本 玩家(%d) 死亡次数(%d)" %
                  (message.id_, actor.get_dead_count()))

    # 获取请求代理
    request_proxy = variable_table.get_variable(
        ccvariable.ttypes.Variable.REQUEST_PROXY)

    # 先复活玩家
    request = ccrequest.scene.ttypes.RequestSceneRevive()
    request.actor_ = message.id_
    request.stay_revive_ = True
    # 序列化
    request_data = TSerialization.serialize(request)
    # 发送请求
    request_proxy.request(ccrequest.ttypes.RequestType.REQUEST_SCENE_REVIVE, \
        request_data, len(request_data))

    # 请求失败消息
    request = ccrequest.playing.ttypes.RequestPlayingFailure()
    request.playing_ = playing_id
    # 序列化
    request_data = TSerialization.serialize(request)
    # 发送请求
    request_proxy.request(ccrequest.ttypes.RequestType.REQUEST_PLAYING_FAILURE, \
        request_data, len(request_data))

    log.log_debug("idol副本 玩家(%d) 失败" % message.id_)
示例#3
0
def playing_idol_on_event_actor_killed(message_type, channel, channel_type, serialize):
  # 事件消息
  message = ccevent.role.ttypes.EventRoleKilled()

  # 消息反序列化
  TSerialization.deserialize(message, serialize)

  # 主角类型只能是玩家
  if message.type_ != ccentity.entity.ttypes.EntityType.TYPE_ACTOR:
    return None

  # 根据玩家ID获取所在副本ID
  playing_id = idol_types.PlayingManager.get_actor_playing(message.id_)
  if playing_id == None or playing_id == 0:
    return None

  # 获取副本对象
  playing = idol_types.PlayingManager.get(playing_id)
  if playing == None:
    return None

  # 获取玩家对象
  actor = playing.get_actor(message.id_)
  if actor == None:
    return None

  # 玩家死亡次数增加
  actor.inc_dead_count()

  log.log_debug("idol副本 玩家(%d) 死亡次数(%d)" % (message.id_, actor.get_dead_count()))

  # 获取请求代理
  request_proxy = variable_table.get_variable(ccvariable.ttypes.Variable.REQUEST_PROXY)

  # 先复活玩家
  request = ccrequest.scene.ttypes.RequestSceneRevive()
  request.actor_ = message.id_
  request.stay_revive_ = True
  # 序列化
  request_data = TSerialization.serialize(request)
  # 发送请求
  request_proxy.request(ccrequest.ttypes.RequestType.REQUEST_SCENE_REVIVE, \
      request_data, len(request_data))

  # 请求失败消息
  request = ccrequest.playing.ttypes.RequestPlayingFailure()
  request.playing_ = playing_id
  # 序列化
  request_data = TSerialization.serialize(request)
  # 发送请求
  request_proxy.request(ccrequest.ttypes.RequestType.REQUEST_PLAYING_FAILURE, \
      request_data, len(request_data))

  log.log_debug("idol副本 玩家(%d) 失败" % message.id_)
	def __configure (self, topic, ctype, field, field2=None): 	# ctype must be thrift.ConfigType = {SUBSCRIBE, SET_STATE, ASSIGN_NAME}
		# Create message header:
		msg_id = random.randint(0, 2147483647)
		timestamp = time.time()
		msg_header = thrift.MsgHeader(version=1, mtype=thrift.MessageType.CONFIGURE, msg_id=msg_id, source="ec", ts=timestamp )
		# Create message body:
		body_msg = thrift.ConfigMsg(ctype=ctype, field=field, field2=field2)
		# self.pub_socket.send(topic+'.#', zmq.SNDMORE)	
		# self.pub_socket.send(TSerialization.serialize(msg_header), zmq.SNDMORE)
		# self.pub_socket.send(TSerialization.serialize(body_msg))
		self.__send(topic+'.#', TSerialization.serialize(msg_header), TSerialization.serialize(body_msg))
	def inform(self, _topics, itype, cid=None, field=None, nodeinfo=None):
		# Create message header:
		msg_id = random.randint(0, 2147483647)
		timestamp = time.time()
		# CHECK: Using uid as source
		msg_header = thrift.MsgHeader(version=1, mtype=thrift.MessageType.INFORM, msg_id=msg_id, source=self.uid, ts=timestamp) 
		# Create message body:
		body_msg = thrift.InformMsg(itype=itype, cid=cid, field=field, nodeinfo=nodeinfo) # , nodeinfo=nodeinfo
		coded_body_msg = TSerialization.serialize(body_msg)
		# print "\n\n\n"
		self.pub_socket.send(_topics, zmq.SNDMORE)
		self.pub_socket.send(TSerialization.serialize(msg_header), zmq.SNDMORE)
		self.pub_socket.send(TSerialization.serialize(body_msg))
	def release (self, topic, field):
		# Create message header:
		msg_id = random.randint(0, 2147483647)
		timestamp = time.time()
		msg_header = thrift.MsgHeader(version=1, mtype=thrift.MessageType.RELEASE, msg_id=msg_id, source="ec", ts=timestamp )
		# Create message body:
		body_msg = thrift.ReleaseMsg(field=field)

		# self.pub_socket.send(topic+'.#', zmq.SNDMORE)
		# self.pub_socket.send(TSerialization.serialize(msg_header), zmq.SNDMORE)
		# self.pub_socket.send(TSerialization.serialize(body_msg), zmq.SNDMORE)
		# self.pub_socket.send(TSerialization.serialize(attachment))
		self.__send(topic+'.#', TSerialization.serialize(msg_header),
		 TSerialization.serialize(body_msg), TSerialization.serialize(attachment))
示例#7
0
def thrift_to_json(tobj, remove_timestamps=False, remove_uuids=False):
    """Get a "pretty-printed" JSON string representation for a Thrift object

    Args:

    - `tobj`: A Thrift object
    - `remove_uuids`: Boolean flag indicating if Concrete UUIDs should be
                      removed

    Returns:

    - A string containing a "pretty-printed" JSON representation of the Thrift
      object
    """
    thrift_json_string = TSerialization.serialize(
        tobj, TJSONProtocol.TSimpleJSONProtocolFactory())
    thrift_json = json.loads(thrift_json_string)

    if remove_timestamps:
        thrift_json = get_json_object_without_timestamps(thrift_json)
    if remove_uuids:
        thrift_json = get_json_object_without_uuids(thrift_json)

    return json.dumps(thrift_json, indent=2, separators=(',', ': '),
                      ensure_ascii=False, sort_keys=True)
示例#8
0
def playing_idol_on_event_playing_actor_enter(message_type, channel,
                                              channel_type, serialize):
    # 获取事件交互代理
    communicator_proxy = variable_table.get_variable(
        ccvariable.ttypes.Variable.COMMUNICATOR_PROXY)

    # 获取请求代理
    request_proxy = variable_table.get_variable(
        ccvariable.ttypes.Variable.REQUEST_PROXY)

    # 事件消息
    message = ccevent.playing.ttypes.EventPlayingActorEnter()
    # 反序列化
    TSerialization.deserialize(message, serialize)

    # 关注玩家杀死NPC事件、角色被杀死事件
    communicator_proxy.follow(ccevent.ttypes.EventType.EVENT_ACTOR_KILL_NPC, \
        message.actor_, ccevent.ttypes.ChannelType.CHANNEL_ACTOR, \
        "playing_idol_on_event_actor_kill_npc")
    communicator_proxy.follow(ccevent.ttypes.EventType.EVENT_ROLE_KILLED, \
        message.actor_, ccevent.ttypes.ChannelType.CHANNEL_ACTOR, \
        "playing_idol_on_event_actor_killed")
    communicator_proxy.follow(ccevent.ttypes.EventType.EVENT_PLAYING_ACTOR_REQUEST_COMPLETE,\
        message.actor_, ccevent.ttypes.ChannelType.CHANNEL_ACTOR,\
        "playing_idol_on_event_playing_actor_request_complete")

    # 获取副本对象
    playing = idol_types.PlayingManager.get(message.playing_)
    if playing == None:
        log.log_error("idol副本 获取 playing(%d) 失败" % message.playing_)
        return None

    now = time.time()

    # 获取玩家对象
    actor = playing.get_actor(message.actor_)
    if actor == None:
        # 玩家不存在时,创建并加入副本管理器中
        actor = idol_types.Actor(message.actor_, now)
        playing.add_actor(actor)

    # 副本管理器中加入一个玩家ID到副本ID的对应关系
    idol_types.PlayingManager.add_actor(message.actor_, message.playing_)

    # 请求初始化玩家
    request = ccrequest.playing.ttypes.RequestPlayingInitializeActor()
    request.actor_ = message.actor_
    request.spend_time_ = now - actor.get_start_time()
    request.scores_ = []
    request.datas_ = []

    # 序列化消息
    request_data = TSerialization.serialize(request)

    # 发送请求
    request_proxy.request(ccrequest.ttypes.RequestType.REQUEST_PLAYING_INITIALIZE_ACTOR, \
        request_data, len(request_data))

    log.log_debug("玩家(%d) 进入 idol副本(id=%d,template=%d)" % \
        (message.actor_, message.playing_, message.template_))
示例#9
0
    def write(self, comm, comm_filename=None):
        """
        Args:
            comm (Communication): communication to write to tar file
            comm_filename (str): desired filename of communication
                within tar file (by default the filename will be the
                communication id appended with a .concrete extension)
        """
        if comm_filename is None:
            comm_filename = comm.id + '.concrete'

        thrift_bytes = TSerialization.serialize(
            comm, protocol_factory=factory.protocolFactory)

        file_like_obj = BytesIO(thrift_bytes)

        comm_tarinfo = tarfile.TarInfo()
        comm_tarinfo.type = tarfile.REGTYPE
        comm_tarinfo.name = comm_filename
        comm_tarinfo.size = len(thrift_bytes)
        comm_tarinfo.mode = 0o644
        comm_tarinfo.mtime = time.time()
        comm_tarinfo.uid = _get_uid()
        comm_tarinfo.uname = _get_username()
        comm_tarinfo.gid = _get_gid()
        comm_tarinfo.gname = _get_groupname()

        self.tarfile.addfile(comm_tarinfo, file_like_obj)
示例#10
0
 def request(request_type, request):
     # 序列化请求
     data = TSerialization.serialize(request)
     # 发送请求
     if Request.REQUEST_PROXY.request(request_type, data, len(data)) != 0:
         return False
     return True
示例#11
0
def main():
    parser = argparse.ArgumentParser(
        description=
        "Encode a Communication archive as a CSV file, where each row contains a "
        "TJSONProtocol encoded Communication",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument(
        'comms_archive',
        help="A directory, TGZ file or Zip file of Communications")
    parser.add_argument(
        'csv_file',
        help="Output CSV file with TJSONProtocol encoded Communications")
    parser.add_argument('--column-name',
                        default='comm',
                        help="Name to use for CSV column header")
    args = parser.parse_args()

    csv_fh = open(args.csv_file, 'wb')

    fieldnames = [args.column_name]
    writer = unicodecsv.DictWriter(csv_fh,
                                   fieldnames,
                                   lineterminator='\n',
                                   quoting=unicodecsv.QUOTE_ALL)
    writer.writeheader()

    for (comm, filename) in CommunicationReader(args.comms_archive):
        json_communication = TSerialization.serialize(
            comm, TJSONProtocol.TJSONProtocolFactory()).decode('utf-8')
        writer.writerow({args.column_name: json_communication})
示例#12
0
def add_award(actor, template):
  log.log_debug("idol.add_award(%d, %d)" % (actor, template))

  request_proxy = variable_table.get_variable(ccvariable.ttypes.Variable.REQUEST_PROXY)

  # 获取副本配置
  playing_config = idol_types.Config.get(template)
  if playing_config == None:
    log.log_error("获取 副本配置失败(%d)" % template)
    return None

  # 请求增加副本奖励
  request = ccrequest.playing.ttypes.RequestPlayingAddAward()
  request.actor_ = actor
  request.playing_template_ = template
  # 奖励
  request.awards_ = playing_config.get_awards()
  # 抽奖
  draw_awards = playing_config.get_draw_awards()
  draw_awards_size = len(draw_awards)
  if draw_awards_size > 0:
    request.draw_award_ = draw_awards[random.randint(0, draw_awards_size - 1)]
  else:
    request.draw_award_ = ccentity.playing.ttypes.PlayingAwardField(\
        ccentity.resource.ttypes.ResourceType.MIN, 0, 0)

  # 序列化
  request_data = TSerialization.serialize(request)
  # 发送请求
  request_proxy.request(ccrequest.ttypes.RequestType.REQUEST_PLAYING_ADD_AWARD,\
      request_data, len(request_data))
示例#13
0
def thrift_to_json(tobj, remove_timestamps=False, remove_uuids=False):
    """Get a "pretty-printed" JSON string representation for a Thrift object

    Args:

    - `tobj`: A Thrift object
    - `remove_uuids`: Boolean flag indicating if Concrete UUIDs should be
                      removed

    Returns:

    - A string containing a "pretty-printed" JSON representation of the Thrift
      object
    """
    thrift_json_string = TSerialization.serialize(
        tobj, TJSONProtocol.TSimpleJSONProtocolFactory())
    thrift_json = json.loads(thrift_json_string)

    if remove_timestamps:
        thrift_json = get_json_object_without_timestamps(thrift_json)
    if remove_uuids:
        thrift_json = get_json_object_without_uuids(thrift_json)

    return json.dumps(thrift_json,
                      indent=2,
                      separators=(',', ': '),
                      ensure_ascii=False,
                      sort_keys=True)
示例#14
0
def blub():
    j1 = JobDTO()
    j1.id = "id1"
    j1.programId = "id2"
    j1.mapRoutineId = "xxx"
    j1.numberOfTasks = 3555556
    j1.numberOfFinishedTasks = -1
    #j1_json = thrift2json(j1)
    #print(j1_json)
    #j2 = json2thrift(j1_json, JobDTO)
    #print(j1)
    #print(j2)
    p = '{"id":"8c8bb3c5-5378-4e0f-a9c1-1cbdf3b750c7","state":1,"createTime":1502199289569,"finishTime":0,"masterLibraryRoutine":false,"setId":true,"setState":false,"setCreateTime":true,"setFinishTime":true,"setMasterLibraryRoutine":true}'
    print(p)
    pp = json2thrift(p, ProgramDTO)
    #print(pp)
    #rib = RoutineInstanceBuilder('routine_id')
    #rib.add_missing_parameter('param1')
    #rib.add_missing_parameter('param2')
    #rib.add_paramater('param3', 'DEFDouble', DEFDouble())
    #rib.add_paramater('param4', 'DEFDouble', DEFDouble())
    #print(rib.get_routine_instance())
    resource = ResourceDTO()
    resource.id = str(uuid.uuid4())
    resource.key = "key"
    resource.data = TSerialization.serialize(DEFDouble(4))
    r = thrift2json(resource)
    print(r)
示例#15
0
 def request(request_type, request):
     # 序列化请求
     data = TSerialization.serialize(request)
     # 发送请求
     if Request.REQUEST_PROXY.request(request_type, data, len(data)) != 0:
         return False
     return True
示例#16
0
    def run(self):
        # setup communication
        self._in_trans = TTransport.TFileObjectTransport(
            open(self._in_pipe, 'rb'))
        self._out_trans = TTransport.TFileObjectTransport(
            open(self._out_pipe, 'wb'))
        self._ctrl_trans = TTransport.TFileObjectTransport(
            open(self._ctrl_pipe, 'wb'))
        self._in_proto = TBinaryProtocol.TBinaryProtocol(self._in_trans)
        self._out_proto = TBinaryProtocol.TBinaryProtocol(self._out_trans)
        self._ctrl_proto = TBinaryProtocol.TBinaryProtocol(self._ctrl_trans)
        self.__log_debug__('Setup communication done.')
        self.__log_debug__('Start routine: {}'.format(self))

        # run real implementation
        self.__log_debug__('Run routine implementation.')
        result = self.__run__()

        # store result to map routine
        self.__log_debug__('Store result to map routine.')
        buffer = TSerialization.serialize(result)
        send_int(self._out_proto, self._out_trans, len(buffer))
        send_binary(self._out_trans, buffer)

        # Shutdown routine
        self.__log_debug__('Done. Shutdown routine')
        send_object(self._ctrl_proto, self._ctrl_trans,
                    Order(command=Command.ROUTINE_DONE))
        self._in_trans.close()
        self._out_trans.close()
        self._ctrl_trans.close()
        return
示例#17
0
def write_thrift_to_file(thrift_obj, filename):
    thrift_bytes = TSerialization.serialize(
        thrift_obj,
        protocol_factory=factory.protocolFactory)
    thrift_file = open(filename, "wb")
    thrift_file.write(thrift_bytes)
    thrift_file.close()
示例#18
0
def playing_slime_on_timer_boss(id):
    # 获取副本对象
    playing = slime_types.PlayingManager.get(id)
    if playing == None:
        proxy.Logging.error("[slime] PlayingManager.get(%d) failed" % id)
        return None

    # 获取玩家对象
    actor = proxy.EntityManager.get_actor(playing.get_actor())
    if actor == None:
        proxy.Logging.error("[slime] get actor(%d) failed" %
                            playing.get_actor())
        return None

    status = actor.get_basic_status()
    if status == ccentity.role.ttypes.RoleBasicStatus.STATUS_DIE:
        # 先复活玩家
        request = ccrequest.scene.ttypes.RequestSceneRevive()
        request.actor_ = message.id_
        request.stay_revive_ = True
        # 序列化
        request_data = TSerialization.serialize(request)
        # 发送请求
        request_proxy.request(ccrequest.ttypes.RequestType.REQUEST_SCENE_REVIVE, \
            request_data, len(request_data))

    request = ccrequest.playing.ttypes.RequestPlayingFailure()
    request.playing_ = id
    proxy.Request.request(ccrequest.ttypes.RequestType.REQUEST_PLAYING_FAILURE,
                          request)
    proxy.Logging.debug("[slime] actor(%d) boss time expired." %
                        playing.get_actor())
示例#19
0
    def write(self, comm, comm_filename=None):
        """
        Args:
            comm (Communication): communication to write to tar file
            comm_filename (str): desired filename of communication
                within tar file (by default the filename will be the
                communication id appended with a .concrete extension)
        """
        if comm_filename is None:
            comm_filename = comm.id + '.concrete'

        thrift_bytes = TSerialization.serialize(
            comm, protocol_factory=factory.protocolFactory)

        file_like_obj = BytesIO(thrift_bytes)

        comm_tarinfo = tarfile.TarInfo()
        comm_tarinfo.type = tarfile.REGTYPE
        comm_tarinfo.name = comm_filename
        comm_tarinfo.size = len(thrift_bytes)
        comm_tarinfo.mode = 0o644
        comm_tarinfo.mtime = time.time()
        comm_tarinfo.uid = _get_uid()
        comm_tarinfo.uname = _get_username()
        comm_tarinfo.gid = _get_gid()
        comm_tarinfo.gname = _get_groupname()

        self.tarfile.addfile(comm_tarinfo, file_like_obj)
示例#20
0
def add_award(actor, template):
    log.log_debug("idol.add_award(%d, %d)" % (actor, template))

    request_proxy = variable_table.get_variable(ccvariable.ttypes.Variable.REQUEST_PROXY)

    # 获取副本配置
    playing_config = idol_types.Config.get(template)
    if playing_config == None:
        log.log_error("获取 副本配置失败(%d)" % template)
        return None

    # 请求增加副本奖励
    request = ccrequest.playing.ttypes.RequestPlayingAddAward()
    request.actor_ = actor
    request.playing_template_ = template
    # 奖励
    request.awards_ = playing_config.get_awards()
    # 抽奖
    draw_awards = playing_config.get_draw_awards()
    draw_awards_size = len(draw_awards)
    if draw_awards_size > 0:
        request.draw_award_ = draw_awards[random.randint(0, draw_awards_size - 1)]
    else:
        request.draw_award_ = ccentity.playing.ttypes.PlayingAwardField(ccentity.resource.ttypes.ResourceType.MIN, 0, 0)

    # 序列化
    request_data = TSerialization.serialize(request)
    # 发送请求
    request_proxy.request(ccrequest.ttypes.RequestType.REQUEST_PLAYING_ADD_AWARD, request_data, len(request_data))
示例#21
0
def thrift_to_json(tobj, remove_timestamps=False, remove_uuids=False):
    """Get a "pretty-printed" JSON string representation for a Thrift object

    Args:
        tobj: A Thrift object
        remove_timestamps (bool): Flag for removing timestamps from JSON output
        remove_uuids (bool): Flag for removing :class:`.UUID` info from JSON output

    Returns:
        str: A "pretty-printed" JSON representation of the Thrift object
    """
    thrift_json_string = TSerialization.serialize(
        tobj, TJSONProtocol.TSimpleJSONProtocolFactory()).decode('utf-8')
    thrift_json = json.loads(thrift_json_string)

    if remove_timestamps:
        thrift_json = get_json_object_without_timestamps(thrift_json)
    if remove_uuids:
        thrift_json = get_json_object_without_uuids(thrift_json)

    return json.dumps(thrift_json,
                      indent=2,
                      separators=(',', ': '),
                      ensure_ascii=False,
                      sort_keys=True)
示例#22
0
def playing_slime_on_timer_boss(id):
  # 获取副本对象
  playing = slime_types.PlayingManager.get(id)
  if playing == None:
    proxy.Logging.error("[slime] PlayingManager.get(%d) failed" % id)
    return None

  # 获取玩家对象
  actor = proxy.EntityManager.get_actor(playing.get_actor())
  if actor == None:
    proxy.Logging.error("[slime] get actor(%d) failed" % playing.get_actor())
    return None

  status = actor.get_basic_status()
  if status == ccentity.role.ttypes.RoleBasicStatus.STATUS_DIE:
    # 先复活玩家
    request = ccrequest.scene.ttypes.RequestSceneRevive()
    request.actor_ = message.id_
    request.stay_revive_ = True
    # 序列化
    request_data = TSerialization.serialize(request)
    # 发送请求
    request_proxy.request(ccrequest.ttypes.RequestType.REQUEST_SCENE_REVIVE, \
        request_data, len(request_data))

  request = ccrequest.playing.ttypes.RequestPlayingFailure()
  request.playing_ = id
  proxy.Request.request(ccrequest.ttypes.RequestType.REQUEST_PLAYING_FAILURE, request)
  proxy.Logging.debug("[slime] actor(%d) boss time expired." % playing.get_actor())
示例#23
0
    def diff_config(self, app, verbose=False):
        """
        Print the diff between current and desired job config
        """
        print(">>>>>>>> Job config diff for %s <<<<<<<<" % app.name)
        cfg_dicts = []
        factory = TSimpleJSONProtocolFactory()
        for cfg in app.current_job_config, app.desired_job_config:
            if cfg:
                cfg_json = TSerialization.serialize(cfg,
                                                    protocol_factory=factory)
                cfg_dict = json.loads(cfg_json)

                # Unset task resources to avoid confusing the job config differ
                cfg_dict["taskConfig"]["resources"] = None
            else:
                cfg_dict = {}
            cfg_dicts.append(cfg_dict)

        if verbose:
            for cfg_dict in cfg_dicts:
                print(json.dumps(cfg_dict, indent=4, sort_keys=True))

        for line in json_delta.udiff(cfg_dicts[0], cfg_dicts[1]):
            print(line)
示例#24
0
def serialize_v2_event(event):
    """Serialize a Thrift struct to bytes for the V2 event protocol.

    :param event: A Thrift struct from the event schemas.

    """
    return TSerialization.serialize(event, _V2_PROTOCOL_FACTORY)
示例#25
0
 def write(self, comm):
     """
     Args:
         comm (Communication): communication to write to file
     """
     thrift_bytes = TSerialization.serialize(
         comm, protocol_factory=factory.protocolFactory)
     self.file.write(thrift_bytes)
示例#26
0
 def write(self, comm):
     """
     Args:
         comm (Communication): communication to write to file
     """
     thrift_bytes = TSerialization.serialize(
         comm, protocol_factory=factory.protocolFactory)
     self.file.write(thrift_bytes)
示例#27
0
 def __ThriftToJson(thriftdata):
     thrift_json_string = TSerialization.serialize(
         thriftdata,
         TJSONProtocol.TSimpleJSONProtocolFactory()).decode('utf-8')
     json_value = json.loads(thrift_json_string)
     json_value['password'] = thriftdata.password.decode()
     json_value['code'] = thriftdata.code.decode()
     return json_value
示例#28
0
    def test_validate_received_token(self):
        token = self.client.user_info(DN, "", "SecurityClientTest")
        nt.assert_true(self.client.validateReceivedToken(token))

        b = tser.serialize(token)
        token = EzSecurityToken()
        tser.deserialize(token, b)
        nt.assert_true(self.client.validateReceivedToken(token))
示例#29
0
def communication_to_json(comm):
    """
    Takes a Communication instance, returns a JSON string with the
    information in that Communication.
    """
    comm_json_string = TSerialization.serialize(comm, TJSONProtocol.TSimpleJSONProtocolFactory())
    comm_json = json.loads(comm_json_string)
    return json.dumps(comm_json, indent=2, separators=(',', ': '), ensure_ascii=False, sort_keys=True)
示例#30
0
 def register(self, instance):
   sync_log('Registering instance: %s' % instance)
   return self._zk.create(
       posixpath.join(self._path, 'member_'),
       codec.serialize(instance),
       [ZOO_OPEN_ACL_UNSAFE],
       zookeeper.SEQUENCE,
       )
示例#31
0
    def new(self, authentication_token=None, loid_id=None, loid_created_ms=None, session_id=None):
        """Return a new EdgeRequestContext object made from scratch.

        Services at the edge that communicate directly with clients should use
        this to pass on the information they get to downstream services. They
        can then use this information to check authentication, run experiments,
        etc.

        To use this, create and attach the context early in your request flow:

        .. code-block:: python

            auth_cookie = request.cookies["authentication"]
            token = request.authentication_service.authenticate_cookie(cookie)
            loid = parse_loid(request.cookies["loid"])
            session = parse_session(request.cookies["session"])

            edge_context = self.edgecontext_factory.new(
                authentication_token=token,
                loid_id=loid.id,
                loid_created_ms=loid.created,
                session_id=session.id,
            )
            edge_context.attach_context(request)

        :param authentication_token: (Optional) A raw authentication token
            as returned by the authentication service.
        :param str loid_id: (Optional) ID for the current LoID in fullname
            format.
        :param int loid_created_ms: (Optional) Epoch milliseconds when the
            current LoID cookie was created.
        :param str session_id: (Optional) ID for the current session cookie.

        """
        # Importing the Thrift models inline so that building them is not a
        # hard, import-time dependency for tasks like building the docs.
        from baseplate.thrift.ttypes import Loid as TLoid
        from baseplate.thrift.ttypes import Request as TRequest
        from baseplate.thrift.ttypes import Session as TSession

        if loid_id is not None and not loid_id.startswith("t2_"):
            raise ValueError(
                "loid_id <%s> is not in a valid format, it should be in the "
                "fullname format with the '0' padding removed: 't2_loid_id'" % loid_id
            )

        t_request = TRequest(
            loid=TLoid(id=loid_id, created_ms=loid_created_ms),
            session=TSession(id=session_id),
            authentication_token=authentication_token,
        )
        header = TSerialization.serialize(t_request, EdgeRequestContext._HEADER_PROTOCOL_FACTORY)

        context = EdgeRequestContext(self.authn_token_validator, header)
        # Set the _t_request property so we can skip the deserialization step
        # since we already have the thrift object.
        context._t_request = t_request
        return context
示例#32
0
def playing_idol_on_event_playing_actor_enter(message_type, channel, channel_type, serialize):
  # 获取事件交互代理
  communicator_proxy = variable_table.get_variable(ccvariable.ttypes.Variable.COMMUNICATOR_PROXY)

  # 获取请求代理
  request_proxy = variable_table.get_variable(ccvariable.ttypes.Variable.REQUEST_PROXY)

  # 事件消息
  message = ccevent.playing.ttypes.EventPlayingActorEnter()
  # 反序列化
  TSerialization.deserialize(message, serialize)

  # 关注玩家杀死NPC事件、角色被杀死事件
  communicator_proxy.follow(ccevent.ttypes.EventType.EVENT_ACTOR_KILL_NPC, \
      message.actor_, ccevent.ttypes.ChannelType.CHANNEL_ACTOR, \
      "playing_idol_on_event_actor_kill_npc")
  communicator_proxy.follow(ccevent.ttypes.EventType.EVENT_ROLE_KILLED, \
      message.actor_, ccevent.ttypes.ChannelType.CHANNEL_ACTOR, \
      "playing_idol_on_event_actor_killed")
  communicator_proxy.follow(ccevent.ttypes.EventType.EVENT_PLAYING_ACTOR_REQUEST_COMPLETE,\
      message.actor_, ccevent.ttypes.ChannelType.CHANNEL_ACTOR,\
      "playing_idol_on_event_playing_actor_request_complete")

  # 获取副本对象
  playing = idol_types.PlayingManager.get(message.playing_)
  if playing == None:
    log.log_error("idol副本 获取 playing(%d) 失败" % message.playing_)
    return None

  now = time.time()

  # 获取玩家对象
  actor = playing.get_actor(message.actor_)
  if actor == None:
    # 玩家不存在时,创建并加入副本管理器中
    actor = idol_types.Actor(message.actor_, now)
    playing.add_actor(actor)

  # 副本管理器中加入一个玩家ID到副本ID的对应关系
  idol_types.PlayingManager.add_actor(message.actor_, message.playing_)

  # 请求初始化玩家
  request = ccrequest.playing.ttypes.RequestPlayingInitializeActor()
  request.actor_ = message.actor_
  request.spend_time_ = now - actor.get_start_time()
  request.scores_ = []
  request.datas_ = []

  # 序列化消息
  request_data = TSerialization.serialize(request)

  # 发送请求
  request_proxy.request(ccrequest.ttypes.RequestType.REQUEST_PLAYING_INITIALIZE_ACTOR, \
      request_data, len(request_data))

  log.log_debug("玩家(%d) 进入 idol副本(id=%d,template=%d)" % \
      (message.actor_, message.playing_, message.template_))
示例#33
0
def serialize_to_base64(thrift_object):
    """
    Serializes the given Thrift object, and encodes it in base64.

    :param thrift_object: A thrift object that you want to serialize and encode.
    :return: Base64-Encoded and Serialzed Thrift object
    """
    serialized_object = TSerialization.serialize(thrift_object)
    return base64.b64encode(serialized_object)
示例#34
0
def verify(token, pubkey, owner, target=None):
    valid = False
    expires = token.response.expires

    # sort auths, formal access
    token.authorizations.externalCommunityAuthorizations.sort()
    token.authorizations.formalAuthorizations.sort()

    projects = token.response.securityInfo.projects
    if projects is not None:
        # sort projects
        od = OrderedDict(sorted(projects.items(), key=lambda t: t[0]))
        token.response.securityInfo.projects = od

    log = logging.getLogger(__name__)
    log.debug("verifying token {0}".format(token))

    if target is not None:
        log.info("Target not None, verifying target security ID")
        if token.response.targetSecurityId == target:
            log.info("Target security ID matches the passed target")
            if expires > current_time_millis():
                log.info("token expiration looks ok ({0} > {1})".format(expires, current_time_millis()))
                data = tser.serialize(token.response)
                sig = base64.b64decode(token.signature)
                log.info("verifying the signature")
                valid = verify_signature(data, sig, pubkey)
            else:
                log.info("expiration is bad ({0} > {1})".format(expires, current_time_millis()))
    elif token.response.securityId == owner:
        log.info("Verifying token for owner: {0}".format(owner))
        if expires > current_time_millis():
            log.info("token expiration looks ok ({0} > {1})".format(expires, current_time_millis()))
            data = tser.serialize(token.response)
            sig = base64.b64decode(token.signature)
            log.info("verifying the signature")
            valid = verify_signature(data, sig, pubkey)
        else:
            log.info("expiration is bad ({0} > {1})".format(expires, current_time_millis()))
    else:
        log.info("Not verifying token because target is none and the security ID doesn't match the owner")
    return valid
示例#35
0
    def user_info(self, dn, sig, targetApp=None):
        """
        Request a token with user info. Includes a targetSecurityId
        in the token if the txApp is passed. If targetSecurityId is set in the
        token, you will be able to pass this token to other thrift services.
        You should set txApp to
        ApplicationConfiguration(ezconfig).getSecurityID() if you are sending
        this to another thrift service within your application,
        @param dn: the user's dn, usually extracted from headers set by EFE
        @param sig: security service signed DN, usually extracted from headers
        set by EFE
        @param targetApp: optiaonlly, request security service to include a
        targetSecurityId in the token
        @return: the EzSecurityToken
        """
        if targetApp is None:
            targetApp = self.appConfig.getSecurityID()
        if sig is None:
            sig = ""
        if self.mock and dn is None:
            dn = self.ezconfig.get(MOCK_USER_DN)
            if dn is None:
                raise RuntimeError("{0} is in mock mode, but {1} is None".
                    format(self.__class__, MOCK_USER_DN))

        # look in the cache
        cache_key = self._cache_key(targetApp, dn)
        try:
            token = EzSecurityClient.user_cache[cache_key]
            if self._validateToken(token):
                self.log.info("Using token from cache")
                return token
            else:
                self.log.info("Token in cache was invalid. getting new")
        except KeyError:
            # it's not in the cache, continue
            pass

        client = self._getClient()
        request = self.build_request(dn, sig, targetApp)

        self.log.info("Requesting user info for %s from EzSecurity", dn)
        token = client.requestUserInfo(request,
                                       self._sign(tser.serialize(request)))
        self.log.info("Received user info for %s from EzSecurity", dn)
        if not self._validateToken(token):
            self.log.error("Invalid token received from EzSecurity")
            token = None
        else:
            self.log.info("Storing user info %s in cache", dn)
            EzSecurityClient.user_cache[cache_key] = token

        self._returnClient(client)
        return token
示例#36
0
def write_thrift_to_file(thrift_obj, filename):
    """Write a Thrift object to a file

    Args:
        thrift_obj: Thrift object to write
        filename (str): path of file to write to
    """
    thrift_bytes = TSerialization.serialize(
        thrift_obj, protocol_factory=factory.protocolFactory)
    thrift_file = open(filename, "wb")
    thrift_file.write(thrift_bytes)
    thrift_file.close()
示例#37
0
def main():
    set_stdout_encoding()

    parser = make_parser()
    args = parser.parse_args()

    logging.basicConfig(format='%(asctime)-15s %(levelname)s: %(message)s',
                        level=args.loglevel.upper())

    mimetypes.init()
    (ifile_type, ifile_encoding) = mimetypes.guess_type(args.input_file)
    (ofile_type, ofile_encoding) = mimetypes.guess_type(args.output_file)
    out_writer = None
    if args.direction is None:
        if args.iprotocol is None or args.oprotocol is None:
            print("Either --direction, or both --iprotocol and --oprotocol,"
                  " must be provided")
            exit(1)
    else:
        if (args.iprotocol is not None) or (args.oprotocol is not None):
            print("Not both --direction, and either --iprotocol or"
                  " --oprotocol, can be provided")
            exit(1)
    encoding_input = KNOWN_CONVERSIONS[
        args.direction][0] if args.iprotocol is None else PROTOCOLS[
            args.iprotocol]
    encoding_output = KNOWN_CONVERSIONS[
        args.direction][1] if args.oprotocol is None else PROTOCOLS[
            args.oprotocol]
    if ofile_encoding == "gzip":
        out_writer = gzip.GzipFile(args.output_file, 'wb')
    else:
        out_writer = open(args.output_file, 'w')
    if ifile_encoding == 'gzip':
        f = gzip.GzipFile(args.input_file)
        transportIn = TTransport.TFileObjectTransport(f)
        protocolIn = encoding_input().getProtocol(transportIn)
        while True:
            try:
                comm = Communication()
                comm.read(protocolIn)
                output_bytes = TSerialization.serialize(
                    comm, protocol_factory=encoding_output())
                out_writer.write(output_bytes)
            except EOFError:
                break
        f.close()
    else:
        convert(input_file_path=args.input_file,
                output_file_path=args.output_file,
                input_protocol_factory=encoding_input,
                output_protocol_factory=encoding_output)
    out_writer.close()
示例#38
0
def main():
    set_stdout_encoding()

    parser = argparse.ArgumentParser(
        description="Pretty Print a Concrete file")
    parser.add_argument('--concrete_type', default='communication',
                        choices=['communication', 'tokenlattice'],
                        help='Default: communication')
    parser.add_argument('--protocol', default='simple',
                        choices=['simple', 'TJSONProtocol'],
                        help='Default: simple')
    parser.add_argument('--remove-timestamps', action='store_true',
                        help="Removes timestamps from JSON output")
    parser.add_argument('--remove-uuids', action='store_true',
                        help="Removes UUIDs from JSON output")
    parser.add_argument('-l', '--loglevel', '--log-level',
                        help='Logging verbosity level threshold (to stderr)',
                        default='info')
    parser.add_argument('concrete_file',
                        help='path to input concrete communication file')
    parser.add_argument('json_file', nargs='?', default='-',
                        help='path to output json file')
    concrete.version.add_argparse_argument(parser)
    args = parser.parse_args()

    logging.basicConfig(format='%(asctime)-15s %(levelname)s: %(message)s',
                        level=args.loglevel.upper())

    if args.protocol == 'simple':
        if args.concrete_type == 'communication':
            json_communication = communication_file_to_json(
                args.concrete_file,
                remove_timestamps=args.remove_timestamps,
                remove_uuids=args.remove_uuids
            )
        else:
            json_communication = tokenlattice_file_to_json(
                args.concrete_file
            )
    else:
        if args.concrete_type == 'communication':
            comm = read_communication_from_file(args.concrete_file)
            json_communication = TSerialization.serialize(
                comm, TJSONProtocol.TJSONProtocolFactory()).decode('utf-8')
        else:
            raise NotImplementedError

    if args.json_file == '-':
        print(json_communication)
    else:
        with codecs.open(args.json_file, 'w', encoding='utf-8') as f:
            f.write(json_communication)
示例#39
0
def playing_idol_on_event_actor_kill_npc(message_type, channel, channel_type,
                                         serialize):
    # 获取请求代理
    request_proxy = variable_table.get_variable(
        ccvariable.ttypes.Variable.REQUEST_PROXY)

    # 事件消息
    message = ccevent.actor.ttypes.EventActorKillNpc()
    # 反序列化
    TSerialization.deserialize(message, serialize)

    # 获取玩家所在副本ID
    playing_id = idol_types.PlayingManager.get_actor_playing(message.actor_)
    if playing_id == None or playing_id == 0:
        return None

    # 获取副本对象
    playing = idol_types.PlayingManager.get(playing_id)
    if playing == None:
        return None

    # 获取副本配置
    playing_config = idol_types.Config.get(playing.get_template_id())
    if playing_config == None:
        log.log_error("获取 副本配置失败(%d)" % playing.get_template_id())
        return None

    # 获取玩家通关需要杀死的NPC
    pass_kill_npc = playing_config.get_pass_kill_npc()
    if message.npc_template_ != pass_kill_npc:
        log.log_error("玩家(%d) 杀死npc(%d) 不是通关npc(%d)" % \
            (message.actor_, message.npc_template_, pass_kill_npc))
        return None

    # 获取副本玩家对象
    actor = playing.get_actor(message.actor_)
    if actor == None:
        log.log_error("idol副本 获取actor(%d) 失败" % message.actor_)
        return None

    if actor.get_finish() == 0 and actor.get_dead_count() <= 0:
        actor.set_finish()
        # 请求同步玩家得分
        request = ccrequest.playing.ttypes.RequestPlayingSynchronizeScore()
        request.actor_ = message.actor_
        request.score_ = ccentity.playing.ttypes.PlayingScoreField(ccentity.playing.ttypes.PlayingScoreType.KILL_NPC, \
            message.npc_template_, 1)
        # 序列化消息
        request_data = TSerialization.serialize(request)
        # 发送请求
        request_proxy.request(ccrequest.ttypes.RequestType.REQUEST_PLAYING_SYNCHRONIZE_SCORE, \
            request_data, len(request_data))
 def test_validate_received_token(self):
     client = self.get_client()
     subject = "Joe USER"
     headers = {
         HTTP_HEADER_USER_INFO: subject,
         HTTP_HEADER_SIGNATURE: ""
     }
     token = client.fetch_user_token(headers, "SecurityClientTest")
     nt.assert_true(client.validate_received_token(token))
     b = TSer.serialize(token)
     token = EzSecurityToken()
     TSer.deserialize(token, b)
     nt.assert_true(client.validate_received_token(token))
示例#41
0
def write_thrift_to_file(thrift_obj, filename):
    """Write a Thrift object to a file

    Args:
        thrift_obj: Thrift object to write
        filename (str): path of file to write to
    """
    thrift_bytes = TSerialization.serialize(
        thrift_obj,
        protocol_factory=factory.protocolFactory)
    thrift_file = open(filename, "wb")
    thrift_file.write(thrift_bytes)
    thrift_file.close()
示例#42
0
    def _user_dn(self, dn):
        """
        Request a signed DN from the security service. Note this will most
        likely fail, since it only signs DNs for the EFE
        @param dn: the user's X509 subject
        @return an EzSecurityDn with a valid signature
        """
        request = self.build_request(dn, "")

        client = self._getClient()
        dn = client.requestUserDN(request, self._sign(tser.serialize(request)))
        self._returnClient(client)
        return dn
示例#43
0
def main():
    set_stdout_encoding()

    parser = make_parser()
    args = parser.parse_args()

    logging.basicConfig(format='%(asctime)-15s %(levelname)s: %(message)s',
                        level=args.loglevel.upper())

    mimetypes.init()
    (ifile_type, ifile_encoding) = mimetypes.guess_type(args.input_file)
    (ofile_type, ofile_encoding) = mimetypes.guess_type(args.output_file)
    out_writer = None
    if args.direction is None:
        if args.iprotocol is None or args.oprotocol is None:
            print("Either --direction, or both --iprotocol and --oprotocol,"
                  " must be provided")
            exit(1)
    else:
        if (args.iprotocol is not None) or (args.oprotocol is not None):
            print("Not both --direction, and either --iprotocol or"
                  " --oprotocol, can be provided")
            exit(1)
    encoding_input = KNOWN_CONVERSIONS[args.direction][
        0] if args.iprotocol is None else PROTOCOLS[args.iprotocol]
    encoding_output = KNOWN_CONVERSIONS[args.direction][
        1] if args.oprotocol is None else PROTOCOLS[args.oprotocol]
    if ofile_encoding == "gzip":
        out_writer = gzip.GzipFile(args.output_file, 'wb')
    else:
        out_writer = open(args.output_file, 'w')
    if ifile_encoding == 'gzip':
        f = gzip.GzipFile(args.input_file)
        transportIn = TTransport.TFileObjectTransport(f)
        protocolIn = encoding_input().getProtocol(transportIn)
        while True:
            try:
                comm = Communication()
                comm.read(protocolIn)
                output_bytes = TSerialization.serialize(
                    comm, protocol_factory=encoding_output())
                out_writer.write(output_bytes)
            except EOFError:
                break
        f.close()
    else:
        convert(input_file_path=args.input_file,
                output_file_path=args.output_file,
                input_protocol_factory=encoding_input,
                output_protocol_factory=encoding_output)
    out_writer.close()
示例#44
0
def playing_plot_on_event_actor_kill_npc(message_type, channel, channel_type,
                                         serialize):
    # 获取请求代理
    request_proxy = variable_table.get_variable(
        ccvariable.ttypes.Variable.REQUEST_PROXY)

    # 事件消息
    message = ccevent.actor.ttypes.EventActorKillNpc()
    # 反序列化
    TSerialization.deserialize(message, serialize)

    # 获取玩家所在副本ID
    playing_id = plot_types.PlayingManager.get_actor_playing(message.actor_)
    if playing_id == None or playing_id == 0:
        proxy.Logging.error(
            "plot_types.PlayingManager.get_actor_playing(%d) failed." %
            message.actor_)
        return None

    # 获取副本对象
    playing = plot_types.PlayingManager.get(playing_id)
    if playing == None:
        proxy.Logging.error("plot_types.PlayingManager.get(%d) failed." %
                            playing_id)
        return None

    # 获取副本玩家对象
    actor = playing.get_actor(message.actor_)
    if actor == None:
        proxy.Logging.error("playing.get_actor(%d) failed." % message.actor_)
        return None

    # 增加杀死NPC个数
    actor.inc_kill_npc(message.npc_template_)

    # 请求消息
    request = ccrequest.playing.ttypes.RequestPlayingSynchronizeScore()
    # 玩家ID
    request.actor_ = message.actor_
    # 分数对象(得分类型=杀死NPC, key=NPC模板ID, value=当前杀死的个数)
    score_field = ccentity.playing.ttypes.PlayingScoreField(ccentity.playing.ttypes.PlayingScoreType.KILL_NPC, \
        message.npc_template_, actor.get_kill_npc(message.npc_template_))
    request.score_ = score_field
    # 序列化消息
    request_data = TSerialization.serialize(request)
    # 发送请求
    request_proxy.request(ccrequest.ttypes.RequestType.REQUEST_PLAYING_SYNCHRONIZE_SCORE, \
        request_data, len(request_data))

    log.log_debug("玩家(%d) 杀死 NPC(%d) 个数(%d)" % (message.actor_, score_field.key_, \
          score_field.value_))
示例#45
0
def playing_idol_on_event_actor_kill_npc(message_type, channel, channel_type, serialize):
  # 获取请求代理
  request_proxy = variable_table.get_variable(ccvariable.ttypes.Variable.REQUEST_PROXY)

  # 事件消息
  message = ccevent.actor.ttypes.EventActorKillNpc()
  # 反序列化
  TSerialization.deserialize(message, serialize)

  # 获取玩家所在副本ID
  playing_id = idol_types.PlayingManager.get_actor_playing(message.actor_)
  if playing_id == None or playing_id == 0:
    return None

  # 获取副本对象
  playing = idol_types.PlayingManager.get(playing_id)
  if playing == None:
    return None

  # 获取副本配置
  playing_config = idol_types.Config.get(playing.get_template_id())
  if playing_config == None:
    log.log_error("获取 副本配置失败(%d)" % playing.get_template_id())
    return None

  # 获取玩家通关需要杀死的NPC
  pass_kill_npc = playing_config.get_pass_kill_npc()
  if message.npc_template_ != pass_kill_npc:
    log.log_error("玩家(%d) 杀死npc(%d) 不是通关npc(%d)" % \
        (message.actor_, message.npc_template_, pass_kill_npc))
    return None

  # 获取副本玩家对象
  actor = playing.get_actor(message.actor_)
  if actor == None:
    log.log_error("idol副本 获取actor(%d) 失败" % message.actor_)
    return None

  if actor.get_finish() == 0 and actor.get_dead_count() <= 0:
    actor.set_finish()
    # 请求同步玩家得分
    request = ccrequest.playing.ttypes.RequestPlayingSynchronizeScore()
    request.actor_ = message.actor_
    request.score_ = ccentity.playing.ttypes.PlayingScoreField(ccentity.playing.ttypes.PlayingScoreType.KILL_NPC, \
        message.npc_template_, 1)
    # 序列化消息
    request_data = TSerialization.serialize(request)
    # 发送请求
    request_proxy.request(ccrequest.ttypes.RequestType.REQUEST_PLAYING_SYNCHRONIZE_SCORE, \
        request_data, len(request_data))
示例#46
0
 def create_shared_resource(self, p_id, value):
     """
     Create a SharedResource in a given Program.
     :param p_id: Program id
     :param value: Shared resource value, DataType value
     :return: SharedResource id.
     """
     data_type_id = getattr(value, '_id')
     data = TSerialization.serialize(value)
     ticket_id = self._request_client.createSharedResource(
         p_id, data_type_id, data)
     future = asyncio.Future(loop=self._loop)
     asyncio.ensure_future(self.__wait_for_ticket(
         ticket_id, future, self._response_client.createSharedResource),
                           loop=self._loop)
     return future
示例#47
0
    def write(self, comm, comm_filename=None):
        '''
        Write communication to zip file.'

        Args:
            comm (Communication): communication to write to zip file
            comm_filename (str): desired filename of communication
                within zip file (by default the filename will be the
                communication id appended with a .concrete extension)
        '''
        if comm_filename is None:
            comm_filename = comm.id + '.concrete'

        thrift_bytes = TSerialization.serialize(
            comm, protocol_factory=factory.protocolFactory)

        self.zip_f.writestr(comm_filename, thrift_bytes)
示例#48
0
def playing_plot_on_event_actor_kill_npc(message_type, channel, channel_type, serialize):
  # 获取请求代理
  request_proxy = variable_table.get_variable(ccvariable.ttypes.Variable.REQUEST_PROXY)

  # 事件消息
  message = ccevent.actor.ttypes.EventActorKillNpc()
  # 反序列化
  TSerialization.deserialize(message, serialize)

  # 获取玩家所在副本ID
  playing_id = plot_types.PlayingManager.get_actor_playing(message.actor_)
  if playing_id == None or playing_id == 0:
    proxy.Logging.error("plot_types.PlayingManager.get_actor_playing(%d) failed." % message.actor_)
    return None

  # 获取副本对象
  playing = plot_types.PlayingManager.get(playing_id)
  if playing == None:
    proxy.Logging.error("plot_types.PlayingManager.get(%d) failed." % playing_id)
    return None

  # 获取副本玩家对象
  actor = playing.get_actor(message.actor_)
  if actor == None:
    proxy.Logging.error("playing.get_actor(%d) failed." % message.actor_)
    return None

  # 增加杀死NPC个数
  actor.inc_kill_npc(message.npc_template_)

  # 请求消息
  request = ccrequest.playing.ttypes.RequestPlayingSynchronizeScore()
  # 玩家ID
  request.actor_ = message.actor_
  # 分数对象(得分类型=杀死NPC, key=NPC模板ID, value=当前杀死的个数)
  score_field = ccentity.playing.ttypes.PlayingScoreField(ccentity.playing.ttypes.PlayingScoreType.KILL_NPC, \
      message.npc_template_, actor.get_kill_npc(message.npc_template_))
  request.score_ = score_field
  # 序列化消息
  request_data = TSerialization.serialize(request)
  # 发送请求
  request_proxy.request(ccrequest.ttypes.RequestType.REQUEST_PLAYING_SYNCHRONIZE_SCORE, \
      request_data, len(request_data))

  log.log_debug("玩家(%d) 杀死 NPC(%d) 个数(%d)" % (message.actor_, score_field.key_, \
        score_field.value_))
示例#49
0
    def write(self, comm, comm_filename=None):
        '''
        Write communication to zip file.'

        Args:
            comm (Communication): communication to write to zip file
            comm_filename (str): desired filename of communication
                within zip file (by default the filename will be the
                communication id appended with a .concrete extension)
        '''
        if comm_filename is None:
            comm_filename = comm.id + '.concrete'

        thrift_bytes = TSerialization.serialize(
            comm, protocol_factory=factory.protocolFactory)

        self.zip_f.writestr(comm_filename, thrift_bytes)
示例#50
0
    def send_data(self):
        request = BmnRequest()
        request.tid = -2
        request.type = RequestType.HEARTBEAT

        # 序列化消息
        reqData = TSerialization.serialize(request)
        # 获取消息长度
        length_data = len(reqData)
        # 将消息长度转成4字节的byte数组
        lenData = struct.pack('>i', length_data)

        # 先发送消息长度
        self.client.send(lenData)
        # 再发送消息体
        self.client.send(reqData)

        print("send heartbeat request success.")
示例#51
0
    def app_info(self, app, targetApp=None):
        """
        Request a token containing application info, optionally with a target
        securityId in the token. If the targetApp is specified, you will be
        able to send this token to another application, and it will validate on
        the other end. You should set txApp to
        ApplicationConfiguration(ezconfig).getSecurityID() if you are sending
        this to another thrift service within your application
        @param app: app to request info about
        @param targetApp: optionally, request security service to include a
        targetSecurityId in the token
        @return the EzSecurityToken
        """
        request = self.build_request(app, "", targetApp)

        # look in the cache
        cache_key = self._cache_key(targetApp, app)
        try:
            token = EzSecurityClient.app_cache[cache_key]
            if self._validateToken(token):
                self.log.info("Using token from cache")
                return token
            else:
                self.log.info("Token in cache was invalid. getting new")
        except KeyError:
            # it's not in the cache, continue
            pass

        client = self._getClient()

        self.log.info("Requesting app info for %s from EzSecurity", app)
        token = client.appInfo(request, self._sign(tser.serialize(request)))
        self.log.info("Received app info for %s from EzSecurity", app)

        self._returnClient(client)

        if not self._validateToken(token):
            self.log.error("Invalid token received from EzSecurity")
            token = None
        else:
            self.log.info("Storing app info %s in cache", app)
            EzSecurityClient.app_cache[cache_key] = token

        return token
示例#52
0
def main():
    # Make stdout output UTF-8, preventing "'ascii' codec can't encode" errors
    sys.stdout = codecs.getwriter('utf8')(sys.stdout)

    parser = argparse.ArgumentParser(
        description="Pretty Print a Concrete file")
    parser.add_argument('--concrete_type', default='communication',
                        choices=['communication', 'tokenlattice'],
                        help='Default: communication')
    parser.add_argument('--protocol', default='simple',
                        choices=['simple', 'TJSONProtocol'],
                        help='Default: simple')
    parser.add_argument('--remove-timestamps', action='store_true',
                        help="Removes timestamps from JSON output")
    parser.add_argument('--remove-uuids', action='store_true',
                        help="Removes UUIDs from JSON output")
    parser.add_argument('concrete_file')
    parser.add_argument('json_file', nargs='?', default='STDOUT')
    concrete.version.add_argparse_argument(parser)
    args = parser.parse_args()

    if args.protocol == 'simple':
        if args.concrete_type == 'communication':
            json_communication = communication_file_to_json(
                args.concrete_file,
                remove_timestamps=args.remove_timestamps,
                remove_uuids=args.remove_uuids
            )
        else:
            json_communication = tokenlattice_file_to_json(args.concrete_file)
    else:
        if args.concrete_type == 'communication':
            comm = read_communication_from_file(args.concrete_file)
            json_communication = TSerialization.serialize(
                comm, TJSONProtocol.TJSONProtocolFactory())
        else:
            raise NotImplementedError

    if args.json_file == 'STDOUT':
        print json_communication
    else:
        f = codecs.open(args.json_file, "w", encoding="utf-8")
        f.write(json_communication)
        f.close()
def convert_communication(input_bytes, input_protocol_factory,
                          output_protocol_factory):
    """
    Convert an input byte stream (to be read in as an
    input_protocol_factory type) to an output byte stream (with encoding
    output_protocol_factory type).

    * input_bytes: Input file byte stream
    * input_protocol_factory: Callable factory function for input
      encoding, e.g., TBinaryProtocol.TBinaryProtocolFactory.
    * output_protocol_factory: Callable factory function for output
      encoding, e.g., TCompactProtocol.TCompactProtocolFactory.
    """
    comm = Communication()
    TSerialization.deserialize(comm,
                               input_bytes,
                               protocol_factory=input_protocol_factory())
    output_bytes = TSerialization.serialize(
        comm, protocol_factory=output_protocol_factory())
    return output_bytes
示例#54
0
def convert_communication(input_bytes, input_protocol_factory,
                          output_protocol_factory):
    """
    Convert an input byte stream (to be read in as an
    input_protocol_factory type) to an output byte stream (with encoding
    output_protocol_factory type).

    * input_bytes: Input file byte stream
    * input_protocol_factory: Callable factory function for input
      encoding, e.g., TBinaryProtocol.TBinaryProtocolAcceleratedFactory.
    * output_protocol_factory: Callable factory function for output
      encoding, e.g., TCompactProtocol.TCompactProtocolAcceleratedFactory.
    """
    comm = Communication()
    TSerialization.deserialize(comm,
                               input_bytes,
                               protocol_factory=input_protocol_factory())
    output_bytes = TSerialization.serialize(
        comm, protocol_factory=output_protocol_factory())
    return output_bytes
示例#55
0
    def write(self, comm, comm_filename=None):
        if comm_filename is None:
            comm_filename = comm.uuid.uuidString + '.concrete'

        thrift_bytes = TSerialization.serialize(
            comm, protocol_factory=factory.protocolFactory)

        file_like_obj = cStringIO.StringIO(thrift_bytes)

        comm_tarinfo = tarfile.TarInfo()
        comm_tarinfo.type = tarfile.REGTYPE
        comm_tarinfo.name = comm_filename
        comm_tarinfo.size = len(thrift_bytes)
        comm_tarinfo.mode = 0644
        comm_tarinfo.mtime = time.time()
        comm_tarinfo.uid = os.getuid()
        comm_tarinfo.uname = pwd.getpwuid(os.getuid()).pw_name
        comm_tarinfo.gid = os.getgid()
        comm_tarinfo.gname = grp.getgrgid(os.getgid()).gr_name

        self.tarfile.addfile(comm_tarinfo, file_like_obj)
    encoding_input = KNOWN_CONVERSIONS[
        args.direction][0] if args.iprotocol is None else PROTOCOLS[
            args.iprotocol]
    encoding_output = KNOWN_CONVERSIONS[
        args.direction][1] if args.oprotocol is None else PROTOCOLS[
            args.oprotocol]
    if ofile_encoding == "gzip":
        out_writer = gzip.GzipFile(args.output_file, 'wb')
    else:
        out_writer = open(args.output_file, 'w')
    if ifile_encoding == 'gzip':
        f = gzip.GzipFile(args.input_file)
        transportIn = TTransport.TFileObjectTransport(f)
        protocolIn = encoding_input().getProtocol(transportIn)
        while True:
            try:
                comm = Communication()
                comm.read(protocolIn)
                output_bytes = TSerialization.serialize(
                    comm, protocol_factory=encoding_output())
                out_writer.write(output_bytes)
            except EOFError:
                break
        f.close()
    else:
        convert(input_file_path=args.input_file,
                output_file_path=args.output_file,
                input_protocol_factory=encoding_input,
                output_protocol_factory=encoding_output)
    out_writer.close()
示例#57
0
def serialize(obj):
    return TSerialization.serialize(obj, protocol_factory=FACTORY)
示例#58
0
 def serialize(self, obj: T) -> bytes:
     if not isinstance(obj, self.thrift_class):
         raise TypeError(
             f"object to serialize must be of {self.thrift_class.__name__} type"
         )
     return TSerialization.serialize(obj, self.factory)