Server.ServerProxy Class Reference

Proxy class for a FeaturePython Server instance. More...

Inheritance diagram for Server.ServerProxy:

Public Member Functions

def __init__ (self, fp)
 Initialization method for ServerProxy. More...
 
def onDocumentRestored (self, fp)
 Method called when document is restored to make sure everything is as it was. More...
 
def onDocumentClosed (self)
 
def setProperties (self, fp)
 Method to set properties during initialization or document restoration. More...
 
def __getstate__ (self)
 Necessary method to avoid errors when trying to save unserializable objects. More...
 
def __setstate__ (self, state)
 Necessary method to avoid errors when trying to restore unserializable objects. More...
 

Static Public Attributes

 cmd_server = None
 A CommandServer instance to handle external commands. More...
 

Detailed Description

Proxy class for a FeaturePython Server instance.

A ServerProxy instance adds properties to a FeaturePython Server instance and responds to theirs changes. It provides a communication. CommandServer cmd_server when Running is set to True by double-clicking on it in the Tree View or right clicking and selecting Connect Server option from context menu. It closes the cmd_server when Running is set to False or an AnimateDocumentObserver detects that a document with Server instance is closing

Because communication.CommandServer cmd_server occupies a Port at selected Address, you cannot have duplicit Servers running simultaneously in a file, a FreeCAD window nor on one computer.

To connect this Proxy object to a FeaturePython Server do:

a = FreeCAD.ActiveDocument.addObject("App::FeaturePython", "Server")
ServerProxy(a)

Definition at line 71 of file Server.py.

Constructor & Destructor Documentation

◆ __init__()

def Server.ServerProxy.__init__ (   self,
  fp 
)

Initialization method for ServerProxy.

A class instance is created and made a Proxy for a generic FeaturePython Server object. During initialization number of properties are specified and preset.

Parameters
fpA barebone FeaturePython Server object to be extended.

Definition at line 88 of file Server.py.

88  def __init__(self, fp):
89  self.setProperties(fp)
90  fp.Proxy = self
91 

Member Function Documentation

◆ __getstate__()

def Server.ServerProxy.__getstate__ (   self)

Necessary method to avoid errors when trying to save unserializable objects.

This method is used by JSON to serialize unserializable objects during autosave. Without this an Error would rise when JSON would try to do that itself.

We need this for unserializable cmd_server attribute, but we don't serialize them, because it's enough to reset it when object is restored.

Returns
None, because we don't serialize anything.

Definition at line 201 of file Server.py.

201  def __getstate__(self):
202  return None
203 

◆ __setstate__()

def Server.ServerProxy.__setstate__ (   self,
  state 
)

Necessary method to avoid errors when trying to restore unserializable objects.

This method is used during a document restoration. We need this for unserializable cmd_server attribute, but we do not restore it, because it's enough to reset them from saved parameters.

Returns
None, because we don't restore anything.

Definition at line 214 of file Server.py.

214  def __setstate__(self, state):
215  return None
216 
217 

◆ onDocumentRestored()

def Server.ServerProxy.onDocumentRestored (   self,
  fp 
)

Method called when document is restored to make sure everything is as it was.

Reinitialization it creates properties and sets them to default values, if they were not restored automatically. It restarts a cmd_server if it was running when document was closed. Properties of connected ViewObject are also recreated and reset if necessary.

Parameters
fpA restored FeaturePython Server object.

Definition at line 103 of file Server.py.

103  def onDocumentRestored(self, fp):
104  self.setProperties(fp)
105  fp.ViewObject.Proxy.setProperties(fp.ViewObject)
106 

◆ setProperties()

def Server.ServerProxy.setProperties (   self,
  fp 
)

Method to set properties during initialization or document restoration.

The properties are set if they are not already present. Constrained properties have their boundaries reset even if present, because constrains are not saved. Also cmd_server is restarted if it was running previously and an AnimateDocumentObserver is recreated.

Parameters
fpA restored or barebone FeaturePython Server object.

Definition at line 131 of file Server.py.

131  def setProperties(self, fp):
132  # Check properties are present and create them if not
133  if not hasattr(fp, "Address"):
134  fp.addProperty("App::PropertyString", "Address", "Server settings",
135  "IP address where the server will listen for "
136  + "connection.\nValid values are IPv4 and "
137  + "IPv6 addresses or 'localhost'string."
138  ).Address = "localhost"
139  if not hasattr(fp, "Port"):
140  fp.addProperty("App::PropertyIntegerConstraint", "Port",
141  "Server settings", "Port where the server will "
142  + "listen for connections.\n" +
143  "Valid port numbers are in range <0 | 65535>,\n"
144  + "but some may be already taken!"
145  ).Port = (54321, 0, 65535, 1)
146  else:
147  fp.Port = (fp.Port, 0, 65535, 1)
148  if not hasattr(fp, "Running"):
149  fp.addProperty("App::PropertyBool", "Running", "Server settings",
150  "If Server Running is true, then Server listens "
151  + "for new connections."
152  ).Running = False
153 
154  # hide Placement property as there is nothing to display/move
155  fp.setEditorMode("Placement", 2)
156  # make Running property read-only as it's set from context menu/
157  # by double clicking
158  fp.setEditorMode("Running", 1)
159 
160  # try to start cmd_server, if it was running before closing
161  if fp.Running:
162  self.cmd_server = com.startServer(fp.Address, fp.Port)
163  if self.cmd_server == com.SERVER_ERROR_INVALID_ADDRESS:
164  fp.ViewObject.Proxy._icon = path.join(PATH_TO_ICONS,
165  "Server.png")
166  QMessageBox.warning(None, 'Error while starting server',
167  "The address was not in supported format.")
168  fp.Running = False
169  elif self.cmd_server == com.SERVER_ERROR_PORT_OCCUPIED:
170  fp.ViewObject.Proxy._icon = path.join(PATH_TO_ICONS,
171  "Server.png")
172  QMessageBox.warning(None, 'Error while starting server',
173  "The port requested is already occupied.")
174  fp.Running = False
175  else:
176  fp.setEditorMode("Address", 1)
177  fp.setEditorMode("Port", 1)
178  fp.Running = True
179  fp.ViewObject.Proxy._icon = path.join(PATH_TO_ICONS,
180  "ServerRunning.png")
181 
182  # Make an document observer to be notified when document will be closed
183  import AnimateDocumentObserver
185  FreeCAD.animate_observer.addServerToNotify(self,
186  FreeCAD.ActiveDocument.Name)
187 
def addObserver()
Adds an AnimateDocumentObserver between FreeCAD's document observers safely.

Member Data Documentation

◆ cmd_server

Server.ServerProxy.cmd_server = None
static

A CommandServer instance to handle external commands.

Definition at line 76 of file Server.py.


The documentation for this class was generated from the following file: