def _send_request(self, request): # Called in tornado thread request.id = self._next_id self._next_id += 1 self._request_lookup[request.id] = request message = json_encode(request) self.log.debug("Sending message %s", message) self._conn.write_message(message)
def do_save(self, layout_name=None): if layout_name is None or layout_name == '': layout_name = self.layout_name.value structure = self._save_to_structure() filename = "/tmp/" + layout_name + ".json" text = json_encode(structure, indent=2) open(filename, "w").write(text) self.layout_name.set_value(layout_name)
def do_save(self, layout_name=None): if not layout_name: layout_name = self.layout_name.value structure = self._save_to_structure() text = json_encode(structure, indent=2) filename = self._validated_config_filename(layout_name) open(filename, "w").write(text) self._set_layout_names(layout_name) self.layout_name.set_value(layout_name) self.load_structure = structure
def on_response(self, response, write_message): # called from tornado thread message = json_encode(response) try: write_message(message) except WebSocketError: if isinstance(response, (Delta, Update)): request = self._subscription_keys[response.id] unsubscribe = Unsubscribe(request.id) controller = self.process.get_controller(request.path[0]) controller.handle_request(unsubscribe)
def _on_response(self, response): # called from tornado thread if isinstance(response, Return): message = json_encode(response.value) self.finish(message + "\n") else: if isinstance(response, Error): message = response.message else: message = "Unknown response %s" % type(response) self.set_status(500, message) self.write_error(500)
def send_messages(self, messages, convert_json=True): conn = yield websocket_connect("ws://localhost:%s/ws" % self.socket) num = len(messages) for msg in messages: if convert_json: msg = json_encode(msg) conn.write_message(msg) for _ in range(num): resp = yield conn.read_message() resp = json.loads(resp) cothread.Callback(self.result.put, resp) conn.close()
def on_response(self, response): # called from cothread if isinstance(response, Return): message = json_encode(response.value) IOLoopHelper.call(self.finish, message + "\n") else: if isinstance(response, Error): message = response.message else: message = "Unknown response %s" % type(response) self.set_status(500, message) IOLoopHelper.call(self.write_error, 500)
def on_message(self, message): # called in tornado's thread if self._writeable is None: # Work out if the remote ip is within the netmask of any of our # interfaces. If not, Put and Post are forbidden ipv4_ip = self.request.remote_ip if ipv4_ip == "::1": # Special case IPV6 loopback ipv4_ip = "127.0.0.1" remoteaddr = struct.unpack("!I", socket.inet_aton(ipv4_ip))[0] self._writeable = max(v(remoteaddr) for v in self._validators) log.info("Puts and Posts are %s from %s", "allowed" if self._writeable else "forbidden", self.request.remote_ip) msg_id = -1 try: d = json_decode(message) try: msg_id = d['id'] except KeyError: raise FieldError('id field not present in JSON message') request = deserialize_object(d, Request) request.set_callback(self.on_response) if isinstance(request, Subscribe): assert msg_id not in self._id_to_mri, \ "Duplicate subscription ID %d" % msg_id self._id_to_mri[msg_id] = request.path[0] if isinstance(request, Unsubscribe): mri = self._id_to_mri[msg_id] else: mri = request.path[0] if isinstance(request, (Put, Post)) and not self._writeable: raise ValueError("Put/Post is forbidden from %s" % self.request.remote_ip) log.info("Request: %s", request) self._registrar.report(builtin.infos.RequestInfo(request, mri)) except Exception as e: log.exception("Error handling message:\n%s", message) error = Error(msg_id, e) error_message = error.to_dict() self.write_message(json_encode(error_message))
def do_save(self, design=""): if not design: design = self.design.value assert design, "Please specify save design name when saving from new" structure = OrderedDict() # Add the layout table part_layouts = {} for name, x, y, visible in sorted( zip(self.layout.value.name, self.layout.value.x, self.layout.value.y, self.layout.value.visible)): layout_structure = OrderedDict() layout_structure["x"] = x layout_structure["y"] = y layout_structure["visible"] = visible part_layouts[name] = layout_structure structure["layout"] = OrderedDict() for part_name in self.parts: if part_name in part_layouts: structure["layout"][part_name] = part_layouts[part_name] # Add the exports table structure["exports"] = OrderedDict() for name, export_name in sorted( zip(self.exports.value.name, self.exports.value.exportName)): structure["exports"][name] = export_name # Add any structure that a child part wants to save part_structures = self.run_hook( self.Save, self.create_part_contexts(only_visible=False)) for part_name, part_structure in sorted(part_structures.items()): structure[part_name] = part_structure text = json_encode(structure, indent=2) filename = self._validated_config_filename(design) with open(filename, "w") as f: f.write(text) if os.path.isdir(os.path.join(self.params.configDir, ".git")): # Try and commit the file to git, don't care if it fails self._run_git_cmd("add", filename) msg = "Saved %s %s" % (self.mri, design) self._run_git_cmd("commit", "--allow-empty", "-m", msg, filename) self._mark_clean(design)
def _on_response(self, response): # type: (Response) -> None # called from tornado thread message = json_encode(response) try: self.write_message(message) except WebSocketError: # The websocket is dead. If the response was a Delta or Update, then # unsubscribe so the local controller doesn't keep on trying to # respond if isinstance(response, (Delta, Update)): # Websocket is dead so we can clear the subscription key. # Subsequent updates may come in before the unsubscribe, but # ignore them as we can't do anything about it mri = self._id_to_mri.pop(response.id, None) if mri: log.info( 'WebSocket Error: unsubscribing from stale handle') unsubscribe = Unsubscribe(response.id) unsubscribe.set_callback(self.on_response) self._registrar.report( builtin.infos.RequestInfo(unsubscribe, mri))
def _send_request(self, request): message = json_encode(request) self.log.debug("Sending message %s", message) self._conn.write_message(message)