2 # -*- coding: utf-8 -*-
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 from SimpleXMLRPCServer import SimpleXMLRPCServer
25 socket.setdefaulttimeout(60) # Timeout auf 60 sec. setzen
38 class ProgressDialog(gtk.Dialog):
41 #self.progressbar.pulse()
44 def __init__(self,title=_("Sync process"), parent=None):
45 gtk.Dialog.__init__(self,title,parent,gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,())
47 logging.info("ProgressDialog, init")
49 label=gtk.Label(_("Sync process running...please wait"))
50 self.vbox.pack_start(label, True, True, 0)
51 label=gtk.Label(_("(this can take some minutes)"))
52 self.vbox.pack_start(label, True, True, 0)
54 #self.progressbar=gtk.ProgressBar()
55 #self.vbox.pack_start(self.progressbar, True, True, 0)
57 #self.set_keep_above(True)
64 'syncFinished' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,(gobject.TYPE_STRING,)),
65 'syncBeforeStart' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,(gobject.TYPE_STRING,)),
68 def changeSyncStatus(self,active,title):
69 self.syncStatusLabel.set_text(title)
71 if self.progress==None:
72 self.progress=ProgressDialog(parent=self.parentwindow)
73 self.emit("syncBeforeStart","syncBeforeStart")
77 if self.progress!=None:
79 self.progress.destroy()
81 self.emit("syncFinished","syncFinished")
84 if self.progress!=None:
86 #if self.server!=None:
90 def getUeberblickBox(self):
91 frame=gtk.Frame(_("Query"))
96 if (self.rpcserver==None): return False
100 while (len(self.poll.poll(0))>0):
101 self.rpcserver.handle_request()
104 def get_ip_address(self,ifname):
105 return socket.gethostbyname(socket.gethostname())
107 # s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
108 # ip=socket.inet_ntoa(fcntl.ioctl(s.fileno(),0x8915,struct.pack('256s', ifname[:15]))[20:24])
111 # ip=socket.gethostbyname(socket.gethostname())
116 def getLastSyncDate(self,sync_uuid):
117 sql="SELECT syncpartner,pcdatum FROM sync WHERE uuid=?"
118 rows=self.db.ladeSQL(sql,(sync_uuid,))
119 if (rows!=None)and(len(rows)==1):
120 syncpartner,pcdatum = rows[0]
123 logging.info("LastSyncDatum: "+str(pcdatum)+" Jetzt "+str(int(time.time())))
127 def check4commit(self,newSQL,lastdate):
128 logging.info("check4commit 1")
129 if self.concernedRows==None:
130 logging.info("check4commit Updatung concernedRows")
131 sql="SELECT pcdatum,rowid FROM logtable WHERE pcdatum>? ORDER BY pcdatum DESC"
132 self.concernedRows=self.db.ladeSQL(sql,(lastdate,))
135 if (self.concernedRows!=None)and(len(self.concernedRows)>0):
136 #logging.info("check4commit 2")
137 id1, pcdatum,sql, param, host, rowid = newSQL
140 for x in self.concernedRows:
141 #logging.info("check4commit 3")
144 logging.info("newer sync entry, ignoring old one")
145 #logging.info("check4commit 9.1")
148 #logging.info("check4commit 9.2")
151 #logging.info("check4commit 9.3")
154 def writeSQLTupel(self,newSQLs,lastdate):
158 self.concernedRows=None
160 logging.info("writeSQLTupel got "+str(len(newSQLs))+" sql tupels")
161 for newSQL in newSQLs:
163 #print "SQL1: ",newSQL[1]
164 #print "SQL2: ",newSQL[2]
165 #print "SQL3: ",newSQL[3]
166 #print "Param:",string.split(newSQL[3]," <<Tren-ner>> ")
169 param=string.split(newSQL[3]," <<Tren-ner>> ")
176 if (newSQL[5]!=None)and(len(newSQL[5])>0):
177 commitSQL=self.check4commit(newSQL,lastdate)
179 if (commitSQL==True):
180 self.db.speichereSQL(newSQL[2],param,commit=False,pcdatum=newSQL[1],rowid=newSQL[5])
182 logging.error("writeSQLTupel: Error")
185 if (pausenzaehler % 10)==0:
187 while (gtk.events_pending()):
188 gtk.main_iteration();
190 logging.info("Alle SQLs an sqlite geschickt, commiting now")
192 logging.info("Alle SQLs commited")
195 def doSync(self,sync_uuid,pcdatum,newSQLs,pcdatumjetzt):
196 #print uuid,pcdatum,newSQLs
197 #logging.info("doSync 0")
198 self.changeSyncStatus(True,_("sync process running"))
200 #logging.info("doSync 1")
202 while (gtk.events_pending()):
203 gtk.main_iteration();
204 diff=time.time()-pcdatumjetzt
210 logging.info("doSync read sqls")
211 sql="SELECT * FROM logtable WHERE pcdatum>?"
212 rows=self.db.ladeSQL(sql,(pcdatum,))
213 logging.info("doSync read sqls")
214 self.writeSQLTupel(newSQLs,pcdatum)
215 logging.info("doSync wrote "+str(len(newSQLs))+" sqls")
216 logging.info("doSync sending "+str(len(rows))+" sqls")
220 def getRemoteSyncUUID(self):
221 return self.sync_uuid
224 def startServer(self, widget, data=None):
226 self.db.speichereDirekt("syncServerIP",self.comboIP.get_child().get_text())
228 if (widget.get_active()==True):
229 logging.info("Starting Server")
232 ip=self.comboIP.get_child().get_text()
233 self.rpcserver = SimpleXMLRPCServer((ip, self.port),allow_none=True)
234 self.rpcserver.register_function(pow)
235 self.rpcserver.register_function(self.getLastSyncDate)
236 self.rpcserver.register_function(self.doSync)
237 self.rpcserver.register_function(self.getRemoteSyncUUID)
238 self.rpcserver.register_function(self.doSaveFinalTime)
239 self.rpcserver.register_function(self.pulse)
240 self.poll=select.poll()
241 self.poll.register(self.rpcserver.fileno())
242 gobject.timeout_add(1000, self.handleRPC)
243 self.syncServerStatusLabel.set_text(_("Syncserver running..."))
246 self.db.speichereDirekt("startSyncServer",True)
249 s=str(sys.exc_info())
250 logging.error("libsync: could not start server. Error: "+s)
251 mbox=gtk.MessageDialog(None,gtk.DIALOG_MODAL,gtk.MESSAGE_ERROR,gtk.BUTTONS_OK,_("Sync server could not start. Please check IP and port.")) #gtk.DIALOG_MODAL
252 mbox.set_modal(False)
256 widget.set_active(False)
259 logging.info("Stopping Server")
264 self.syncServerStatusLabel.set_text(_("Syncserver not running..."))
266 self.db.speichereDirekt("startSyncServer",False)
268 def doSaveFinalTime(self,sync_uuid,pcdatum=None):
269 if (pcdatum==None): pcdatum=int(time.time())
270 if (time.time()>pcdatum):
271 pcdatum=int(time.time()) #größere Zeit nehmen
276 sql="DELETE FROM sync WHERE uuid=?"
277 self.db.speichereSQL(sql,(sync_uuid,),log=False)
278 sql="INSERT INTO sync (syncpartner,uuid,pcdatum) VALUES (?,?,?)"
279 self.db.speichereSQL(sql,("x",str(sync_uuid),pcdatum),log=False)
281 self.changeSyncStatus(False,_("no sync process (at the moment)"))
282 return (self.sync_uuid,pcdatum)
285 def syncButton(self, widget, data=None):
286 logging.info("Syncing")
287 #sql="DELETE FROM logtable WHERE sql LIKE externeStundenplanung"
288 #self.db.speichereSQL(sql)
290 self.changeSyncStatus(True,_("sync process running"))
291 while (gtk.events_pending()):
292 gtk.main_iteration();
294 self.db.speichereDirekt("syncRemoteIP",self.comboRemoteIP.get_child().get_text())
296 self.server = xmlrpclib.ServerProxy("http://"+self.comboRemoteIP.get_child().get_text()+":"+str(self.port),allow_none=True)
297 #lastDate=server.getLastSyncDate(str(self.sync_uuid))
298 server_sync_uuid=self.server.getRemoteSyncUUID()
299 lastDate=self.getLastSyncDate(str(server_sync_uuid))
301 #print ("LastSyncDate: "+str(lastDate)+" Now: "+str(int(time.time())))
303 sql="SELECT * FROM logtable WHERE pcdatum>?"
304 rows=self.db.ladeSQL(sql,(lastDate,))
306 logging.info("loaded concerned rows")
308 newSQLs=self.server.doSync(self.sync_uuid,lastDate,rows,time.time())
310 logging.info("did do sync, processing sqls now")
312 self.writeSQLTupel(newSQLs,lastDate)
314 sync_uuid, finalpcdatum=self.server.doSaveFinalTime(self.sync_uuid)
315 self.doSaveFinalTime(sync_uuid, finalpcdatum)
317 self.changeSyncStatus(False,_("no sync process (at the moment)"))
319 mbox = gtk.MessageDialog(None,gtk.DIALOG_MODAL,gtk.MESSAGE_INFO,gtk.BUTTONS_OK,_("Synchronization successfully completed"))
320 response = mbox.run()
324 logging.warning("Zeitdiff zu groß/oder anderer db-Fehler")
325 self.changeSyncStatus(False,_("no sync process (at the moment)"))
326 mbox = gtk.MessageDialog(None,gtk.DIALOG_MODAL,gtk.MESSAGE_INFO,gtk.BUTTONS_OK,_("The clocks are not synchronized between stations"))
327 response = mbox.run()
331 logging.warning("Sync connect failed")
332 self.changeSyncStatus(False,_("no sync process (at the moment)"))
333 mbox = gtk.MessageDialog(None,gtk.DIALOG_MODAL,gtk.MESSAGE_INFO,gtk.BUTTONS_OK,_("Sync failed, reason: ")+unicode(sys.exc_info()[1][1]))
334 response = mbox.run()
343 def __init__(self,db,parentwindow,port):
344 gtk.VBox.__init__(self,homogeneous=False, spacing=0)
346 logging.info("Sync, init")
351 self.parentwindow=parentwindow
352 self.concernedRows=None
355 #sql = "DROP TABLE sync"
356 #self.db.speichereSQL(sql,log=False)
358 sql = "CREATE TABLE sync (id INTEGER PRIMARY KEY, syncpartner TEXT, uuid TEXT, pcdatum INTEGER)"
359 self.db.speichereSQL(sql,log=False)
363 sql="SELECT uuid,pcdatum FROM sync WHERE syncpartner=?"
364 rows=self.db.ladeSQL(sql,("self",)) #Eigene Id feststellen
367 if (rows==None)or(len(rows)!=1):
368 sql="DELETE FROM sync WHERE syncpartner=?"
369 self.db.speichereSQL(sql,("self",),log=False)
375 self.sync_uuid=str(uuid.uuid4())
376 sql="INSERT INTO sync (syncpartner,uuid,pcdatum) VALUES (?,?,?)"
377 self.db.speichereSQL(sql,("self",str(self.sync_uuid),int(time.time())),log=False)
380 sync_uuid,pcdatum = rows[0]
381 self.sync_uuid=sync_uuid
389 frame=gtk.Frame(_("Local SyncServer (port ")+str(self.port)+")")
393 self.comboIP=gtk.combo_box_entry_new_text()
396 self.comboIP.append_text("") #self.get_ip_address("eth0"))
397 #self.comboIP.append_text(self.get_ip_address("eth1")) #fixme
398 #self.comboIP.append_text(self.get_ip_address("eth2"))
399 #self.comboIP.append_text(self.get_ip_address("eth3"))
401 #self.comboIP.append_text(self.get_ip_address("wlan0"))
402 #self.comboIP.append_text(self.get_ip_address("wlan1"))
406 frame.add(self.comboIP)
407 serverbutton=gtk.ToggleButton(_("Start SyncServer"))
408 serverbutton.connect("clicked",self.startServer,(None,))
409 self.pack_start(frame, expand=False, fill=True, padding=1)
410 self.pack_start(serverbutton, expand=False, fill=True, padding=1)
411 self.syncServerStatusLabel=gtk.Label(_("Syncserver not running"))
412 self.pack_start(self.syncServerStatusLabel, expand=False, fill=True, padding=1)
414 frame=gtk.Frame(_("RemoteSync-Server (Port ")+str(self.port)+")")
415 self.comboRemoteIP=gtk.combo_box_entry_new_text()
416 self.comboRemoteIP.append_text("192.168.0.?")
417 self.comboRemoteIP.append_text("192.168.1.?")
418 self.comboRemoteIP.append_text("192.168.176.?")
419 frame.add(self.comboRemoteIP)
420 syncbutton=gtk.Button(_("Connect to remote SyncServer"))
421 syncbutton.connect("clicked",self.syncButton,(None,))
422 self.pack_start(frame, expand=False, fill=True, padding=1)
423 self.pack_start(syncbutton, expand=False, fill=True, padding=1)
424 self.syncStatusLabel=gtk.Label(_("no sync process (at the moment)"))
425 self.pack_start(self.syncStatusLabel, expand=False, fill=True, padding=1)
428 #self.comboRemoteIP.set_text_column("Test")
429 self.comboRemoteIP.get_child().set_text(self.db.ladeDirekt("syncRemoteIP"))
430 self.comboIP.get_child().set_text(self.db.ladeDirekt("syncServerIP"))
433 if (self.db.ladeDirekt("startSyncServer",False)==True):
434 serverbutton.set_active(True)