示例#1
0
def connect_handler(socket):
    def parser_handler(line):
        line = line.to_string().rstrip()
        if line.startswith("subscribe,"):
            topic_name = line.split(",", 2)[1]
            print "subscribing to %s"% topic_name
            topic = SharedData.get_set(topic_name)
            topic.add(socket.write_handler_id)
        elif line.startswith("unsubscribe,"):
            topic_name = line.split(",", 2)[1]
            print "unsubscribing from %s"% topic_name
            topic = SharedData.get_set(topic_name)
            topic.delete(socket.write_handler_id)
            if topic.empty:
                SharedData.remove_set(topic_name)
        elif line.startswith("publish,"):
            sp = line.split(',', 3)
            print "publishing to %s with %s"% (sp[1], sp[2])
            topic = SharedData.get_set(sp[1])
            print "topic is %s"% topic
            @topic.each
            def each(address):
                EventBus.send(address, Buffer.create_from_str(sp[2]))

    parser = RecordParser.new_delimited("\n", parser_handler)
    socket.data_handler(parser)
示例#2
0
    def test_delimited(self):
        str = ""
        iters = 100
        for i in range(0, iters):
            str += "line %s"% i
            if i != iters - 1:
                str += "\n" 

        self.lines = []
        def each_line(line):
            self.lines.append(line)
        parser = RecordParser.new_delimited("\n", each_line)
        
        parser.input(Buffer.create_from_str(str))

        count = 0
        for line in self.lines:    
            tu.azzert("line %i"% count == line.to_string())
            count += 1
        tu.test_complete()
    def test_delimited(self):
        str = ""
        iters = 100
        for i in range(0, iters):
            str += "line %s" % i
            if i != iters - 1:
                str += "\n"

        self.lines = []

        def each_line(line):
            self.lines.append(line)

        parser = RecordParser.new_delimited("\n", each_line)

        parser.input(Buffer.create_from_str(str))

        count = 0
        for line in self.lines:
            tu.azzert("line %i" % count == line.to_string())
            count += 1
        tu.test_complete()