Pharo Networking by Example

44
Networking by Example Noury Bouraqadi http://car.mines-douai.fr/noury "Deep Into Smalltalk" Spring School 8 march 2011 - Lille, France

description

Invited Speaker at the first "Deep into Smalltalk" spring school. 7-11 march 2011. The pdf of this slides is also available onlie at: http://car.mines-douai.fr/noury/docs/deepIntoSmalltalk2011

Transcript of Pharo Networking by Example

Page 1: Pharo Networking by Example

Networking by Example!

Noury Bouraqadi http://car.mines-douai.fr/noury

"Deep Into Smalltalk" Spring School 8 march 2011 - Lille, France

Page 2: Pharo Networking by Example

Agenda •  Networking Basics

– Sockets and protocols – Client vs. Server – Hands-on with SocketStream

•  Serving – Connection vs. communication – Hands-on Concurrency!

•  Complex interactions – Exchanging objects over a network – Remote messaging Hands-on

2

Page 3: Pharo Networking by Example

3

Socket

NeworkSoftware

Process

Software

ProcessSocket

Bi-directional communication

Page 4: Pharo Networking by Example

4

Socket

NeworkSoftware

Process

Software

ProcessSocket

Device A Device B

Page 5: Pharo Networking by Example

5

Socket

NeworkSoftware

Process

Software

ProcessSocket

Device Z

Page 6: Pharo Networking by Example

6

2 Main Transport Protocols

TCP UDP

Transmission Control Protocol

User Datagram Protocol

Page 7: Pharo Networking by Example

7

TCP UDP

•  Connected •  Reliable •  Streams

•  Connection free •  Unreliable

•  Limited size

2 Main Transport Protocols

Page 8: Pharo Networking by Example

8

TCP

HTTP

Focus

FTP

SMTP POP

SSH

Page 9: Pharo Networking by Example

9

Server

Connection Handling Process

Client A

Communication Process

Page 10: Pharo Networking by Example

10

Server

Connection Handling Process

Client A

Communication Process

Communication Process

Page 11: Pharo Networking by Example

11

Server

Connection Handling Process

Client A

Communication Process

Communication Process

Client B

Communication Process

Page 12: Pharo Networking by Example

12

Server

Connection Handling Process

Communication Process

Client A

Communication Process

Communication Process

Client B

Communication Process

Page 13: Pharo Networking by Example

13

Server

Connection Handling Process

Communication Process

Client A

Communication Process

Communication Process

Client B

Communication Process

Client C

Communication Process

Page 14: Pharo Networking by Example

14

Server

Connection Handling Process

Communication Process

Communication Process

Client A

Communication Process

Communication Process

Client B

Communication Process

Client C

Communication Process

Page 15: Pharo Networking by Example

15

Client Socket �

1.   Connect to a server�2.  Send a String �3.   Receive a String �4.  Close�

SocketStream !

Page 16: Pharo Networking by Example

Server for Tests 16

echo "Smalltalk" | nc –lk 12345 �

Port number�

Multiple�connections �

Unix!Serve�

Page 17: Pharo Networking by Example

17

|stream| stream := SocketStream

openConnectionToHostNamed: 'localhost' port: 12345.

[ stream sendCommand: 'Pharo’. Transcript cr; show: (stream nextLineLf).

] ensure: [ stream close]

Page 18: Pharo Networking by Example

18

Simplest Possible Server�1.   Listen on some port �2.  Accept 1 single client connection�3.   Send a String �4.  Receive a String �5.  Close�

Page 19: Pharo Networking by Example

Client for Tests 19

echo "Smalltalk" | nc localhost 12345 �

Port number�

Unix!Host �

Page 20: Pharo Networking by Example

20

connectionSock := Socket newTCP. [ connectionSock listenOn: 12345 backlogSize: 10. interactSock := connectionSock

waitForAcceptFor: 30. stream := SocketStream on: interactSock. stream sendCommand: 'Pharo Server!’. Transcript cr; show: stream nextLineLf. ] ensure: [ connectionSock closeAndDestroy. stream close.]

Page 21: Pharo Networking by Example

21

Luc: I’m struggling with the robots ---------- Noury: Which robots? WifiBots or Robulabs? ---------- Luc: I need to setup the internal board of Robulab #2 ----------

Twitter�like �

Page 22: Pharo Networking by Example

22

Multi-threaded Server�

1 process�for connections �

1 process�for each client �

Sycnrhonization �is needed�

Page 23: Pharo Networking by Example

23

Multi-threaded Server�

1 process�for connections �

1 process�for each client �

Sycnrhonization �is needed�

fork�

Mutex �

critical: �

Page 24: Pharo Networking by Example

24

Socket �1 �

Socket �2 �

10110101 �Network

Page 25: Pharo Networking by Example

Copying an object ! 25

Socket �1 �

Socket �2 �

10110101 �Network

1011

10@34 � 10@34 �

110101

Page 26: Pharo Networking by Example

26

ReferenceStream�

streamedRepresentationOf: 10@34

Page 27: Pharo Networking by Example

Classes should be �on both sides �

27

Page 28: Pharo Networking by Example

Remote messaging 28

Network

Page 29: Pharo Networking by Example

29

proxy�

Socket �1 �

Socket �2 �10110101 �

dispatcher�

Network

Page 30: Pharo Networking by Example

30

proxy�id1 �

Socket �1 �

Socket �2 �10110101 �

dispatcher�

id1 �

Network

id3 �

Page 31: Pharo Networking by Example

31

Remote �Transcript �

Page 32: Pharo Networking by Example

32

Proxy�

Message�passing control�(De-)Serializing �

Messages�

Garbage �Collection?�

Argument passing �

by reference�

Code�Deployment �

Page 33: Pharo Networking by Example

33

Proxy�

Message�passing control�

doesNotUnderstand: �

Page 34: Pharo Networking by Example

34

(De-)Serializing �Messages�

ReferenceStream�

streamedRepresentationOf: anObject �

unStream: aString�

Page 35: Pharo Networking by Example

35

Distributed-Objects (rST)�

OCEAN �New Socket �

Library �

What Next?

Page 36: Pharo Networking by Example

a Clean, Portable Networking Library

Page 37: Pharo Networking by Example

Socket

HTTPSocket SocksSocket

SocketAdress

ByteArray TCP+

UDP+… IPv4

+IPv6 IPv4

+IPv6

Current network library

0% tested

Dirty!

Page 38: Pharo Networking by Example

TcpSocket UdpSocket

NetworkLibraryProxyFactory

WindowsLibrary

LinuxLibrary

SocketnetworkLibrarysocketID

MacOSLibrary

NetworkingPluginWrapperLibrary

OCEAN Library

OOP 100% tested

Page 39: Pharo Networking by Example

TcpSocket UdpSocket

NetworkLibraryProxyFactory

WindowsLibrary

LinuxLibrary

SocketnetworkLibrarysocketID

MacOSLibrary

NetworkingPluginWrapperLibrary

OCEAN Library

Pharo 1.3

Latest

version

OOP 100% tested

Page 40: Pharo Networking by Example

VM

Image

OS

Plugin Wrapper

Network API

OCEAN for Pharo 1.3

Socket Plugin

Socket Library

ALL Primitives !

Page 41: Pharo Networking by Example

VM

Image

OS

Posix Library

Wrapper

Posix Library

OCEAN for Pharo 1.4?

FFI Plugin

Socket Library

OOP Smalltalk

IDE

Page 42: Pharo Networking by Example

OCEAN + FFI + Posix

0

50

100

150

200

250

300

350

Receive 10MB Send 10MB

ms Ocean

OldSocket

Page 43: Pharo Networking by Example

VM

Image

Other OS

Library A

Library B

Library Z

API α API β API ψ

Generalization

FFI Smaller VM

Page 44: Pharo Networking by Example

44