Commits

Vincenzo Ampolo committed c3e992c

main, syntax fixes, elementtree fix

Comments (0)

Files changed (1)

+#!/usr/bin/python
+# -*- coding: UTF-8 -*-
+'''
+Usage here
+'''
 import logging
 import os
 import base64
 import time
-import elementtree.ElementTree as ET
+import xml.etree.ElementTree as ET
+
 import bluetooth
 
 from os.path import join, getsize
 from subprocess import call
 
-def class DataFile:
+class DataFile:
     """
      Create an instance of DataFile in the working directory
      Append other data if there is still data to send to BluebatServer
     static this(){
     datafile = new DataFile();
     }
+    
+    Vincenzo: basta che sia una istanza aperta per tutti i thread in esecuzione
     """
 
     def upload_data():
 
         while(1):
             #perche' tutti i file di log sono data.xml??
+            #Vincenzo: tutti i thread scrivono su data.xml :) ed è sbagliato che ognuno ne apre un'istanza
             o = open("data.xml","w+")
             logger.info("data.xml is not empty, trying to send to server{}", server)
-            try:
-                    # crea un socket INET di tipo STREAM
-                    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-                    # ora si connette al server web sulla porta 80
-                    #  - la normale porta http
+            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+            
+            for i in range(30):
+                try:
                     s.connect((server, port))
-            except socket.error:
-                pass # non ci sarebbe da fare qualcosa qui?
+                except:
+                    continue
+                else:
+                    break
 
             logger.info("Connection Created. Sending data.xml")
 
             o.close()
 
             #come implementare datafile.CleanData() ??
-
+            #Vincenzo: Semplicemente pulendo il file data.xml
             s.send(buf)
             s.close() # equivalente a s.shutdown?
 
         f.set("name", name)
         f.text = e # put the encoded file into the text field
 
-        logger.warn("Communication on protocol %s port %s, client address %s", % protocol, port, addr)
+        logger.warn("Communication on protocol %s port %s, client address %s" % protocol, port, addr)
 
         tree.write("data.xml") # write back the tree to disk
 
             process_client(client, "l2cap", i, address)
 
 
-    def main():
-        logger = logging.getLogger('bluebat.main')
-        hdlr = logging.FileHandler('log.txt')
-        formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
-        hdlr.setFormatter(formatter)
-        logger.addHandler(hdlr) 
-        logger.setLevel(logging.INFO)
+                
+def main(argv=None):
+    if argv is None:
+       argv = sys.argv
+    
+    opts, extraparams = getopt.gnu_getopt(argv[1:], "hv", ["help"])
+    
+    verbose = False
+    
+    for o, a in opts:
+        if o == "-v":
+            verbose = True
+        elif o in ("-h", "--help"):
+            print __doc__
+            sys.exit(0)
+        else:
+            assert False, "UnhandledOption"
+            
+    #continue program execution here
+    logger = logging.getLogger('bluebat.main')
+    hdlr = logging.FileHandler('log.txt')
+    formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
+    hdlr.setFormatter(formatter)
+    logger.addHandler(hdlr) 
+    logger.setLevel(logging.INFO)
 
-        logger.info("starting")
+    logger.info("starting")
 
-        for i in range(1,31):
-            rfcomm.append(i)
+    for i in range(1,31):
+        rfcomm.append(i)
 
-        logger.info("Rfcomm sockets to monitor are %s", % len(rfcomm))
-        #dovrebbe funzionare anche
-        #logger.info("Rfcomm sockets to monitor are {0}".format(len(rfcomm))
-        logger.info("Rfcomm {0}".format(rfcomm))
+    logger.info("Rfcomm sockets to monitor are %s" % len(rfcomm))
+    #dovrebbe funzionare anche
+    #logger.info("Rfcomm sockets to monitor are {0}".format(len(rfcomm))
+    logger.info("Rfcomm {0}".format(rfcomm))
 
-        i = 0x1001 # it should be possible to write like this
-        while(i<0x10FF):
-            l2cap.append(i)
-            i = i + 1
+    i = 0x1001 # it should be possible to write like this
+    while(i<0x10FF):
+        l2cap.append(i)
+        i = i + 1
 
-        logger.info("Creating threads")
-        logger.info("Total threads are {0}".format(len(rfcomm) + len(l2cap)))
-        # create threads and run them
+    logger.info("Creating threads")
+    logger.info("Total threads are {0}".format(len(rfcomm) + len(l2cap)))
+    # create threads and run them
 
-        # create /tmp/sobexfiles
+    # create /tmp/sobexfiles
 
-        # run some commands
-        command = os.getcwd() + "sobexsrv-1.0.1/src/sobexsrv" 
-        subprocess.call([command,"-d","-I","-r","/tmp/sobexfiles/"])
+    # run some commands
+    command = os.getcwd() + "sobexsrv-1.0.1/src/sobexsrv" 
+    subprocess.call([command,"-d","-I","-r","/tmp/sobexfiles/"])
 
-        time.sleep(5)
+    time.sleep(5)
 
-        # how to quit the program?
+    # how to quit the program?
+
         
         
-              
+if __name__ == '__main__':
+    sys.exit(main())