-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import MySQLdb | ||
from WWMException import WWMException | ||
from Config import Config,Constants | ||
|
||
import time, Queue | ||
|
||
class CallbackDBI: | ||
def __init__(self): | ||
self.dbi = None | ||
self.cursor = None | ||
self.connectedAt = 0 | ||
self.connect() #Hack | ||
self.waitTimeout = Config.CallbackDBI_queryTimeout | ||
self.queryQueue = Queue.Queue(50) | ||
|
||
def connect(self): | ||
try: | ||
print "CallbackDBI CONNECTING TO DB!" | ||
self.dbi = MySQLdb.connect(Config.mysql_host,Config.mysql_user,Config.mysql_password,Config.mysql_database) | ||
self.cursor = self.dbi.cursor(MySQLdb.cursors.DictCursor) | ||
#All hail autocommit! | ||
self.dbi.autocommit(True) | ||
|
||
self.connectedAt = time.time() | ||
self.inUse =0 | ||
|
||
except MySQLdb.Error, e: | ||
self.close() | ||
raise WWMException("MySQL could not connect: %s" %e) | ||
|
||
def addToQueue(self, queryTup): | ||
self.queryQueue.put(queryTup) | ||
|
||
def processQueue(self): | ||
while self.queryQueue.qsize()>0: | ||
q = self.queryQueue.get() | ||
queryStr, args = q | ||
self.executeQuery(queryStr,args) | ||
|
||
def execute(self,queryStr, args): | ||
waitingFor = 0 | ||
while self.inUse: | ||
if waitingFor>=self.waitTimeout: | ||
self.addToQueue((queryStr,args)) #the | ||
return | ||
|
||
time.sleep(1) | ||
waitingFor+= 1 | ||
|
||
self.inUse = 1 | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
krishnangovindraj
Author
Owner
|
||
self.executeQuery(queryStr,args) | ||
self.processQueue() #Whichever query was added | ||
|
||
self.inUse = 0 | ||
|
||
def executeQuery(self,queryStr,args): #Yes, It needs to be this complicated. | ||
self.cursor.execute(queryStr,args) | ||
|
||
|
||
def close(self): | ||
print "DBI.close WAS CALLED!" | ||
#Closing/Querying a closed connection crashes python. So be careful | ||
if self.dbi!= None and self.dbi.open!=0: | ||
print "CLOSING CONN TO DB!" | ||
self.dbi.close() | ||
self.dbi = None | ||
self.dbiCursor = None | ||
self.connectedAt = 0 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,8 @@ | |
from Session import Session | ||
from Sender import Sender | ||
from Listener import Listener | ||
from DBInterface import DBI | ||
from DBInterface import DBI, BlockingDBICursor | ||
from CallbackDBI import CallbackDBI | ||
|
||
import MySQLdb,time, os, hashlib | ||
class WWMCore: | ||
|
@@ -27,7 +28,9 @@ def __init__(self): | |
self.httpRequestHandler = None | ||
self.instanceId = None | ||
self.status = Constants.INSTANCESTATUS_INITED | ||
self.dbi = DBI() #Make an instance since it's a core part of the webapp. It'll control itself | ||
|
||
self.dbi = DBI() #Use this for HTTP requests only. For every other case, Go ahead and create a different connection for now | ||
self.callbackDBI = CallbackDBI() #Extra care for preventing interference | ||
#self.dbiCursor = None | ||
self.yowsupStarted = 0 | ||
|
||
|
@@ -70,14 +73,15 @@ def getDBICursor(self): #returns dbi and cursor | |
#This method is called from session | ||
def initSession(self, phone, AESKey): | ||
print "Core.initSession called" | ||
if self.yowsupStarted==0 and (self.session == None or self.session.authStatus == Constants.AUTHSTATUS_IDLE): | ||
if self.session == None or self.session.authStatus == Constants.AUTHSTATUS_IDLE: #self.yowsupStarted==0 and | ||
self.yowsupStarted = 1 | ||
if self.session == None: | ||
if self.session == None: | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
krishnangovindraj
Author
Owner
|
||
self.session = Session(self, phone, AESKey) | ||
self.session.getAuthData() | ||
self.signalsInterface.registerListener("disconnected", self.onDisconnected) | ||
self.session.login() | ||
|
||
else: | ||
print "\nPretty sure yowsup is already started." | ||
|
||
def authCallback(self,isSuccess): #Called manually from Session | ||
if isSuccess: | ||
|
@@ -92,15 +96,16 @@ def authCallback(self,isSuccess): #Called manually from Session | |
self.yowsupStarted= 0 | ||
|
||
def onDisconnected(self, reason): | ||
|
||
print "Core.onDisconnected called" | ||
|
||
if self.status==Constants.INSTANCESTATUS_WRAPPEDUP: | ||
print "Core.onDisconnected: Disconnected and wrapping up" | ||
return #And die | ||
|
||
|
||
self.yowsupStarted = 0 | ||
self.session.updateAuthStatus(Constants.AUTHSTATUS_IDLE) | ||
self.session.authStatus = Constants.AUTHSTATUS_IDLE | ||
#self.session.updateAuthStatus(Constants.AUTHSTATUS_IDLE) | ||
|
||
|
||
if self.yowsupRanOnce==0: | ||
|
@@ -133,7 +138,7 @@ def getStatus(self): | |
#Create a dictionary and return it | ||
status = {} | ||
|
||
status["Core"] = [("yowsupStarted", self.yowsupStarted)] | ||
status["Core"] = [("yowsupStarted", self.yowsupStarted), ("instanceStatus", self.status)] | ||
|
||
if self.session==None: | ||
status["Session"]=[("inited","No")] | ||
|
@@ -150,10 +155,8 @@ def wrapUp(self, reason): | |
#Write some code to wrap up | ||
self.status = Constants.INSTANCESTATUS_WRAPPEDUP | ||
|
||
dbiCursor = self.dbi.getCursor() | ||
dbiCursor.execute("UPDATE pythonInstances set status=%s WHERE instanceId=%s ", (self.status, self.instanceId)) | ||
self.dbi.commit() | ||
self.dbi.done() | ||
with BlockingDBICursor(self.dbi) as dbiCursor: | ||
dbiCursor.execute("UPDATE pythonInstances set status=%s WHERE instanceId=%s ", (self.status, self.instanceId)) | ||
|
||
self.connectionManager.disconnect(reason) | ||
self.yowsupStarted = 0 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,20 @@ | ||
README: | ||
Aim: Make a web client for whatsapp. Do whatever you want as long as that's achieved. And rewrite this readme when you're done | ||
|
||
NOTE: You need Yowsup copied to the same directory. ie .\Yowsup has to contain all the Yowsup library files | ||
|
||
NOTE: | ||
You need Yowsup Library copied to the same directory. ie .\Yowsup has to contain all the Yowsup library files | ||
|
||
OTHER DEPENDENCIES: | ||
MySQLdb ( If you can rewrite it to have a server independent solution, Please do :) ) | ||
|
||
Usage: | ||
Import the webwhatsapp.sql file into a database name webwhatasapp ( or reconfigure your config.py mysql_ fields accordingly ) | ||
adduser by running >py adduser.py through the commandline ( Web interface adding users is an easy to do ) | ||
Launch an instance using >py wsgi.py | ||
visit http://host:portnumber | ||
Login using the password you specified. | ||
That should be it | ||
|
||
Repo: https://github.com/krishnangovindraj/webwhatsapp | ||
Contact: [email protected] |
I think this should be declared at the beginning of the method as the while loop fails for the first entry to the queue as
inUse
is set to 0