On 06/11/2010 11:33 PM, Paolo Bonzini wrote:

> Just name it "size" please.  There is no <comment: ''> pragma in
> methods.  Finally, please put a single ^ outside the if blocks.

So no return from within the if? What is the reasoning? What would be
preferred, assign to a local variable and then return, or put the return
before the object ifNil check?



> 
> This should go to a (very high) superclass as you suspected.

I picked the AbstractSocket class... maybe we should even move it into
SocketAddr?

I have three patches attached to carry out these changes. Looking for
comments.


>From b531e7d40a0453e9cdad3b2e6d5aed80f09fef19 Mon Sep 17 00:00:00 2001
From: Holger Hans Peter Freyther <[email protected]>
Date: Sat, 12 Jun 2010 11:52:36 +0800
Subject: [PATCH 1/3] UDP: Make it possible to find out how many bytes were received

This makes it possible to see the number of bytes received by
the recvfrom call and such a Datagram can be send using
the #nextPut: selector.

2010-06-12  Holger Hans Peter Freyther  <[email protected]>

	* Datagram.st: Add #dataSize, #dataSize: and #size.
	* AbstractSocketImpl.st: Call Datagram>>#dataSize: from the
	#receive:datagram selector with the bytes read, use
	Datagram>>#size in the #send:to:port selector to only send
	a limited amount of bytes from the Datagram>>#data.
---
 packages/sockets/AbstractSocketImpl.st |    7 +++--
 packages/sockets/ChangeLog             |    8 +++++++
 packages/sockets/Datagram.st           |   36 ++++++++++++++++++++++++++++++-
 3 files changed, 46 insertions(+), 5 deletions(-)

diff --git a/packages/sockets/AbstractSocketImpl.st b/packages/sockets/AbstractSocketImpl.st
index a71410e..0e5d8ce 100644
--- a/packages/sockets/AbstractSocketImpl.st
+++ b/packages/sockets/AbstractSocketImpl.st
@@ -647,12 +647,12 @@ implementations.'>
 	 to only peek for it without removing it from the queue."
 
 	<category: 'socket operations'>
-	| address port data from addrLen fd |
+	| address port data from addrLen fd read |
 	data := ByteArray new: self bufferSize.
 	from := ByteArray new: 128.
 	addrLen := CInt gcValue: 128.
 	(fd := self fd) isNil ifTrue: [ ^SystemExceptions.EndOfStream signal ].
-	self 
+	read := self 
 	    receive: fd
 	    buffer: data
 	    size: data size
@@ -662,6 +662,7 @@ implementations.'>
 	port := ValueHolder new.
 	^aDatagram
 	    data: data;
+	    dataSize: read;
 	    address: (SocketAddress fromSockAddr: from port: port);
 	    port: port value;
 	    yourself
@@ -681,7 +682,7 @@ implementations.'>
 	self 
 	    send: fd
 	    buffer: aDatagram data
-	    size: aDatagram data size
+	    size: aDatagram size
 	    flags: self flags
 	    to: receiver
 	    size: size
diff --git a/packages/sockets/ChangeLog b/packages/sockets/ChangeLog
index 4ed3aba..a6ac75e 100644
--- a/packages/sockets/ChangeLog
+++ b/packages/sockets/ChangeLog
@@ -1,3 +1,11 @@
+2010-06-12  Holger Hans Peter Freyther  <[email protected]>
+
+	* Datagram.st: Add #dataSize, #dataSize: and #size.
+	* AbstractSocketImpl.st: Call Datagram>>#dataSize: from the
+	#receive:datagram selector with the bytes read, use
+	Datagram>>#size in the #send:to:port selector to only send
+	a limited amount of bytes from the Datagram>>#data.
+
 2010-03-27  Paolo Bonzini <[email protected]>
 
 	* AbstractSocketImpl.st: Do not check errno unless a system call
diff --git a/packages/sockets/Datagram.st b/packages/sockets/Datagram.st
index 0793601..a5a3126 100644
--- a/packages/sockets/Datagram.st
+++ b/packages/sockets/Datagram.st
@@ -32,7 +32,7 @@
 
 
 Object subclass: Datagram [
-    | data address port |
+    | data address port dataSize |
     
     <category: 'Sockets-Protocols'>
     <comment: '
@@ -125,9 +125,41 @@ This class can also be used for receiving data from the network.'>
 	"Set the data attached to the datagram"
 
 	<category: 'accessing'>
-	data := aByteArray
+	data := aByteArray.
+	dataSize := nil.
     ]
 
+    dataSize [
+	"Answer the size of the message."
+
+	<category: 'accessing'>
+
+	^dataSize
+    ]
+
+    dataSize: aSize [
+	"I am called to update the size..."
+
+	<category: 'accessing'>
+
+	dataSize := aSize		
+    ]
+
+    size [
+	"I determine the size of the datagram. It is either the specified dataSize or
+	 data size."
+
+        <category: 'accessing'>
+	| result |
+
+        result := dataSize isNil
+	    ifTrue: [data size]
+	    ifFalse: [dataSize].
+
+	^ result
+    ]
+
+
     get [
 	"Parse the data attached to the datagram through a newly created
 	 ObjectDumper, and answer the resulting object.  This method is
-- 
1.7.0.1

>From 408d55282f4eed62adf9a6e3b0fce88fef35fc83 Mon Sep 17 00:00:00 2001
From: Holger Hans Peter Freyther <[email protected]>
Date: Sat, 12 Jun 2010 11:59:12 +0800
Subject: [PATCH 2/3] 2010-06-12  Holger Hans Peter Freyther  <[email protected]>

	* Sockets.st: Add AbstractSocket class>>#addressFromString to
	parse a string into a SocketAddress and use it inside the
	ServerSocket and DatagramSocket class.
---
 packages/sockets/ChangeLog  |    6 ++++++
 packages/sockets/Sockets.st |   26 ++++++++++++++++----------
 2 files changed, 22 insertions(+), 10 deletions(-)

diff --git a/packages/sockets/ChangeLog b/packages/sockets/ChangeLog
index a6ac75e..ba4dd79 100644
--- a/packages/sockets/ChangeLog
+++ b/packages/sockets/ChangeLog
@@ -1,5 +1,11 @@
 2010-06-12  Holger Hans Peter Freyther  <[email protected]>
 
+	* Sockets.st: Add AbstractSocket class>>#addressFromString to
+	parse a string into a SocketAddress and use it inside the
+	ServerSocket and DatagramSocket class.
+
+2010-06-12  Holger Hans Peter Freyther  <[email protected]>
+
 	* Datagram.st: Add #dataSize, #dataSize: and #size.
 	* AbstractSocketImpl.st: Call Datagram>>#dataSize: from the
 	#receive:datagram selector with the bytes read, use
diff --git a/packages/sockets/Sockets.st b/packages/sockets/Sockets.st
index a279c42..471f528 100644
--- a/packages/sockets/Sockets.st
+++ b/packages/sockets/Sockets.st
@@ -304,6 +304,20 @@ implementation objects.'>
 	DefaultAddressClass := class
     ]
 
+    AbstractSocket class >> addressFromString: ipAddressOrString [
+	| addr |
+
+	ipAddressOrString isString 
+	    ifTrue: [
+		addr := SocketAddress byName: ipAddressOrString.
+		addr isNil 
+		    ifTrue: 
+			[self error: 'cannot resolve host name ' , ipAddressOrString printString]]
+	    ifFalse: [addr := ipAddressOrString].
+
+	^ addr
+    ]
+
     AbstractSocket class >> new: implementation [
 	"Answer a new instance of the receiver, using as the underlying
 	 layer the object passed as the `implementation' parameter; the
@@ -639,13 +653,7 @@ but it is done for cleanliness and symmetry.'>
 
 	<category: 'instance creation'>
 	| localAddr remoteAddr addressClass |
-	ipAddressOrString isString 
-	    ifTrue: [
-		remoteAddr := SocketAddress byName: ipAddressOrString.
-		remoteAddr isNil 
-		    ifTrue: 
-			[self error: 'cannot resolve host name ' , ipAddressOrString printString]]
-	    ifFalse: [remoteAddr := ipAddressOrString].
+	remoteAddr := self addressFromString: ipAddressOrString.
 	addressClass := remoteAddr isNil 
 		    ifTrue: [self defaultAddressClass]
 		    ifFalse: [remoteAddr class].
@@ -1079,9 +1087,7 @@ This class adds a read buffer to the basic model of AbstractSocket.'>
 
 	<category: 'instance creation'>
 	| localAddr remoteAddr addressClass |
-	remoteAddr := ipAddressOrString isString 
-		    ifTrue: [SocketAddress byName: ipAddressOrString]
-		    ifFalse: [ipAddressOrString].
+	remoteAddr := self addressFromString ipAddressOrString.
 	remoteAddr isNil 
 	    ifTrue: 
 		[self error: 'cannot resolve host name ' , ipAddressOrString printString].
-- 
1.7.0.1

>From e2498d102ed8f3db3837769a977dc144767739ec Mon Sep 17 00:00:00 2001
From: Holger Hans Peter Freyther <[email protected]>
Date: Sat, 12 Jun 2010 12:07:07 +0800
Subject: [PATCH 3/3] 2010-06-12  Holger Hans Peter Freyther  <[email protected]>

	* Sockets.st: Make DatagramSocket local: string port: int
	work. Resolve the ipAddress to localAddr early on, use the
	localAddr instead of ipAddress to determine the addressClass
	and afterwards fallback to localhost in case localAddr was nil.
---
 packages/sockets/ChangeLog  |    7 +++++++
 packages/sockets/Sockets.st |    9 ++++-----
 2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/packages/sockets/ChangeLog b/packages/sockets/ChangeLog
index ba4dd79..0637a8e 100644
--- a/packages/sockets/ChangeLog
+++ b/packages/sockets/ChangeLog
@@ -1,5 +1,12 @@
 2010-06-12  Holger Hans Peter Freyther  <[email protected]>
 
+	* Sockets.st: Make DatagramSocket local: string port: int
+	work. Resolve the ipAddress to localAddr early on, use the
+	localAddr instead of ipAddress to determine the addressClass
+	and afterwards fallback to localhost in case localAddr was nil.
+
+2010-06-12  Holger Hans Peter Freyther  <[email protected]>
+
 	* Sockets.st: Add AbstractSocket class>>#addressFromString to
 	parse a string into a SocketAddress and use it inside the
 	ServerSocket and DatagramSocket class.
diff --git a/packages/sockets/Sockets.st b/packages/sockets/Sockets.st
index 471f528..9dc892b 100644
--- a/packages/sockets/Sockets.st
+++ b/packages/sockets/Sockets.st
@@ -654,15 +654,14 @@ but it is done for cleanliness and symmetry.'>
 	<category: 'instance creation'>
 	| localAddr remoteAddr addressClass |
 	remoteAddr := self addressFromString: ipAddressOrString.
+	localAddr := self addressFromString: ipAddress.
 	addressClass := remoteAddr isNil 
 		    ifTrue: [self defaultAddressClass]
 		    ifFalse: [remoteAddr class].
-	addressClass := ipAddress isNil 
+	addressClass := localAddr isNil
 		    ifTrue: [addressClass]
-		    ifFalse: [ipAddress class].
-	localAddr := ipAddress isNil 
-		    ifTrue: [addressClass anyLocalAddress]
-		    ifFalse: [ipAddress].
+		    ifFalse: [localAddr class].
+	localAddr ifNil: [localAddr := addressClass anyLocalAddress].
 	^(addressClass newSocket: self)
 	    remote: remoteAddr
 	    port: remotePort
-- 
1.7.0.1

_______________________________________________
help-smalltalk mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/help-smalltalk

Reply via email to