|
| 1 | +# Copyright (c) 2017, Stephanie Wehner and Axel Dahlberg |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# Redistribution and use in source and binary forms, with or without |
| 5 | +# modification, are permitted provided that the following conditions are met: |
| 6 | +# 1. Redistributions of source code must retain the above copyright |
| 7 | +# notice, this list of conditions and the following disclaimer. |
| 8 | +# 2. Redistributions in binary form must reproduce the above copyright |
| 9 | +# notice, this list of conditions and the following disclaimer in the |
| 10 | +# documentation and/or other materials provided with the distribution. |
| 11 | +# 3. All advertising materials mentioning features or use of this software |
| 12 | +# must display the following acknowledgement: |
| 13 | +# This product includes software developed by Stephanie Wehner, QuTech. |
| 14 | +# 4. Neither the name of the QuTech organization nor the |
| 15 | +# names of its contributors may be used to endorse or promote products |
| 16 | +# derived from this software without specific prior written permission. |
| 17 | +# |
| 18 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ''AS IS'' AND ANY |
| 19 | +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 | +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY |
| 22 | +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 | +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 24 | +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 25 | +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 | +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | + |
| 29 | +import logging |
| 30 | + |
| 31 | +from twisted.internet.defer import inlineCallbacks |
| 32 | +from twisted.internet.protocol import Protocol, connectionDone |
| 33 | + |
| 34 | +from cqc.cqcHeader import CQC_HDR_LENGTH, CQC_VERSION, CQCHeader |
| 35 | + |
| 36 | +############################################################################### |
| 37 | +# |
| 38 | +# CQC Protocol |
| 39 | +# |
| 40 | +# Execute the CQC Protocol giving access to the SimulaQron backend via the |
| 41 | +# universal interface. |
| 42 | +# |
| 43 | + |
| 44 | + |
| 45 | +class CQCProtocol(Protocol): |
| 46 | + # Dictionary storing the next unique qubit id for each used app_id |
| 47 | + _next_q_id = {} |
| 48 | + |
| 49 | + # Dictionary storing the next unique entanglement id for each used |
| 50 | + # (host_app_id,remote_node,remote_app_id) |
| 51 | + _next_ent_id = {} |
| 52 | + |
| 53 | + def __init__(self, factory): |
| 54 | + |
| 55 | + # CQC Factory, including our connection to the SimulaQron backend |
| 56 | + self.factory = factory |
| 57 | + |
| 58 | + # Default application ID, typically one connection per application but |
| 59 | + # we will deliberately NOT check for that since this is the task of |
| 60 | + # higher layers or an OS |
| 61 | + self.app_id = 0 |
| 62 | + |
| 63 | + # Define the backend to use. Is a setting in settings.ini |
| 64 | + self.messageHandler = factory.backend |
| 65 | + |
| 66 | + # Flag to determine whether we already received _all_ of the CQC header |
| 67 | + self.gotCQCHeader = False |
| 68 | + |
| 69 | + # Header for which we are currently processing a packet |
| 70 | + self.currHeader = None |
| 71 | + |
| 72 | + # Buffer received data (which may arrive in chunks) |
| 73 | + self.buf = None |
| 74 | + |
| 75 | + # Convenience |
| 76 | + self.name = self.factory.name |
| 77 | + |
| 78 | + logging.debug("CQC %s: Initialized Protocol", self.name) |
| 79 | + |
| 80 | + def connectionMade(self): |
| 81 | + pass |
| 82 | + |
| 83 | + def connectionLost(self, reason=connectionDone): |
| 84 | + pass |
| 85 | + |
| 86 | + def dataReceived(self, data): |
| 87 | + """ |
| 88 | + Receive data. We will always wait to receive enough data for the |
| 89 | + header, and then the entire packet first before commencing processing. |
| 90 | + """ |
| 91 | + # Read whatever we received into a buffer |
| 92 | + if self.buf: |
| 93 | + self.buf = self.buf + data |
| 94 | + else: |
| 95 | + self.buf = data |
| 96 | + |
| 97 | + # If we don't have the CQC header yet, try and read it in full. |
| 98 | + if not self.gotCQCHeader: |
| 99 | + if len(self.buf) < CQC_HDR_LENGTH: |
| 100 | + # Not enough data for CQC header, return and wait for the rest |
| 101 | + return |
| 102 | + |
| 103 | + # Got enough data for the CQC Header so read it in |
| 104 | + self.gotCQCHeader = True |
| 105 | + raw_header = self.buf[0:CQC_HDR_LENGTH] |
| 106 | + self.currHeader = CQCHeader(raw_header) |
| 107 | + |
| 108 | + # Remove the header from the buffer |
| 109 | + self.buf = self.buf[CQC_HDR_LENGTH: len(self.buf)] |
| 110 | + |
| 111 | + logging.debug("CQC %s: Read CQC Header: %s", self.name, self.currHeader.printable()) |
| 112 | + |
| 113 | + # Check whether we already received all the data |
| 114 | + if len(self.buf) < self.currHeader.length: |
| 115 | + # Still waiting for data |
| 116 | + logging.debug( |
| 117 | + "CQC %s: Incomplete data. Waiting. Current length %s, " "required length %s", |
| 118 | + self.name, |
| 119 | + len(self.buf), |
| 120 | + self.currHeader.length, |
| 121 | + ) |
| 122 | + return |
| 123 | + |
| 124 | + # We got the header and all the data for this packet. Start processing. |
| 125 | + # Update our app ID |
| 126 | + self.app_id = self.currHeader.app_id |
| 127 | + # Invoke the relevant message handler, processing the possibly |
| 128 | + # remaining data |
| 129 | + try: |
| 130 | + self._parseData(self.currHeader, self.buf[0: self.currHeader.length]) |
| 131 | + except Exception as e: |
| 132 | + print(e) |
| 133 | + import traceback |
| 134 | + |
| 135 | + traceback.print_exc() |
| 136 | + |
| 137 | + # if self.currHeader.tp in self.messageHandlers: |
| 138 | + # self.messageHandlers[self.currHeader.tp](self.currHeader, ) |
| 139 | + # else: |
| 140 | + # self._send_back_cqc(self.currHeader, CQC_ERR_UNSUPP) |
| 141 | + |
| 142 | + # Reset and await the next packet |
| 143 | + self.gotCQCHeader = False |
| 144 | + |
| 145 | + # Check if we received data already for the next packet, if so save it |
| 146 | + if self.currHeader.length < len(self.buf): |
| 147 | + self.buf = self.buf[self.currHeader.length: len(self.buf)] |
| 148 | + self.dataReceived(b"") |
| 149 | + else: |
| 150 | + self.buf = None |
| 151 | + |
| 152 | + @inlineCallbacks |
| 153 | + def _parseData(self, header, data): |
| 154 | + try: |
| 155 | + yield self.messageHandler.handle_cqc_message(header, data) |
| 156 | + messages = self.messageHandler.retrieve_return_messages() |
| 157 | + except Exception as e: |
| 158 | + raise e |
| 159 | + |
| 160 | + if messages: |
| 161 | + # self.factory._lock.acquire() |
| 162 | + for msg in messages: |
| 163 | + self.transport.write(msg) |
| 164 | + # self.factory._lock.release() |
| 165 | + |
| 166 | + def _send_back_cqc(self, header, msgType, length=0): |
| 167 | + """ |
| 168 | + Return a simple CQC header with the specified type. |
| 169 | +
|
| 170 | + header CQC header of the packet we respond to |
| 171 | + msgType Message type to return |
| 172 | + length Length of additional message |
| 173 | + """ |
| 174 | + hdr = CQCHeader() |
| 175 | + hdr.setVals(CQC_VERSION, msgType, header.app_id, length) |
| 176 | + |
| 177 | + msg = hdr.pack() |
| 178 | + self.transport.write(msg) |
0 commit comments