You are viewing a plain text version of this content. The canonical link for it is here.
Posted to s4-commits@incubator.apache.org by mm...@apache.org on 2012/01/03 11:19:14 UTC
[2/50] [abbrv] git commit: Rename packages in preparation for move to
Apache
Rename packages in preparation for move to Apache
Project: http://git-wip-us.apache.org/repos/asf/incubator-s4/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-s4/commit/bca594c5
Tree: http://git-wip-us.apache.org/repos/asf/incubator-s4/tree/bca594c5
Diff: http://git-wip-us.apache.org/repos/asf/incubator-s4/diff/bca594c5
Branch: refs/heads/dev
Commit: bca594c5f0d17ec10d25225ca96b7cacabc73d6e
Parents: fb5875a
Author: Bruce Robbins <ro...@everychoose-lm.corp.yahoo.com>
Authored: Mon Nov 28 14:52:39 2011 -0800
Committer: Bruce Robbins <ro...@everychoose-lm.corp.yahoo.com>
Committed: Mon Nov 28 14:52:39 2011 -0800
----------------------------------------------------------------------
s4-driver/python/io/s4/client/driver.py | 181 ----------------
s4-driver/python/io/s4/client/helper.py | 79 -------
s4-driver/python/org/apache/s4/client/driver.py | 181 ++++++++++++++++
s4-driver/python/org/apache/s4/client/helper.py | 79 +++++++
4 files changed, 260 insertions(+), 260 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-s4/blob/bca594c5/s4-driver/python/io/__init__.py
----------------------------------------------------------------------
diff --git a/s4-driver/python/io/__init__.py b/s4-driver/python/io/__init__.py
deleted file mode 100644
index e69de29..0000000
http://git-wip-us.apache.org/repos/asf/incubator-s4/blob/bca594c5/s4-driver/python/io/s4/__init__.py
----------------------------------------------------------------------
diff --git a/s4-driver/python/io/s4/__init__.py b/s4-driver/python/io/s4/__init__.py
deleted file mode 100644
index e69de29..0000000
http://git-wip-us.apache.org/repos/asf/incubator-s4/blob/bca594c5/s4-driver/python/io/s4/client/__init__.py
----------------------------------------------------------------------
diff --git a/s4-driver/python/io/s4/client/__init__.py b/s4-driver/python/io/s4/client/__init__.py
deleted file mode 100644
index e69de29..0000000
http://git-wip-us.apache.org/repos/asf/incubator-s4/blob/bca594c5/s4-driver/python/io/s4/client/driver.py
----------------------------------------------------------------------
diff --git a/s4-driver/python/io/s4/client/driver.py b/s4-driver/python/io/s4/client/driver.py
deleted file mode 100644
index 83d1e94..0000000
--- a/s4-driver/python/io/s4/client/driver.py
+++ /dev/null
@@ -1,181 +0,0 @@
-#
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-import socket
-import sys
-from struct import pack
-from struct import unpack
-
-# json was added to python-2.6
-# before that, needed simplejson
-try:
- import json
-except ImportError:
- import simplejson as json
-
-from helper import _ByteIO
-
-class Driver:
-
- protocolName = "generic-json"
- versionMajor = 1;
- versionMinor = 0;
-
- _bio = _ByteIO();
- _debug = False;
-
- def setDebug(self, d):
- self._debug = d;
- self._bio._debug = d;
-
- def __init__(self, hostname, port):
- self.hostname = hostname
- self.port = port
- self.state = "null"
-
-
- def initialize(self):
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- sock.setblocking(True)
- sock.connect((self.hostname, self.port))
-
- self._bio.send_byte_array(sock, bytearray(0));
-
- [response, t] = self._bio.recv_byte_array(sock)
-
- sock.close()
-
- r = json.loads(response.decode('utf-8'))
-
- if not self._iscompatible(r['protocol']):
- return False;
-
- self.uuid = r['uuid'];
- self.state = "initialized"
-
- if self._debug:
- print >> sys.stderr, "Initialized. uuid: " + self.uuid;
-
- return True
-
-
- def connect(self, spec = {'readMode': 'private', 'writeMode': 'enabled'}):
- if self.state != 'initialized':
- return False;
-
- conn = spec;
- conn['uuid'] = self.uuid;
- cstr = json.dumps(conn).encode('utf-8');
-
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- sock.setblocking(True)
- sock.connect((self.hostname, self.port))
-
- self._bio.send_byte_array(sock, cstr);
-
- [response, t] = self._bio.recv_byte_array(sock);
- r = json.loads(response.decode('utf-8'));
-
- if r['status'] == 'ok':
- if self._debug:
- print >> sys.stderr, "Connected"
- self.state = "connected"
- self.sock = sock;
- return True;
- else:
- if self._debug:
- print >> sys.stderr, "Connect failed. " + response;
- sock.close();
- return False;
-
- def sendKeyed(self, stream, clazz, keys, object):
- if self.state != 'connected':
- return 0;
- self._send(stream, clazz, keys, object);
-
- def send(self, stream, clazz, object):
- if self.state != 'connected':
- return 0;
- self._send(stream, clazz, None, object);
-
- def recv(self, timeout=0):
- if self.state != 'connected':
- return None;
- [b, t] = self._bio.recv_byte_array(self.sock, timeout);
- if b == None or len(b) == 0:
- return None;
-
- m = json.loads(b.decode('utf-8'));
-
- return m;
-
- def recvAll(self, interval):
- if self.state != 'connected':
- return False;
-
- messages = [];
-
- try:
- tr = interval
- while (tr > 0):
- [b, t] = self._bio.recv_byte_array(self.sock, tr);
- if b == None or len(b) == 0:
- break
-
- m = json.loads(b.decode('utf-8'));
- messages.append(m);
-
- tr = tr - t
-
- except socket.timeout:
- # Nothing to do here
- 1
-
- return messages;
-
- def disconnect(self):
- if self.state != 'connected':
- return False;
-
- sock = self.sock;
- self._bio.send_byte_array(sock, bytearray(0));
- sock.close();
-
- return True;
-
-# PRIVATE FUNCTIONS
- def _send(self, stream, clazz, keys, object):
- message = {
- 'stream': str(stream),
- 'class': str(clazz),
- 'object': str(object)
- }
-
- if keys != None and isinstance(keys, ListType):
- message['keys'] = map(str, keys); # convert all keys to strings
-
- m = json.dumps(message);
- self._bio.send_byte_array(self.sock, m.encode('utf-8'));
-
- def _iscompatible(self, p):
- return (p['name'] == self.protocolName
- and p['versionMajor'] == self.versionMajor
- and p['versionMinor'] >= self.versionMinor)
-
-
-
http://git-wip-us.apache.org/repos/asf/incubator-s4/blob/bca594c5/s4-driver/python/io/s4/client/helper.py
----------------------------------------------------------------------
diff --git a/s4-driver/python/io/s4/client/helper.py b/s4-driver/python/io/s4/client/helper.py
deleted file mode 100644
index 535aefb..0000000
--- a/s4-driver/python/io/s4/client/helper.py
+++ /dev/null
@@ -1,79 +0,0 @@
-#
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-from struct import pack
-from struct import pack_into
-from struct import unpack_from
-import sys
-import time
-from ctypes import create_string_buffer
-
-# Helper Classes
-
-# Byte-oriented IO
-class _ByteIO:
- _debug = False;
-
- def recv_bytes(self, sock, n, timeout=0):
- b = bytearray()
- r = 0
-
- tNow = time.time()
- tEnd = tNow + timeout
- tStart = tNow
-
- while (r < n):
- if (timeout > 0):
- sock.settimeout(tEnd - tNow)
-
- p = sock.recv(n-r) # partial recv
- b.extend(p)
- r += len(p)
- tNow = time.time()
-
- return [b, (tNow-tStart)]
-
-# Better: r += sock.recv_into(buffer(b, r), (n-r));
-# But doesn't work on MacOS X :(
-
- def recv_byte_array(self, sock, timeout=0):
- [s, t0] = self.recv_bytes(sock, 4, timeout)
- sz = unpack_from('>I', buffer(s))[0]
-
- tr = 0;
- if (timeout > 0):
- if (t0 < timeout):
- tr = timeout - t0;
- else:
- tr = 0.001;
-
- [m, t1] = self.recv_bytes(sock, sz, tr)
-
- if self._debug:
- print >> sys.stderr, ">>[" + str(sz) + "]" + str(m)
-
- return [m, t0+t1]
-
- def send_byte_array(self, sock, b):
- sz = pack('>I', len(b));
- sock.sendall(sz);
- sock.sendall(b);
-
- if self._debug:
- print >> sys.stderr, "<<[" + str(len(b)) + "]" + str(b)
-
http://git-wip-us.apache.org/repos/asf/incubator-s4/blob/bca594c5/s4-driver/python/org/__init__.py
----------------------------------------------------------------------
diff --git a/s4-driver/python/org/__init__.py b/s4-driver/python/org/__init__.py
new file mode 100644
index 0000000..e69de29
http://git-wip-us.apache.org/repos/asf/incubator-s4/blob/bca594c5/s4-driver/python/org/apache/__init__.py
----------------------------------------------------------------------
diff --git a/s4-driver/python/org/apache/__init__.py b/s4-driver/python/org/apache/__init__.py
new file mode 100644
index 0000000..e69de29
http://git-wip-us.apache.org/repos/asf/incubator-s4/blob/bca594c5/s4-driver/python/org/apache/s4/__init__.py
----------------------------------------------------------------------
diff --git a/s4-driver/python/org/apache/s4/__init__.py b/s4-driver/python/org/apache/s4/__init__.py
new file mode 100644
index 0000000..e69de29
http://git-wip-us.apache.org/repos/asf/incubator-s4/blob/bca594c5/s4-driver/python/org/apache/s4/client/__init__.py
----------------------------------------------------------------------
diff --git a/s4-driver/python/org/apache/s4/client/__init__.py b/s4-driver/python/org/apache/s4/client/__init__.py
new file mode 100644
index 0000000..e69de29
http://git-wip-us.apache.org/repos/asf/incubator-s4/blob/bca594c5/s4-driver/python/org/apache/s4/client/driver.py
----------------------------------------------------------------------
diff --git a/s4-driver/python/org/apache/s4/client/driver.py b/s4-driver/python/org/apache/s4/client/driver.py
new file mode 100644
index 0000000..83d1e94
--- /dev/null
+++ b/s4-driver/python/org/apache/s4/client/driver.py
@@ -0,0 +1,181 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+import socket
+import sys
+from struct import pack
+from struct import unpack
+
+# json was added to python-2.6
+# before that, needed simplejson
+try:
+ import json
+except ImportError:
+ import simplejson as json
+
+from helper import _ByteIO
+
+class Driver:
+
+ protocolName = "generic-json"
+ versionMajor = 1;
+ versionMinor = 0;
+
+ _bio = _ByteIO();
+ _debug = False;
+
+ def setDebug(self, d):
+ self._debug = d;
+ self._bio._debug = d;
+
+ def __init__(self, hostname, port):
+ self.hostname = hostname
+ self.port = port
+ self.state = "null"
+
+
+ def initialize(self):
+ sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ sock.setblocking(True)
+ sock.connect((self.hostname, self.port))
+
+ self._bio.send_byte_array(sock, bytearray(0));
+
+ [response, t] = self._bio.recv_byte_array(sock)
+
+ sock.close()
+
+ r = json.loads(response.decode('utf-8'))
+
+ if not self._iscompatible(r['protocol']):
+ return False;
+
+ self.uuid = r['uuid'];
+ self.state = "initialized"
+
+ if self._debug:
+ print >> sys.stderr, "Initialized. uuid: " + self.uuid;
+
+ return True
+
+
+ def connect(self, spec = {'readMode': 'private', 'writeMode': 'enabled'}):
+ if self.state != 'initialized':
+ return False;
+
+ conn = spec;
+ conn['uuid'] = self.uuid;
+ cstr = json.dumps(conn).encode('utf-8');
+
+ sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ sock.setblocking(True)
+ sock.connect((self.hostname, self.port))
+
+ self._bio.send_byte_array(sock, cstr);
+
+ [response, t] = self._bio.recv_byte_array(sock);
+ r = json.loads(response.decode('utf-8'));
+
+ if r['status'] == 'ok':
+ if self._debug:
+ print >> sys.stderr, "Connected"
+ self.state = "connected"
+ self.sock = sock;
+ return True;
+ else:
+ if self._debug:
+ print >> sys.stderr, "Connect failed. " + response;
+ sock.close();
+ return False;
+
+ def sendKeyed(self, stream, clazz, keys, object):
+ if self.state != 'connected':
+ return 0;
+ self._send(stream, clazz, keys, object);
+
+ def send(self, stream, clazz, object):
+ if self.state != 'connected':
+ return 0;
+ self._send(stream, clazz, None, object);
+
+ def recv(self, timeout=0):
+ if self.state != 'connected':
+ return None;
+ [b, t] = self._bio.recv_byte_array(self.sock, timeout);
+ if b == None or len(b) == 0:
+ return None;
+
+ m = json.loads(b.decode('utf-8'));
+
+ return m;
+
+ def recvAll(self, interval):
+ if self.state != 'connected':
+ return False;
+
+ messages = [];
+
+ try:
+ tr = interval
+ while (tr > 0):
+ [b, t] = self._bio.recv_byte_array(self.sock, tr);
+ if b == None or len(b) == 0:
+ break
+
+ m = json.loads(b.decode('utf-8'));
+ messages.append(m);
+
+ tr = tr - t
+
+ except socket.timeout:
+ # Nothing to do here
+ 1
+
+ return messages;
+
+ def disconnect(self):
+ if self.state != 'connected':
+ return False;
+
+ sock = self.sock;
+ self._bio.send_byte_array(sock, bytearray(0));
+ sock.close();
+
+ return True;
+
+# PRIVATE FUNCTIONS
+ def _send(self, stream, clazz, keys, object):
+ message = {
+ 'stream': str(stream),
+ 'class': str(clazz),
+ 'object': str(object)
+ }
+
+ if keys != None and isinstance(keys, ListType):
+ message['keys'] = map(str, keys); # convert all keys to strings
+
+ m = json.dumps(message);
+ self._bio.send_byte_array(self.sock, m.encode('utf-8'));
+
+ def _iscompatible(self, p):
+ return (p['name'] == self.protocolName
+ and p['versionMajor'] == self.versionMajor
+ and p['versionMinor'] >= self.versionMinor)
+
+
+
http://git-wip-us.apache.org/repos/asf/incubator-s4/blob/bca594c5/s4-driver/python/org/apache/s4/client/helper.py
----------------------------------------------------------------------
diff --git a/s4-driver/python/org/apache/s4/client/helper.py b/s4-driver/python/org/apache/s4/client/helper.py
new file mode 100644
index 0000000..535aefb
--- /dev/null
+++ b/s4-driver/python/org/apache/s4/client/helper.py
@@ -0,0 +1,79 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+from struct import pack
+from struct import pack_into
+from struct import unpack_from
+import sys
+import time
+from ctypes import create_string_buffer
+
+# Helper Classes
+
+# Byte-oriented IO
+class _ByteIO:
+ _debug = False;
+
+ def recv_bytes(self, sock, n, timeout=0):
+ b = bytearray()
+ r = 0
+
+ tNow = time.time()
+ tEnd = tNow + timeout
+ tStart = tNow
+
+ while (r < n):
+ if (timeout > 0):
+ sock.settimeout(tEnd - tNow)
+
+ p = sock.recv(n-r) # partial recv
+ b.extend(p)
+ r += len(p)
+ tNow = time.time()
+
+ return [b, (tNow-tStart)]
+
+# Better: r += sock.recv_into(buffer(b, r), (n-r));
+# But doesn't work on MacOS X :(
+
+ def recv_byte_array(self, sock, timeout=0):
+ [s, t0] = self.recv_bytes(sock, 4, timeout)
+ sz = unpack_from('>I', buffer(s))[0]
+
+ tr = 0;
+ if (timeout > 0):
+ if (t0 < timeout):
+ tr = timeout - t0;
+ else:
+ tr = 0.001;
+
+ [m, t1] = self.recv_bytes(sock, sz, tr)
+
+ if self._debug:
+ print >> sys.stderr, ">>[" + str(sz) + "]" + str(m)
+
+ return [m, t0+t1]
+
+ def send_byte_array(self, sock, b):
+ sz = pack('>I', len(b));
+ sock.sendall(sz);
+ sock.sendall(b);
+
+ if self._debug:
+ print >> sys.stderr, "<<[" + str(len(b)) + "]" + str(b)
+