Beispiel #1
0
    def send_transactions(self, productdata, member_id, queue):
        """ Send transaction requests to the server """
        self.logger.info("Sending transactions")

        message = Message()

        for (barcode, count) in productdata:
            packet = Packet(PacketTypes.Transaction, {"barcode":barcode, "memberid":member_id, "count":count})
            message.add_packet(packet)

        self.send_queue.put( MessagingQueueItem(PacketTypes.Transaction, message.get_xml(), queue) )
Beispiel #2
0
    def process_item(self, item):
        
        if item.type == PacketTypes.Ping:
            reply, recvd = self.send(item.message, True)
        else:
            reply, recvd = self.send(item.message)
            
        if recvd > 0:
            try:
                packets = Message.parse_xml(reply)
            
                for packet in packets:
                   
                    if packet.type in (PacketTypes.UserData, PacketTypes.UnknownUser):
                        item.queue.put(packet)
                    elif packet.type == PacketTypes.PingReply:
                        self.got_ping_reply(packet, recvd)
                    elif packet.type in (PacketTypes.ProductData, PacketTypes.UnknownProduct):
                        item.queue.put(packet)
                    elif packet.type in (PacketTypes.RandomProduct):
                        item.queue.put(packet)
                    elif packet.type == PacketTypes.Result:
                        if packet.data['action'] == PacketTypes.Transaction:
                            item.queue.put(packet)
                        if packet.data['action'] == PacketTypes.AddProduct:
                            item.queue.put(packet)
                        if packet.data['action'] == PacketTypes.AddCredit:
                            item.queue.put(packet)

            except InputException:
                pass ## Let the ping task deal with finding the server has gone
        else:
            if (item.type == PacketTypes.Ping):
                self.got_ping_reply(None, 0)
Beispiel #3
0
    def process_packets(self, packets):
        """ Process a list of packets """
        
        reply = Message() ## The reply starts as an empty message

        for packet in packets:

            self.logger.info("Handling '%s' packet..." % packet.type)

            if packet.type == PacketTypes.Ping:
                reply.add_packet( Packet(PacketTypes.PingReply) )
            elif packet.type == PacketTypes.GetProduct:
                reply.add_packet( self.get_product_from_data(packet.data) )
            elif packet.type == PacketTypes.GetUser:
                reply.add_packet( self.get_user_from_data(packet.data) )
            elif packet.type == PacketTypes.Transaction:
                reply.add_packet( self.apply_transaction_from_data(packet.data) )
            elif packet.type == PacketTypes.AddProduct:
                reply.add_packet ( self.add_product_from_data(packet.data) )
            elif packet.type == PacketTypes.AddCredit:
                reply.add_packet( self.add_credit_from_data(packet.data) )
            elif packet.type == PacketTypes.GetRandomProduct:
                reply.add_packet( self.get_random_product_packet() )
            elif packet.type == PacketTypes.PingReply:
                pass # No action required for ping reply
            else:
                self.logger.warning("Unknown packet '%s'" % packet.type)

        return reply.get_xml()
Beispiel #4
0
  def _decode(self, xfr):
    dp = EMPTY_DP
    mp = EMPTY_MP

    for h in xfr.headers:
      if isinstance(h, DeliveryProperties):
        dp = h
      elif isinstance(h, MessageProperties):
        mp = h

    ap = mp.application_headers
    enc, dec = get_codec(mp.content_type)
    content = dec(xfr.payload)
    msg = Message(content)
    msg.id = mp.message_id
    if ap is not None:
      msg.to = ap.get(TO)
      msg.subject = ap.get(SUBJECT)
    msg.user_id = mp.user_id
    if mp.reply_to is not None:
      msg.reply_to = reply_to2addr(mp.reply_to)
    msg.correlation_id = mp.correlation_id
    msg.durable = dp.delivery_mode == delivery_mode.persistent
    msg.redelivered = dp.redelivered
    msg.properties = mp.application_headers
    msg.content_type = mp.content_type
    msg._transfer_id = xfr.id
    return msg
Beispiel #5
0
    def test_message(self):
        """
        Test messaging.Message and its methods
        """
        from messaging import Message
        new_msg = Message(to=['0805'], text='Holla if console backend works')
        new_msg.send()

        new_msg = Message(to=['0805'], text='Holla if console backend works \
            but this time a message that is more than one hundred and \
            sixty characters. For a backend like the console backend, \
            this should not be an issue but with real backends, this \
            might break things.')
        new_msg.send()

        new_msg = Message(to=['0805'], text=' ')
        new_msg.send()

        new_msg = Message(text='Empty `to` field')
        new_msg.send()
Beispiel #6
0
from messaging import Message

pp = pprint.PrettyPrinter(indent=4)

log_file = "{}weather.log".format(LOG_PATH)
logging.basicConfig(level=logging.INFO,
                    format="%(message)s",
                    filename=log_file,
                    filemode='a+')

now  = datetime.datetime.now()
year = now.strftime("%Y")

count = 0

m = Message()

for z in RECIPIENTS:
    count      += 1
    zipcode     = z

    weather     = pywapi.get_weather_from_weather_com(zipcode)

    location    = weather['location']['name'].encode('utf-8')
    soonest     = weather['forecasts'][0]

    date        = soonest['date'].encode('utf-8')
    day_text    = soonest['day']['text'].encode('utf-8')
    high_c      = soonest['high'].encode('utf-8')
    high        = tempmath.celsius_to_fahr(int(high_c))
    low_c       = soonest['low'].encode('utf-8')