Skip to content
Browse files

Menu entries + multiple threads.

  • Loading branch information...
1 parent 80f55bc commit 1b15f666235f0f574337503141662d83519b1954 @mmarcon committed Aug 15, 2012
Showing with 101 additions and 28 deletions.
  1. +1 −0 .gitignore
  2. +22 −0 livecode/Context.sublime-menu
  3. +29 −14 livecode/Main.sublime-menu
  4. +27 −13 livecode/livecode.py
  5. +9 −0 livecode/main.py
  6. +13 −1 livecode/websocket.py
View
1 .gitignore
@@ -1 +1,2 @@
server/node_modules
+livecode/*.pyc
View
22 livecode/Context.sublime-menu
@@ -0,0 +1,22 @@
+[{
+ "id": "livecode",
+ "caption": "Livecode",
+ "children": [
+ {
+ "id": "enable",
+ "caption": "Enable",
+ "command": "livecode",
+ "args": {
+ "execute": "on"
+ }
+ },
+ {
+ "id": "disable",
+ "caption": "Disable",
+ "command": "livecode",
+ "args": {
+ "execute": "off"
+ }
+ }
+ ]
+}]
View
43 livecode/Main.sublime-menu
@@ -1,14 +1,29 @@
-[
- {
- "id": "tools",
- "children":
- [
- {"id": "livecode-on"},
- { "command": "livecode",
- "args": {
- "execute": "on"
- }
- }
- ]
- }
-]
+[
+ {
+ "id": "tools",
+ "children": [
+ {
+ "id": "livecode",
+ "caption": "Livecode",
+ "children": [
+ {
+ "id": "enable",
+ "caption": "Enable",
+ "command": "livecode",
+ "args": {
+ "execute": "on"
+ }
+ },
+ {
+ "id": "disable",
+ "caption": "Disable",
+ "command": "livecode",
+ "args": {
+ "execute": "off"
+ }
+ }
+ ]
+ }
+ ]
+ }
+]
View
40 livecode/livecode.py
@@ -12,6 +12,7 @@ def __init__(self, view):
self.ws = None
self.view = view
self.running = False
+ self.buffer = None
def run(self, edit, execute=None):
self.choose(execute, edit)
@@ -24,22 +25,35 @@ def choose(self, command, edit):
def turn_on(self, edit):
if self.running == False:
- self.ws = create_connection("ws://echo.websocket.org/")
+ self.ws = create_connection("ws://localhost:8000")
+ self.ws.settimeout(800)
+ self.running = True
+ thread.start_new_thread(lambda: self.getbuffer(edit), ())
thread.start_new_thread(lambda: self.send(edit), ())
- def send(self, edit):
- self.running = True
-
- def sendbuffer(*args):
+ def getbuffer(self, edit):
+ print "*** ***"
+ def get_buffer_internal(*args):
+ print "Getting Buffer"
view = sublime.active_window().active_view()
- self.ws.send(view.substr(sublime.Region(0, view.size())))
-
- while self.running:
- print "Sending buffer content..."
- sublime.set_timeout(sendbuffer, 1)
- print "Sent"
- #result = self.ws.recv()
- #print "Received '%s'" % result
+ self.buffer = view.substr(sublime.Region(0, view.size()));
+
+ while self.running and self.ws.connected:
+ print "Other LOOP"
+ sublime.set_timeout(get_buffer_internal , 1)
+ time.sleep(6)
+
+ def send(self, edit):
+ while self.running and self.ws.connected:
+ try :
+ print "LOOP"
+ if self.buffer != None:
+ print "Sending buffer content..."
+ self.ws.send(self.buffer)
+ print "Sent"
+ except Exception, e:
+ print "Uhmmm... something wrong happened here..."
+ self.turn_off(edit)
time.sleep(5)
def turn_off(self, edit):
View
9 livecode/main.py
@@ -0,0 +1,9 @@
+from websocket import create_connection
+import thread
+import time
+
+ws = create_connection("ws://localhost:8000")
+ws.settimeout(800)
+while True:
+ ws.send("HELLO")
+ time.sleep(5)
View
14 livecode/websocket.py
@@ -332,6 +332,10 @@ def __init__(self, get_mask_key = None):
"""
self.connected = False
self.io_sock = self.sock = socket.socket()
+ """
+ I am making it non blocking. I am only sending anyway, so who cares?!
+ """
+ self.sock.setblocking(0)
self.get_mask_key = get_mask_key
def set_mask_key(self, func):
@@ -489,7 +493,12 @@ def send(self, payload, opcode = ABNF.OPCODE_TEXT):
if self.get_mask_key:
frame.get_mask_key = self.get_mask_key
data = frame.format()
- self.io_sock.send(data)
+
+ try:
+ self.io_sock.send(data)
+ except Exception, e:
+ self.connected = False
+
if traceEnabled:
logger.debug("send: " + repr(data))
@@ -622,6 +631,9 @@ def close(self, status = STATUS_NORMAL, reason = ""):
pass
self._closeInternal()
+ # def connected(self):
+ # return self.connected
+
def _closeInternal(self):
self.connected = False
self.sock.close()

0 comments on commit 1b15f66

Please sign in to comment.
Something went wrong with that request. Please try again.