<commit_before>"""
Author: <NAME>

Start date: March 27, 2009

Description: Abstracts the task of looking up and advertising servers and forwarders.

"""

include centralizedadvertise.repy
include deserialize.py

# What should we register as?
NAT_FORWARDER_ADVERTISE_KEY = "__NAT__FORWARDER__"
NAT_SRV_PREFIX = "__NAT_SRV__"

# Limit of forwarder lookups
NAT_MAX_LOOKUP = 50

NAT_ADVERTISE_INTERVAL = 30
NAT_ADVERTISE_TTL = 3*NAT_ADVERTISE_INTERVAL

# Pools the advertisements, so that they can be done in one thread
# Maps key-> value
NAT_ADVERTISE_POOL = {}

# Enable allows toggling of periodic advertisement
# Run controls the advertisement thread, False will stop the thread
NAT_ADVERTISE_STATE = {"enable":False,"run":False}

# Registers the forwarder so that clients using the NATLayer can find us
def forwarder_advertise(ip, serverport, clientport):
  # Generate the value to advertise
  valueDict = {"ip":ip, "server":serverport, "client":clientport}
  value = str(valueDict)
  
  # Add to the advertising pool
  NAT_ADVERTISE_POOL[NAT_FORWARDER_ADVERTISE_KEY] = value


# Advertises a server, so that other NATLayer users can connect
def server_advertise(key, forwarderIP, forwarderCltPort):
  # Generate the value to advertise
  valueDict = {"key":key, "forwarder":forwarderIP, "port":forwarderCltPort}
  value = str(valueDict)

  # Alter the key, add the prefix
  key = NAT_SRV_PREFIX + key

  # Add to the advertising pool
  NAT_ADVERTISE_POOL[key] = value
    
    
# Lookup a forwarder so that we can connect
def forwarder_lookup():
  # Get the list of forwarders
  forwarders = centralizedadvertise_lookup(NAT_FORWARDER_ADVERTISE_KEY, NAT_MAX_LOOKUP)
  
  # Safety check..
  if len(forwarders) == 0:
    raise Exception, "No forwarders could be found!"
  
  # Grab a random forwarder
  index = int(randomfloat() * len(forwarders))
  
  # Get the info
  forwarderInfo = forwarders[index]
  forwarderInfo = deserialize(forwarderInfo)  
  
  # Return a tuple containing the IP and port for server and client
  return (forwaderInfo["ip"], forwarderInfo["server"], forwarderInfo["client"])


# Finds a server using the NATLayer
def server_lookup(key):
  # Get the proper key, add the prefix
  key = NAT_SRV_PREFIX + key

  # Fetch all the keys
  lst = centralizedadvertise_lookup(key, NAT_MAX_LOOKUP)
  num = len(lst)
  
  # Safety check...
  assert(num <= 1)
  if num == 0:
    raise Exception, "Host could not be found!"

  # Get the information about the server
  info = deserialize(lst[0])

  # Return a tuple of the forwarder IP port
  return (info["forwarder"], info["port"])


# Toggles advertisement
# Enable: allows or disallows advertisement of the pool
# threadRun: allows/starts or stops the advertisement thread
def nat_toggle_advertisement(enabled, threadRun=True): 
  NAT_ADVERTISE_STATE["enable"] = enabled
  
  # Start the advertisement thread if necessary
  if not NAT_ADVERTISE_STATE["run"] and threadRun:
    settimer(.1, _nat_advertise_thread, ())
  
  NAT_ADVERTISE_STATE["run"] = threadRun

  
# Launch this thread with settimer to handle the advertisement  
def _nat_advertise_thread():
  while True:
    # Check if advertising is enabled:
    if NAT_ADVERTISE_STATE["enable"]:
      # Advertise everything in the pool
      for (key, val) in NAT_ADVERTISE_POOL.items():
        try:
          centralizedadvertise_announce(key, val, NAT_ADVERTISE_TTL)
        except:
          pass
        
    # Check if we should terminate
    if not NAT_ADVERTISE_STATE["run"]:
      break
    
    # Sleep for a while
    sleep(NAT_ADVERTISE_INTERVAL)
<commit_msg>Added NAT prefix to advertisement functions to prevent namespace collision with user programs<commit_after>"""
Author: <NAME>

Start date: March 27, 2009

Description: Abstracts the task of looking up and advertising servers and forwarders.

"""

include centralizedadvertise.repy
include deserialize.py

# What should we register as?
NAT_FORWARDER_ADVERTISE_KEY = "__NAT__FORWARDER__"
NAT_SRV_PREFIX = "__NAT_SRV__"

# Limit of forwarder lookups
NAT_MAX_LOOKUP = 50

NAT_ADVERTISE_INTERVAL = 30
NAT_ADVERTISE_TTL = 3*NAT_ADVERTISE_INTERVAL

# Pools the advertisements, so that they can be done in one thread
# Maps key-> value
NAT_ADVERTISE_POOL = {}

# Enable allows toggling of periodic advertisement
# Run controls the advertisement thread, False will stop the thread
NAT_ADVERTISE_STATE = {"enable":False,"run":False}

# Registers the forwarder so that clients using the NATLayer can find us
def nat_forwarder_advertise(ip, serverport, clientport):
  # Generate the value to advertise
  valueDict = {"ip":ip, "server":serverport, "client":clientport}
  value = str(valueDict)
  
  # Add to the advertising pool
  NAT_ADVERTISE_POOL[NAT_FORWARDER_ADVERTISE_KEY] = value


# Advertises a server, so that other NATLayer users can connect
def nat_server_advertise(key, forwarderIP, forwarderCltPort):
  # Generate the value to advertise
  valueDict = {"key":key, "forwarder":forwarderIP, "port":forwarderCltPort}
  value = str(valueDict)

  # Alter the key, add the prefix
  key = NAT_SRV_PREFIX + key

  # Add to the advertising pool
  NAT_ADVERTISE_POOL[key] = value

# Stops advertising a server key    
def nat_stop_server_advertise(key):
  if key in NAT_ADVERTISE_POOL:
    del NAT_ADVERTISE_POOL[key]
    
# Lookup a forwarder so that we can connect
def nat_forwarder_lookup():
  # Get the list of forwarders
  forwarders = centralizedadvertise_lookup(NAT_FORWARDER_ADVERTISE_KEY, NAT_MAX_LOOKUP)
  
  # Safety check..
  if len(forwarders) == 0:
    raise Exception, "No forwarders could be found!"
  
  # Grab a random forwarder
  index = int(randomfloat() * len(forwarders))
  
  # Get the info
  forwarderInfo = forwarders[index]
  forwarderInfo = deserialize(forwarderInfo)  
  
  # Return a tuple containing the IP and port for server and client
  return (forwaderInfo["ip"], forwarderInfo["server"], forwarderInfo["client"])


# Finds a server using the NATLayer
def nat_server_lookup(key):
  # Get the proper key, add the prefix
  key = NAT_SRV_PREFIX + key

  # Fetch all the keys
  lst = centralizedadvertise_lookup(key, NAT_MAX_LOOKUP)
  num = len(lst)
  
  # Safety check...
  assert(num <= 1)
  if num == 0:
    raise Exception, "Host could not be found!"

  # Get the information about the server
  info = deserialize(lst[0])

  # Return a tuple of the forwarder IP port
  return (info["forwarder"], info["port"])


# Toggles advertisement
# Enable: allows or disallows advertisement of the pool
# threadRun: allows/starts or stops the advertisement thread
def nat_toggle_advertisement(enabled, threadRun=True): 
  NAT_ADVERTISE_STATE["enable"] = enabled
  
  # Start the advertisement thread if necessary
  if not NAT_ADVERTISE_STATE["run"] and threadRun:
    settimer(.1, _nat_advertise_thread, ())
  
  NAT_ADVERTISE_STATE["run"] = threadRun

  
# Launch this thread with settimer to handle the advertisement  
def _nat_advertise_thread():
  while True:
    # Check if advertising is enabled:
    if NAT_ADVERTISE_STATE["enable"]:
      # Advertise everything in the pool
      for (key, val) in NAT_ADVERTISE_POOL.items():
        try:
          centralizedadvertise_announce(key, val, NAT_ADVERTISE_TTL)
        except:
          pass
    
    # Sleep for a while
    sleep(NAT_ADVERTISE_INTERVAL)
              
    # Check if we should terminate
    if not NAT_ADVERTISE_STATE["run"]:
      break

{'fraction_non_alphanumeric': 0.07783623364359908, 'fraction_numerical': 0.004316740860650209, 'mean_word_length': 4.254429482636428, 'pattern_counts': {'":': 16, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5879569', 'n_tokens_mistral': 2464, 'n_tokens_neox': 2312, 'n_words': 743}