Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/nxt_plugin/nxt/ipsock.py
diff options
context:
space:
mode:
Diffstat (limited to 'nxt_plugin/nxt/ipsock.py')
-rw-r--r--nxt_plugin/nxt/ipsock.py61
1 files changed, 61 insertions, 0 deletions
diff --git a/nxt_plugin/nxt/ipsock.py b/nxt_plugin/nxt/ipsock.py
new file mode 100644
index 0000000..66c1a51
--- /dev/null
+++ b/nxt_plugin/nxt/ipsock.py
@@ -0,0 +1,61 @@
+# nxt.ipsock module -- Server socket communication with LEGO Minstorms NXT
+# Copyright (C) 2011 zonedaobne, Marcus Wanner
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+
+import socket
+from nxt.brick import Brick
+
+class IpSock(object):
+ def __init__(self, host, port):
+ self.host = host
+ self.port = port
+ self.sock = None
+ self.debug = False
+
+ def __str__(self):
+ return 'Server (%s)' % self.host
+
+ def connect(self):
+ if self.debug:
+ print 'Connecting via Server...'
+ sock = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
+ sock.connect((self.host, self.port))
+ #TODO: sasl authentication here?
+ self.sock = sock
+ if self.debug:
+ print 'Connected.'
+ self.send('\x98')
+ self.type = 'ip'+self.recv()
+ return Brick(self)
+
+ def close(self):
+ if self.debug:
+ print 'Closing Server connection...'
+ self.sock.send('\x99')
+ self.sock.close()
+ if self.debug:
+ print 'Server connection closed.'
+
+ def send(self, data):
+ if self.debug:
+ print 'Send:',
+ print ':'.join('%02x' % ord(c) for c in data)
+ self.sock.send(data)
+
+ def recv(self):
+ data = self.sock.recv(1024)
+ if self.debug:
+ print 'Recv:',
+ print ':'.join('%02x' % ord(c) for c in data)
+ return data
+
+#TODO: add a find_bricks method and a corresponding broadcast system to nxt_server?