Commits

Fabio Pozzi  committed 09fc612

fixed thread index bug, introduced a while(true) loop to keep the main running, a more elegant solution TBD

  • Participants
  • Parent commits 4587a63

Comments (0)

Files changed (1)

File bluebatClient.py

 import sys
 import getopt
 import socket
+import signal
 
 from os.path import join, getsize
 from subprocess import call
         logger.setLevel(logging.INFO)
         return logger
 
-
+    def quit(self):
+        """
+        when called it quits gracefully the program
+        """
+        pass
+        
     """
     come tradurlo?
     DataFile datafile;
 
     def process_client(self, client, protocol, port, addr):
         logger = open_log('bluebat.client.communication')
+	# receive file
+	client
 
-        e = base64.b64encode(buf) #encode the file
+	try:
+	    while True:
+		data = client_sock.recv(1024)
+	        if len(data) == 0: break
+	except IOError:
+   		pass
+	#TODO: sistemare	
+
+        e = base64.b64encode(data) #encode the file
 
         tree = ElementTree(file="data.xml") # read the xml tree of data.xml
         root = tree.getroot() # get the root node
 
         server_sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
         port = i
-       	server_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+       	#server_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
         server_sock.bind(("",port))
         server_sock.listen(1)
 
 
     logger.info("Creating threads")
     logger.info("Total threads are {0}".format(len(rfcomm) + len(l2cap)))
+
     # create threads and run them
     # Start the two thread groups:
     for x in range ( len(rfcomm) ):
-	t= Thread(target=d.rf_thread, args=(i,))
+	t= Thread(target=d.rf_thread, args=(rfcomm[x],))
         t.daemon = True
         t.start()
 
 #    delegate_thread.daemon = True
 #    delegate_thread.start()
 
-    time.sleep(5)
+#    time.sleep(5)
 
     p = "/tmp/sobexfiles"
     # create /tmp/sobexfiles if it doesn't exist
         os.makedirs(d)
 
     # run some commands
-    command = os.getcwd() + "sobexsrv-1.0.1/src/sobexsrv" 
-    subprocess.call([command,"-d","-I","-r","/tmp/sobexfiles/"])
+    #command = os.getcwd() + "sobexsrv-1.0.1/src/sobexsrv" 
+    #call([command,"-d","-I","-r","/tmp/sobexfiles/"])
 
     #bash = "/bin/bash"
     #subprocess.call([bash,"-c","'dbus-send --system --type=method_call --print-reply --dest=org.bluez /org/bluez/`pidof bluetoothd`/hci0 org.bluez.Adapter.SetProperty string:Discoverable variant:boolean:true'"])
     #dbus="dbus-send"
     #subprocess.call([dbus,"--system","--type=method_call","--print-reply","--dest=org.bluez","/org/bluez/`pidof bluetoothd`/hci0","org.bluez.Adapter.SetProperty","string:Discoverable","variant:boolean:true"])
 
-    # how to allow program termination ?
+    # quit when receiving SIGUSR1
+    print("I should now setup the signal handler")
+#    signal.signal(signal.SIGUSR1, lambda *args: self.quit())
+    print(os.getpid())
+
+    while(True):
+        time.sleep(1)
 
     # e' necessario chiudere i vari thread? dovrebbero essere terminati
     # con la terminazione del programma dato che e' stato settato Thread.daemon
         
         
 if __name__ == '__main__':
-    sys.exit(main())              
+    main()