|
| 1 | +# |
| 2 | +# Copyright (c) 2017, Stephanie Wehner and Axel Dahlberg |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# Redistribution and use in source and binary forms, with or without |
| 6 | +# modification, are permitted provided that the following conditions are met: |
| 7 | +# 1. Redistributions of source code must retain the above copyright |
| 8 | +# notice, this list of conditions and the following disclaimer. |
| 9 | +# 2. Redistributions in binary form must reproduce the above copyright |
| 10 | +# notice, this list of conditions and the following disclaimer in the |
| 11 | +# documentation and/or other materials provided with the distribution. |
| 12 | +# 3. All advertising materials mentioning features or use of this software |
| 13 | +# must display the following acknowledgement: |
| 14 | +# This product includes software developed by Stephanie Wehner, QuTech. |
| 15 | +# 4. Neither the name of the QuTech organization nor the |
| 16 | +# names of its contributors may be used to endorse or promote products |
| 17 | +# derived from this software without specific prior written permission. |
| 18 | +# |
| 19 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ''AS IS'' AND ANY |
| 20 | +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 21 | +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY |
| 23 | +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 24 | +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 25 | +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 26 | +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 28 | +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + |
| 30 | +import logging |
| 31 | +import os |
| 32 | +import socket |
| 33 | + |
| 34 | +from simulaqron.general.hostConfig import networkConfig |
| 35 | +from cqc.cqcHeader import CQCHeader, CQC_TP_HELLO, CQC_VERSION |
| 36 | +from simulaqron.toolbox import get_simulaqron_path |
| 37 | + |
| 38 | + |
| 39 | +##################################################################################################### |
| 40 | +# |
| 41 | +# init |
| 42 | +# |
| 43 | +def init(name, cqcFile=None): |
| 44 | + """ |
| 45 | + Initialize a connection to the cqc server with the name given as input. |
| 46 | + A path to a configure file for the cqc network can be given, |
| 47 | + if it's not given the config file 'config/cqcNodes.cfg' will be used. |
| 48 | + Returns a socket object. |
| 49 | + """ |
| 50 | + |
| 51 | + # This file defines the network of CQC servers interfacing to virtual quantum nodes |
| 52 | + if cqcFile is None: |
| 53 | + simulaqron_path = get_simulaqron_path.main() |
| 54 | + cqcFile = os.path.join(simulaqron_path, "config/cqcNodes.cfg") |
| 55 | + |
| 56 | + # Read configuration files for the cqc network |
| 57 | + cqcNet = networkConfig(cqcFile) |
| 58 | + |
| 59 | + # Host data |
| 60 | + if name in cqcNet.hostDict: |
| 61 | + myHost = cqcNet.hostDict[name] |
| 62 | + else: |
| 63 | + logging.error("The name '%s' is not in the cqc network.", name) |
| 64 | + raise LookupError("The name '%s' is not in the cqc network.".format(name)) |
| 65 | + |
| 66 | + addr = myHost.addr |
| 67 | + |
| 68 | + # Connect to cqc server and run protocol |
| 69 | + cqc = None |
| 70 | + try: |
| 71 | + cqc = socket.socket(addr[0], addr[1], addr[2]) |
| 72 | + except socket.error: |
| 73 | + logging.error("Could not connect to cqc server: %s", name) |
| 74 | + try: |
| 75 | + cqc.connect(addr[4]) |
| 76 | + except socket.error: |
| 77 | + cqc.close() |
| 78 | + logging.error("Could not connect to cqc server: %s", name) |
| 79 | + return cqc |
| 80 | + |
| 81 | + |
| 82 | +##################################################################################################### |
| 83 | +# |
| 84 | +# main |
| 85 | +# |
| 86 | +def main(): |
| 87 | + |
| 88 | + # In this example, we are Alice. |
| 89 | + myName = "Alice" |
| 90 | + |
| 91 | + # Initialize the connection |
| 92 | + cqc = init(myName) |
| 93 | + |
| 94 | + # Send Hello message |
| 95 | + print("App {} tells CQC: 'HELLO'".format(myName)) |
| 96 | + hdr = CQCHeader() |
| 97 | + hdr.setVals(CQC_VERSION, CQC_TP_HELLO, 0, 0) |
| 98 | + msg = hdr.pack() |
| 99 | + cqc.send(msg) |
| 100 | + |
| 101 | + # Receive return message |
| 102 | + data = cqc.recv(192) |
| 103 | + hdr = CQCHeader(data) |
| 104 | + if hdr.tp == CQC_TP_HELLO: |
| 105 | + print("CQC tells App {}: 'HELLO'".format(myName)) |
| 106 | + else: |
| 107 | + print("Did not receive a hello message, but rather: {}".format(hdr.printable())) |
| 108 | + |
| 109 | + # Close the connection |
| 110 | + cqc.close() |
| 111 | + |
| 112 | + |
| 113 | +################################################################################################## |
| 114 | +main() |
0 commit comments