def hasPrevious(self, name, args): socket = QLocalSocket() socket.connectToServer(name, QLocalSocket.ReadWrite) if socket.waitForConnected(): if len(args) > 1: socket.write(args[1]) socket.flush() return True return False
def initLocalConnection(self): socket = QLocalSocket() socket.connectToServer(self.serverName) if socket.waitForConnected(self.TIME_OUT): self.isRuning = True if len(self.args) >= 3: data = self.args[1] + ',' + self.args[2] socket.writeData(data.encode()) socket.flush() socket.waitForBytesWritten() return self.newLocalServer()
def startClient(self) -> bool: Logger.log( "i", "Checking for the presence of an ready running Cura instance.") single_instance_socket = QLocalSocket(self._application) Logger.log("d", "Full single instance server name: %s", single_instance_socket.fullServerName()) single_instance_socket.connectToServer("ultimaker-cura") single_instance_socket.waitForConnected( msecs=3000) # wait for 3 seconds if single_instance_socket.state() != QLocalSocket.ConnectedState: return False # We only send the files that need to be opened. if not self._files_to_open: Logger.log("i", "No file need to be opened, do nothing.") return True if single_instance_socket.state() == QLocalSocket.ConnectedState: Logger.log( "i", "Connection has been made to the single-instance Cura socket.") # Protocol is one line of JSON terminated with a carriage return. # "command" field is required and holds the name of the command to execute. # Other fields depend on the command. if self._application.getPreferences().getValue( "cura/single_instance_clear_before_load"): payload = {"command": "clear-all"} single_instance_socket.write( bytes(json.dumps(payload) + "\n", encoding="ascii")) payload = {"command": "focus"} single_instance_socket.write( bytes(json.dumps(payload) + "\n", encoding="ascii")) for filename in self._files_to_open: payload = { "command": "open", "filePath": os.path.abspath(filename) } single_instance_socket.write( bytes(json.dumps(payload) + "\n", encoding="ascii")) payload = {"command": "close-connection"} single_instance_socket.write( bytes(json.dumps(payload) + "\n", encoding="ascii")) single_instance_socket.flush() single_instance_socket.waitForDisconnected() return True
def __init__(self, serverName, parent=None): SocketInterface.__init__(self, serverName, parent=parent) socket = QLocalSocket(parent) socket.readyRead.connect(self.receiveMessage) socket.connectToServer(serverName) if socket.waitForConnected(1000): socket.write("Hello {0}!".format(serverName).encode("utf-8")) socket.flush() socket.waitForBytesWritten(1000) self.conn = socket else: self.log("Could not connect to SocketServer.")
def hasPrevious(self, name, args): print("önceki socket kontrol ediliyor...") socket = QLocalSocket() socket.connectToServer(name, QLocalSocket.ReadWrite) if socket.waitForConnected(): print("Önceki instansa argümanlar gönderiliyor.") if len(args) > 1: socket.write(args[1]) else: pass socket.flush() return True print(socket.errorString()) return False
def startClient(self) -> bool: Logger.log("i", "Checking for the presence of an ready running Cura instance.") single_instance_socket = QLocalSocket(self._application) Logger.log("d", "Full single instance server name: %s", single_instance_socket.fullServerName()) single_instance_socket.connectToServer("ultimaker-cura") single_instance_socket.waitForConnected(msecs = 3000) # wait for 3 seconds if single_instance_socket.state() != QLocalSocket.ConnectedState: return False # We only send the files that need to be opened. if not self._files_to_open: Logger.log("i", "No file need to be opened, do nothing.") return True if single_instance_socket.state() == QLocalSocket.ConnectedState: Logger.log("i", "Connection has been made to the single-instance Cura socket.") # Protocol is one line of JSON terminated with a carriage return. # "command" field is required and holds the name of the command to execute. # Other fields depend on the command. payload = {"command": "clear-all"} single_instance_socket.write(bytes(json.dumps(payload) + "\n", encoding = "ascii")) payload = {"command": "focus"} single_instance_socket.write(bytes(json.dumps(payload) + "\n", encoding = "ascii")) for filename in self._files_to_open: payload = {"command": "open", "filePath": os.path.abspath(filename)} single_instance_socket.write(bytes(json.dumps(payload) + "\n", encoding = "ascii")) payload = {"command": "close-connection"} single_instance_socket.write(bytes(json.dumps(payload) + "\n", encoding = "ascii")) single_instance_socket.flush() single_instance_socket.waitForDisconnected() return True
class Client: def __init__(self): self.socket = QLocalSocket() self.socket.setServerName(piony.G_SOCKET_NAME) self.socket.error.connect(self.displayError) self.socket.disconnected.connect(self.socket.deleteLater) def _log_(self, text, *args): logger.info(self.__class__.__qualname__ + ': ' + text, *args) def connect(self): self.socket.abort() self._log_("connection attempt") self.socket.connectToServer() def send(self, argv): data = QByteArray() out = QDataStream(data, QIODevice.WriteOnly) out.setVersion(QDataStream.Qt_5_0) out.writeQVariant(argv) self._log_("writes: %s", str(data)) self.socket.write(data) self.socket.flush() self.socket.disconnectFromServer() def displayError(self, err): msg = { QLocalSocket.ServerNotFoundError: "The host was not found. Check the host name and port.", QLocalSocket.ConnectionRefusedError: "The connection was refused by the peer. " "Check server is running, it's host and port.", QLocalSocket.PeerClosedError: "Peer was closed", # None, }.get(err, "Client error: {}.".format(self.socket.errorString())) self._log_(msg)
class SingleApplicationClient(object): """ Class implementing the single application client base class. """ def __init__(self, name): """ Constructor @param name name of the local server to connect to (string) """ self.name = name self.connected = False def connect(self, timeout=10000): """ Public method to connect the single application client to its server. @param timeout connection timeout value in milliseconds @type int @return value indicating success or an error number. Value is one of: <table> <tr><td>0</td><td>No application is running</td></tr> <tr><td>1</td><td>Application is already running</td></tr> </table> """ self.sock = QLocalSocket() self.sock.connectToServer(self.name) if self.sock.waitForConnected(timeout): self.connected = True return 1 else: err = self.sock.error() if err == QLocalSocket.ServerNotFoundError: return 0 else: return -err def disconnect(self): """ Public method to disconnect from the Single Appliocation server. """ self.sock.disconnectFromServer() self.connected = False def processArgs(self, args): """ Public method to process the command line args passed to the UI. <b>Note</b>: This method must be overridden by subclasses. @param args command line args (list of strings) @exception RuntimeError raised to indicate that this method must be implemented by a subclass """ raise RuntimeError("'processArgs' must be overridden") def sendCommand(self, command, arguments): """ Public method to send the command to the application server. @param command command to be sent to the server @type str @param arguments list of command arguments @type list of str """ if self.connected: commandDict = { "command": command, "arguments": arguments, } self.sock.write(QByteArray( "{0}\n".format(json.dumps(commandDict)).encode() )) self.sock.flush() def errstr(self): """ Public method to return a meaningful error string for the last error. @return error string for the last error (string) """ return self.sock.errorString()
class SingleApplicationClient(object): """ Class implementing the single application client base class. """ def __init__(self, name): """ Constructor @param name name of the local server to connect to (string) """ self.name = name self.connected = False def connect(self): """ Public method to connect the single application client to its server. @return value indicating success or an error number. Value is one of: <table> <tr><td>0</td><td>No application is running</td></tr> <tr><td>1</td><td>Application is already running</td></tr> </table> """ self.sock = QLocalSocket() self.sock.connectToServer(self.name) if self.sock.waitForConnected(10000): self.connected = True return 1 else: err = self.sock.error() if err == QLocalSocket.ServerNotFoundError: return 0 else: return -err def disconnect(self): """ Public method to disconnect from the Single Appliocation server. """ self.sock.disconnectFromServer() self.connected = False def processArgs(self, args): """ Public method to process the command line args passed to the UI. <b>Note</b>: This method must be overridden by subclasses. @param args command line args (list of strings) @exception RuntimeError raised to indicate that this method must be implemented by a subclass """ raise RuntimeError("'processArgs' must be overridden") def sendCommand(self, cmd): """ Public method to send the command to the application server. @param cmd command to be sent (string) """ if self.connected: self.sock.write(cmd) self.sock.flush() def errstr(self): """ Public method to return a meaningful error string for the last error. @return error string for the last error (string) """ return self.sock.errorString()