Source code for SimulaQron.general.hostConfig
#
# Copyright (c) 2017, Stephanie Wehner and Axel Dahlberg
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
# must display the following acknowledgement:
# This product includes software developed by Stephanie Wehner, QuTech.
# 4. Neither the name of the QuTech organization nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ''AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import sys, socket, struct
from twisted.spread import pb
[docs]class networkConfig(pb.Referenceable):
def __init__(self, filename):
"""
Initialize by reading in the configuration file.
"""
# Dictionary where we will keep host details, indexed by node name (e.g. Alice)
self.hostDict = {}
# Read config file
self.read_config(filename)
[docs] def read_config(self, filename):
"""
Reads the configuration file in which each line has the form: node name, hostname, port number.
For example:
Alice, localhost, 8888
"""
with open(filename) as confFile:
for line in confFile:
if not line.startswith("#"):
words = line.split(',')
# We will simply ignore lines which are not of the right form
if len(words) == 3:
newHost = host(words[0].strip(), words[1].strip(), words[2].strip())
self.hostDict[words[0]] = newHost
[docs] def print_details(self, name):
"""
Prints the details of the specified node with name.
"""
host = self.hostDict[name]
print("Host details of ", name, ": ", host.hostname, ":", host.port)
[docs]class host(pb.Referenceable):
def __init__(self, name, hostname, port):
"""
Initialize the details of the host. For now, we just keep the following:
name informal name of the host (e.g. Alice)
hostname name of the node on the network (e.g. localhost or yournode.qutech.nl)
port port number on hostname
"""
self.name = name
self.hostname = hostname
self.port = int(port)
# Lookup IP address - store in network byte order(!)
addr = socket.gethostbyname(hostname)
packedIP = socket.inet_aton(addr)
self.ip = struct.unpack("!L", packedIP)[0]
# Connection identifiers used after connected
self.factory = 0
self.root = 0
self.defer = 0