Networking by Example � Noury Bouraqadi http://car.mines-douai.fr/noury "Deep Into Smalltalk" Spring School 8 march 2011 - Lille, France
2 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
3 Software Software Nework Process Process Socket Socket Bi-directional communication
4 Device B Device A Software Software Nework Process Process Socket Socket
5 Device Z Software Software Nework Process Process Socket Socket
6 2 Main Transport Protocols TCP UDP Transmission User Control Datagram Protocol Protocol
7 2 Main Transport Protocols TCP UDP • Connected • Connection free • Reliable • Unreliable • Streams • Limited size
8 Focus SSH POP TCP SMTP HTTP FTP
9 Connection Handling Process Server Client A Communication Process
10 Connection Handling Process Server Client A Communication Communication Process Process
11 Connection Handling Process Server Client B Communication Process Client A Communication Communication Process Process
12 Connection Handling Process Server Client B Communication Communication Process Process Client A Communication Communication Process Process
13 Connection Handling Client C Process Communication Process Server Client B Communication Communication Process Process Client A Communication Communication Process Process
14 Connection Handling Client C Process Communication Communication Process Process Server Client B Communication Communication Process Process Client A Communication Communication Process Process
15 Clien ent Socket � m m � � a a e e r r t t S S t t e e k k c c o o S S 1. 1. Co Connect t to a se o a server � 2. Sen 2. end a S a Str tring � 3. Recei 3. eive a S a Str tring � 4. 4. Close �
16 Server for Tests Serve � Unix � Unix echo " ec "Sma malltalk" " | nc nc – –lk lk 1 1234 2345 � Multiple � Port Po co connections � numb mber �
17 |stream| stream := SocketStream openConnectionToHostNamed: 'localhost' port: 12345. [ stream sendCommand: 'Pharo’. Transcript cr; show: (stream nextLineLf). ] ensure: [ stream close]
18 Simp mplest Possible e Server � 1. 1. Listen en o on n so some p port rt � 2. 2. Ac Accept 1 si 1 singl gle clien ent co connection � 3. 3. Sen end a S a Str tring � 4. 4. Recei eive a S a Str tring � 5. 5. Close �
19 Client for Tests Ho Host � Unix � Unix echo " ec "Sma malltalk" " | nc nc lo localhost 1 1234 2345 � Po Port numb mber �
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.]
21 Tw Twit Luc: I’m struggling with the robots itter � ---------- Noury: Which robots? WifiBots or Robulabs? li like � ---------- Luc: I need to setup the internal board of Robulab #2 ----------
22 Multi-threa eaded Se Server � 1 1 pr proce cess � fo for co connections � 1 1 pr proce cess � fo for ea each clien ent � Sy Sycnrho honiza izatio ion � is is nee eeded �
23 fo fork � Multi-threa eaded Se Server � 1 1 pr proce cess � for co fo connections � 1 1 pr proce cess � Mutex � Mu for ea fo each clien ent � Sycnrho Sy honiza izatio ion � is is nee eeded � cr critical: �
24 10110101 10110101 � So Socket � So Socket � 1 � 2 � Network
25 Copying an object ! 10@34 10@ 34 � 10@34 10@ 34 � 110101 110101 1011 1011 10110101 � 10110101 Socket � So Socket � So 1 � 2 � Network
26 Referen enceS eStrea eam � s t r e a m e d R e p r e s e n t a t i o n O f : 1 0 @ 3 4
27 Cl Classe sses s sho should ld be be � on on bo both th si side des �
28 Remote messaging Network
29 pr proxy � dispa dispatche her � So Socket � So Socket � 10110101 10110101 � 1 � 2 � Network
30 id id3 � id1 � id proxy � pr dispa dispatche her � id id1 � So Socket � So Socket � 10110101 � 10110101 1 � 2 � Network
31 Remo emote � Tr Transcript �
32 Code � Deploymen ent � Pr Prox oxy � Message � (De- e-)Serializing � passi pa ssing c g contr trol ol � Me Messa ssages � Ga Garbage � Argumen ent Collectio Co ion? � pa passi ssing g � by by referen ence �
33 Pr Prox oxy � Message � pa passi ssing c g contr trol ol � do doesN sNotUnde derstan and: �
34 Referen enceS eStrea eam � unStrea eam: : aSt aStrin ing � (De- e-)Serializing � Messa Me ssages � strea eamedRepresen entationOf Of: : anOb Object �
35 OC OCEA EAN � What New ew Socket � Next? Li Libr brary � Di Distr tributed- Ob Objects (rST rST) �
a Clean, Portable Networking Library
Current network library ByteArray IPv4 IPv4 TCP+ +IPv6 +IPv6 UDP+… Socket SocketAdress Dirty! HTTPSocket SocksSocket 0% tested
OCEAN Library Socket networkLibrary NetworkLibraryProxyFactory socketID WindowsLibrary MacOSLibrary TcpSocket UdpSocket LinuxLibrary % 0 OOP 0 1 d e NetworkingPlugin t s e t WrapperLibrary
Latest OCEAN Library version Socket networkLibrary NetworkLibraryProxyFactory socketID WindowsLibrary MacOSLibrary TcpSocket UdpSocket LinuxLibrary o r a h P % 0 OOP 0 1 3 . 1 d e NetworkingPlugin t s e t WrapperLibrary
OCEAN for Pharo 1.3 ALL Primitives ! Socket Plugin Image Library Wrapper Socket VM Plugin OS Network API
OCEAN for Pharo 1.4? Posix Socket Image Library Library Wrapper Smalltalk FFI VM OOP IDE Plugin OS Posix Library
OCEAN + FFI + Posix 350 300 250 200 Ocean ms OldSocket 150 100 50 0 Receive 10MB Send 10MB
Generalization Library Library Library Image A B Z Smaller FFI VM VM Other OS API α API β API ψ
44
Recommend
More recommend