mirror of
https://github.com/EiffelSoftware/eiffel-org.git
synced 2025-12-08 07:42:33 +01:00
Changed assignment attempt usage to object test. Used /examples/net/same_mach as guide.
Author:halw Date:2012-04-07T15:46:24.000000Z git-svn-id: https://svn.eiffel.com/eiffel-org/trunk@1069 abb3cda0-5349-4a8f-a601-0c33ac3a8c38
This commit is contained in:
@@ -44,11 +44,17 @@ Whenever the server needs to exchange objects with one of the clients, it obtain
|
|||||||
|
|
||||||
Procedure <eiffel>accept</eiffel> ensures synchronization with the client. When communication is established, <eiffel>accept</eiffel> creates a new socket which will be accessible through attribute <eiffel>accepted</eiffel>, whose value is here assigned to the local entity <eiffel>soc2</eiffel>. To receive objects, the server will use operations of the form introduced earlier ( [[An overview of EiffelNet Mechanisms|An overview of EiffelNet, sending and receiving object structures]] ):
|
Procedure <eiffel>accept</eiffel> ensures synchronization with the client. When communication is established, <eiffel>accept</eiffel> creates a new socket which will be accessible through attribute <eiffel>accepted</eiffel>, whose value is here assigned to the local entity <eiffel>soc2</eiffel>. To receive objects, the server will use operations of the form introduced earlier ( [[An overview of EiffelNet Mechanisms|An overview of EiffelNet, sending and receiving object structures]] ):
|
||||||
<code>
|
<code>
|
||||||
struct ?= soc2.retrieved</code>
|
if attached {SOME_EXPECTED_TYPE} soc2.retrieved as l_temp then
|
||||||
|
-- soc2.retrieved was attached to an object of the expected type. Now that object is attached to `l_temp'
|
||||||
|
-- Proceed with normal computation, typically involving calls of the form l_temp.some_feature
|
||||||
|
else
|
||||||
|
-- soc2.retrieved was not attached to an object of the expected type.
|
||||||
|
end
|
||||||
|
</code>
|
||||||
|
|
||||||
applying to <eiffel>soc2</eiffel>, not <eiffel>soc1</eiffel>; this makes <eiffel>soc1</eiffel> available to accept connections with other clients, a fundamental feature of client-server mechanisms.
|
applying to <eiffel>soc2</eiffel>, not <eiffel>soc1</eiffel>; this makes <eiffel>soc1</eiffel> available to accept connections with other clients, a fundamental feature of client-server mechanisms.
|
||||||
|
|
||||||
The operation <eiffel>soc2</eiffel>. <eiffel>close</eiffel> which terminates the above sequence closes the new socket. In principle this is not necessary, since garbage collection should eventually reclaim the socket object, and the <eiffel>dispose</eiffel> procedure of the corresponding socket class includes a call to <eiffel>close</eiffel>. But the risk exists that you run out of sockets before garbage collection reclaims all currently opened sockets, so it is preferable to include the <eiffel>close</eiffel> calls explicitly.
|
The operation <eiffel>soc2.close</eiffel> which terminates the above sequence closes the new socket. In principle this is not necessary, since garbage collection should eventually reclaim the socket object, and the <eiffel>dispose</eiffel> procedure of the corresponding socket class includes a call to <eiffel>close</eiffel>. But the risk exists that you run out of sockets before garbage collection reclaims all currently opened sockets, so it is preferable to include the <eiffel>close</eiffel> calls explicitly.
|
||||||
|
|
||||||
At the end of the processing it is necessary to close the original socket <eiffel>soc1</eiffel> but also to unlink it. The feature <eiffel>cleanup</eiffel> from class <eiffel>SOCKET</eiffel> takes care of both closing and unlinking.
|
At the end of the processing it is necessary to close the original socket <eiffel>soc1</eiffel> but also to unlink it. The feature <eiffel>cleanup</eiffel> from class <eiffel>SOCKET</eiffel> takes care of both closing and unlinking.
|
||||||
|
|
||||||
@@ -67,12 +73,11 @@ creation
|
|||||||
|
|
||||||
feature
|
feature
|
||||||
|
|
||||||
soc1, soc2: UNIX_STREAM_SOCKET
|
|
||||||
|
|
||||||
make
|
make
|
||||||
-- Accept communication with client and exchange messages
|
-- Accept communication with client and exchange messages
|
||||||
local
|
local
|
||||||
count: INTEGER
|
count: INTEGER
|
||||||
|
soc1: detachable UNIX_STREAM_SOCKET
|
||||||
do
|
do
|
||||||
create soc1.make_server ("/tmp/here")
|
create soc1.make_server ("/tmp/here")
|
||||||
from
|
from
|
||||||
@@ -81,35 +86,37 @@ feature
|
|||||||
until
|
until
|
||||||
count = 3
|
count = 3
|
||||||
loop
|
loop
|
||||||
process -- See below
|
process (soc1) -- See below
|
||||||
count := count + 1
|
count := count + 1
|
||||||
end
|
end
|
||||||
soc1.cleanup
|
soc1.cleanup
|
||||||
rescue
|
rescue
|
||||||
|
if soc1 /= Void then
|
||||||
soc1.cleanup
|
soc1.cleanup
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
process
|
process (soc1: UNIX_STREAM_SOCKET)
|
||||||
-- Receive a message, extend it, and send it back
|
-- Receive a message, extend it, and send it back
|
||||||
local
|
|
||||||
our_new_list: OUR_MESSAGE
|
|
||||||
do
|
do
|
||||||
soc1.accept
|
soc1.accept
|
||||||
soc2 ?= soc1.accepted
|
if attached soc1.accepted as l_soc2 then
|
||||||
our_new_list ?= retrieved (soc2)
|
if attached {OUR_MESSAGE} retrieved (l_soc2) as l_our_new_list then
|
||||||
from
|
from
|
||||||
our_new_list.start
|
l_our_new_list.start
|
||||||
until
|
until
|
||||||
our_new_list.after
|
l_our_new_list.after
|
||||||
loop
|
loop
|
||||||
io.putstring (our_new_list.item)
|
io.putstring (l_our_new_list.item)
|
||||||
io.new_line
|
io.new_line
|
||||||
our_new_list.forth
|
l_our_new_list.forth
|
||||||
|
end
|
||||||
|
l_our_new_list.extend ("%N I'm back.%N")
|
||||||
|
l_our_new_list.independent_store (l_soc2)
|
||||||
end
|
end
|
||||||
our_new_list.extend ("%N I'm back.%N")
|
|
||||||
our_new_list.independent_store (soc2)
|
|
||||||
soc2.close
|
soc2.close
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
</code>
|
</code>
|
||||||
@@ -129,40 +136,45 @@ creation
|
|||||||
|
|
||||||
feature
|
feature
|
||||||
|
|
||||||
soc1: UNIX_STREAM_SOCKET
|
|
||||||
|
|
||||||
make
|
make
|
||||||
-- Establish communication with server, and exchange messages
|
-- Establish communication with server, and exchange messages
|
||||||
|
local
|
||||||
|
soc1: detachable UNIX_STREAM_SOCKET
|
||||||
do
|
do
|
||||||
create soc1.make_client ("/tmp/here")
|
create soc1.make_client ("/tmp/here")
|
||||||
soc1.connect
|
soc1.connect
|
||||||
process -- See below
|
process (soc1) -- See below
|
||||||
soc1.cleanup
|
soc1.cleanup
|
||||||
rescue
|
rescue
|
||||||
|
if soc1 /= Void then
|
||||||
soc1.cleanup
|
soc1.cleanup
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
process
|
process (soc1: UNIX_STREAM_SOCKET)
|
||||||
-- Build a message to server, receive answer, build
|
-- Build a message to server, receive answer, build
|
||||||
-- modified message from that answer, and print it.
|
-- modified message from that answer, and print it.
|
||||||
local
|
local
|
||||||
our_list, our_new_list: OUR_MESSAGE
|
our_list: OUR_MESSAGE
|
||||||
do
|
do
|
||||||
create our_list.make
|
create our_list.make
|
||||||
our_list.extend("This"); our_list.extend (" is")
|
our_list.extend("This")
|
||||||
our_list.extend (" our"); our_list.extend (" test")
|
our_list.extend (" is")
|
||||||
|
our_list.extend (" our")
|
||||||
|
our_list.extend (" test")
|
||||||
our_list.independent_store (soc1)
|
our_list.independent_store (soc1)
|
||||||
our_new_list ?= soc1.retrieved
|
if attached {OUR_MESSAGE} our_list.retrieved (soc1) as l_our_new_list then
|
||||||
from
|
from
|
||||||
our_new_list.start
|
l_our_new_list.start
|
||||||
until
|
until
|
||||||
our_new_list.after
|
l_our_new_list.after
|
||||||
loop
|
loop
|
||||||
io.putstring (our_new_list.item)
|
io.putstring (l_our_new_list.item)
|
||||||
our_new_list.forth
|
l_our_new_list.forth
|
||||||
end
|
end
|
||||||
io.new_line
|
io.new_line
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
</code>
|
</code>
|
||||||
|
|||||||
Reference in New Issue
Block a user