use a single sfiprocess object for all commands.
[sface.git] / sface / screens / mainscreen.py
index d43bd62..e2e90e6 100644 (file)
@@ -112,6 +112,15 @@ class TreeItem:
         del self.childItems
         self.childItems = []
 
+    def allChildItems(self):
+        all = []
+        for c in self.childItems:
+            all.append(c)
+            if c.childItems:
+                for cc in c.childItems:
+                    all.append(cc)
+        return all
+
     def appendChild(self, child):
         self.childItems.append(child)
 
@@ -166,7 +175,6 @@ class TreeItem:
         return self.parentItem
 
 
-
 class NodeModel(QAbstractItemModel):
     def __init__(self, parent):
         QAbstractItemModel.__init__(self, parent)
@@ -182,7 +190,8 @@ class NodeModel(QAbstractItemModel):
     def getItem(self, index):
         if index.isValid():
             item = index.internalPointer()
-            if item: return item
+            if isinstance(item, TreeItem):
+                return item
         return self.rootItem
 
     def headerData(self, section, orientation, role):
@@ -264,6 +273,7 @@ class SliceWidget(QWidget):
         QWidget.__init__(self, parent)
 
         self.network_names = []
+        self.process = SfiProcess(self)
 
         slicename = QLabel ("Slice : %s"%(config.getSlice() or "None"),self)
         slicename.setScaledContents(False)
@@ -304,9 +314,38 @@ class SliceWidget(QWidget):
         self.connect(refresh, SIGNAL('clicked()'), self.refresh)
         self.connect(submit, SIGNAL('clicked()'), self.submit)
         self.connect(searchbox, SIGNAL('textChanged(QString)'), self.filter)
+        self.connect(self.process, SIGNAL('readyReadStandardOutput()'), self.processOutputReady)
+        self.connect(self.process, SIGNAL('readyReadStandardError()'), self.processOutputReady)
 
         self.updateView()
 
+    def processOutputReady(self):
+        self.parent().logWindow.setText(self.process.readOutput())
+
+    def submitFinished(self):
+        self.setStatus("<font color='green'>Slice data submitted.</font>")
+        QTimer.singleShot(1000, self.refresh)
+
+    def refreshFinished(self):
+        self.setStatus("<font color='green'>Slice data updated.</font>", timeout=5000)
+        self.updateView()
+
+    def readSliceRSpec(self):
+        rspec_file = config.getSliceRSpecFile()
+        if os.path.exists(rspec_file):
+            xml = open(rspec_file).read()
+            return xml
+        return None
+
+    def setStatus(self, msg, timeout=None):
+        self.parent().setStatus(msg, timeout)
+
+    def checkRunningProcess(self):
+        if self.process.isRunning():
+            self.setStatus("<font color='red'>There is already a process running. Please wait.</font>")
+            return True
+        return False
+
     def filter(self, filter_string):
         # for hierarchical models QSortFilterProxyModel applies the
         # sort recursively. if the parent doesn't match the criteria
@@ -317,30 +356,50 @@ class SliceWidget(QWidget):
         self.filterModel.setFilterRegExp(QRegExp('|'.join(filters)))
 
     def submit(self):
-        self.parent().setStatus("TODO: Submit not implemented yet!", 3000)
+        if self.checkRunningProcess():
+            return
+
+        rspec = RSpec(self.readSliceRSpec())
+        
+        no_change = True
+        all_child = self.nodeModel.rootItem.allChildItems()
+        for c in all_child:
+            testbed, hostname, status = c.itemData
+            if isinstance(status, QVariant):
+                status = status.toString()
+
+            if status == node_status['add']:
+                rspec.add_sliver(hostname)
+                no_change = False
+            elif str(status) == node_status['remove']:
+                rspec.remove_sliver(hostname)
+                no_change = False
+
+        if no_change:
+            self.setStatus("<font color=red>No change in slice data. Not submitting!</font>", timeout=3000)
+            return
+
+        self.disconnect(self.process, SIGNAL('finished()'), self.refreshFinished)
+        self.connect(self.process, SIGNAL('finished()'), self.submitFinished)
+
+        self.process.applyRSpec(rspec)
+        self.setStatus("Sending slice data (RSpec). This may take some time...")
         
-    def readSliceRSpec(self):
-        rspec_file = config.getSliceRSpecFile()
-        if os.path.exists(rspec_file):
-            xml = open(rspec_file).read()
-            return xml
-        return None
 
     def refresh(self):
         if not config.getSlice():
-            self.parent().setStatus("<font color='red'>Slice not set yet!</font>", timeout=None)
+            self.setStatus("<font color='red'>Slice not set yet!</font>")
             return
 
-        self.process = SfiProcess()
-        outfile = self.process.getRSpecFromSM()
-        self.parent().setStatus("Updating slice data. This may take some time...", timeout=None)
-        
+        if self.process.isRunning():
+            self.setStatus("<font color='red'>There is already a process running. Please wait.</font>")
+            return
+
+        self.disconnect(self.process, SIGNAL('finished()'), self.submitFinished)
         self.connect(self.process, SIGNAL('finished()'), self.refreshFinished)
 
-    def refreshFinished(self):
-        del self.process
-        self.parent().setStatus("<font color='green'>Slice data updated.</font>", timeout=5000)
-        self.updateView()
+        self.process.getRSpecFromSM()
+        self.setStatus("Updating slice data. This may take some time...")
 
     def updateView(self):
         global already_in_nodes