示例#1
0
文件: pfxm3.py 项目: woodlums/graspy
    def run(self):

        grasp.tprint("Ready to negotiate", obj1.name,
                     "as listener, unless pool is empty")

        while True:
            if len(ppool) > 0:
                #attempt to listen for negotiation
                err, snonce, answer = grasp.listen_negotiate(asa_nonce, obj1)
                if err:
                    grasp.tprint("listen_negotiate error:", grasp.etext[err])
                    time.sleep(5)  #to calm things if there's a looping error
                else:
                    #got a new negotiation request; kick off a separate negotiator
                    #so that multiple requests can be handled in parallel
                    negotiator(snonce, answer).start()
            else:
                time.sleep(5)  #hoping the pool will refill
示例#2
0
###################################
# Negotiate EX3 as listener for ever
###################################

obj3.value = ["NZD", 0]
grasp.tprint("Ready to negotiate EX3 as listener")
grasp.ttprint(
    "(Note: Cyrillic test case fails in a Windows console window, OK in IDLE window.)"
)

grasp.init_bubble_text("Briggs")

while True:
    #start of a negotiating session

    #create a random amount of resource and a random waiting time
    reserves = grasp._prng.randint(100, 400)
    wt = grasp._prng.randint(9000, 20000)
    grasp.tprint("Reserves: $", reserves, "Wait:", wt, "ms")

    #attempt to listen for negotiation
    err, snonce, answer = grasp.listen_negotiate(asa_nonce, obj3)
    if err:
        grasp.tprint("listen_negotiate error:", grasp.etext[err])
        time.sleep(5)  #to calm things if there's a looping error
    else:
        #got a new negotiation request; kick off a separate negotiator
        #so that multiple requests can be handled in parallel
        negotiator(snonce, answer).start()
示例#3
0
                                       key_size=2048,
                                       backend=default_backend())
public_key = private_key.public_key()
my_pem = public_key.public_bytes(
    encoding=serialization.Encoding.PEM,
    format=serialization.PublicFormat.SubjectPublicKeyInfo)

###################################
# Start flooding out the public key
###################################

flood_obj.value = my_pem
flooder().start()
grasp.tprint("Flooding", flood_obj.name, "for ever")

###################################
# Negotiate as listener for ever
###################################

while True:
    #grasp.tprint("Encryption", grasp.crypto, "(should be False)")
    # listen for negotiation request
    err, snonce, request = grasp.listen_negotiate(asa_nonce, keys_obj)
    if err:
        grasp.tprint("listen_negotiate error:", grasp.etext[err])
        time.sleep(5)  #to calm things if there's a looping error
    else:
        #got a new negotiation request; kick off a separate negotiator
        #so that multiple requests can be handled in parallel
        negotiator(snonce, request).start()
示例#4
0
obj3 = grasp.objective("EX3")
obj3.neg = True

err = grasp.register_obj(asa_handle, obj3)
if not err:
    grasp.tprint("Objective EX3 registered OK")
else:
    exit()

grasp.init_bubble_text(name)

###################################
# Negotiate EX3 as listener for ever
###################################
grasp.tprint("Ready to negotiate EX3 as server")

while True:
    #start of a negotiating session

    obj3.value = "Hello!"

    #attempt to listen for negotiation
    err, shandle, answer = grasp.listen_negotiate(asa_handle, obj3)
    if err:
        grasp.tprint("listen_negotiate error:", grasp.etext[err])
        time.sleep(5)  #to calm things if there's a looping error
    else:
        #got a new negotiation request; kick off a separate negotiator
        #so that multiple requests can be handled in parallel
        negotiator(shandle, answer).start()
示例#5
0
 def run(self):
     global cheat_nonce
     reserves = grasp._prng.randint(100, 400)
     wt = grasp._prng.randint(15000, 40000)
     grasp.tprint("Reserves: $", reserves, "wait:", wt)
     err, asa_nonce = grasp.register_asa("Neg1")  #assume it worked
     obj = grasp.objective("EX2")
     obj.neg = True
     #obj.loop_count = 2  #for testing purposes
     grasp.register_obj(asa_nonce, obj)  #assume it worked
     cheat_nonce = asa_nonce  #pass the nonce to the other negotiator!
     #attempt to listen
     err, snonce, answer = grasp.listen_negotiate(asa_nonce, obj)
     if err:
         grasp.tprint("listen_negotiate error:", grasp.etext[err])
     else:
         grasp.tprint("listened, answer", answer.name, answer.value)
         grasp.tprint("Source was", snonce.id_source)
         result = True
         reason = None
         concluded = False
         if answer.value[0] != "NZD":
             result = False
             reason = "Invalid currency"
         elif answer.value[1] > reserves / 2:
             answer.value = ["NZD", int(reserves / 2)]
             err, temp, answer2 = grasp.negotiate_step(
                 asa_nonce, snonce, answer, 1000)
             grasp.tprint("Step1 gave:", err, temp, answer2)
             if (not err) and (not temp):
                 concluded = True
                 grasp.tprint("Negotiation succeeded", answer2.value)
             elif not err:
                 err1 = grasp.negotiate_wait(asa_nonce, snonce, wt)
                 grasp.tprint("Tried wait:", grasp.etext[err1])
                 time.sleep(
                     20)  #note - if wt<20000 this tests anomaly handling
                 grasp.tprint("Woke up")
                 answer2.value = ["NZD", int(0.75 * reserves)]
                 err2, temp, answer3 = grasp.negotiate_step(
                     asa_nonce, snonce, answer2, 1000)
                 grasp.tprint("Step2 gave:", err2, temp, answer3)
                 if (not err2) and (not temp):
                     concluded = True
                     grasp.tprint("Negotiation succeeded", answer3.value)
                 elif not err2:
                     result = False
                     if reserves % 2:
                         reason = "Insufficient funds"
                     else:
                         reason = u"Недостаточно средств"
                 else:
                     #other end rejected
                     concluded = True
                     grasp.tprint("Peer reject2:", answer3)
             else:
                 #other end rejected
                 concluded = True
                 grasp.tprint("Peer reject1:", answer2)
         else:  #can accept the requested value
             pass
         if not concluded:
             err = grasp.end_negotiate(asa_nonce,
                                       snonce,
                                       result,
                                       reason=reason)
             if err:
                 grasp.tprint("end_negotiate error:", grasp.etext[err])
     grasp.tprint("Exit")
示例#6
0
supported_obj.loop_count = 4
supported_obj.neg = True

err = grasp.register_obj(asa_nonce, supported_obj)
if not err:
    grasp.tprint("Objective", supported_obj.name, "registered OK")
else:
    exit()

###################################
# Set up pretty printing
###################################

grasp.init_bubble_text("pusher")
grasp.tprint("pusher is listening")

###################################
# Negotiate as listener for ever
###################################

while True:
    # listen for negotiation request
    err, snonce, request = grasp.listen_negotiate(asa_nonce, supported_obj)
    if err:
        grasp.tprint("listen_negotiate error:", grasp.etext[err])
        time.sleep(5)  #to calm things if there's a looping error
    else:
        #got a new negotiation request; kick off a separate negotiator
        #so that multiple requests can be handled in parallel
        negotiator(snonce, request).start()