![Page 2: Connectors Style: Main program and subroutines](https://reader036.vdocument.in/reader036/viewer/2022062520/56816380550346895dd4622f/html5/thumbnails/2.jpg)
2
Outline
• Some theory• Connector 1: Procedure call • Style: Main program and subroutines• Connector 2: Socket
![Page 3: Connectors Style: Main program and subroutines](https://reader036.vdocument.in/reader036/viewer/2022062520/56816380550346895dd4622f/html5/thumbnails/3.jpg)
3
SOME THEORY
![Page 4: Connectors Style: Main program and subroutines](https://reader036.vdocument.in/reader036/viewer/2022062520/56816380550346895dd4622f/html5/thumbnails/4.jpg)
4
Connector
• Architectural element in charge of the interactions among components
• Connector in implementations– No dedicated code – Scattered across modules
• Connector in architectures– Its own spec– To distinguish computations from interactions
![Page 5: Connectors Style: Main program and subroutines](https://reader036.vdocument.in/reader036/viewer/2022062520/56816380550346895dd4622f/html5/thumbnails/5.jpg)
5
Connectors
• Allow components to transfer control and data with each other
• Aka facilitating control flow and data flow
What is control?
![Page 6: Connectors Style: Main program and subroutines](https://reader036.vdocument.in/reader036/viewer/2022062520/56816380550346895dd4622f/html5/thumbnails/6.jpg)
6
Connectors
• “The guards at the gate of separation of concerns”
• Often domain-agnostic: the how, not the whatI protect that which matters most
![Page 7: Connectors Style: Main program and subroutines](https://reader036.vdocument.in/reader036/viewer/2022062520/56816380550346895dd4622f/html5/thumbnails/7.jpg)
7
Connectors
• Can be super smart and complex• E.g. order, filter, combine, or discard messages
I am a trafficker of information
![Page 8: Connectors Style: Main program and subroutines](https://reader036.vdocument.in/reader036/viewer/2022062520/56816380550346895dd4622f/html5/thumbnails/8.jpg)
8
4 interaction roles
• Communication– Transfer of data– Message passing
• Coordination– Transfer of control– Function calls
• Conversion– Translation– Wrapping
• Facilitation– Load-balancing– Locks
![Page 9: Connectors Style: Main program and subroutines](https://reader036.vdocument.in/reader036/viewer/2022062520/56816380550346895dd4622f/html5/thumbnails/9.jpg)
9
Connector examples vs concerns• Procedure call• Method call• Event broker• Pubsub• Interrupts• Socket• Load balancer• DB driver• SSH Tunnel• …
• Best effort vs exactly once vs reliable
• Encrypted vs cleartext• Uni/multi/broadcast• Static vs dynamic linkage• Serialization (JSON, XML,
binary)• Stateless vs stateful• Sync vs asynchronous• …
![Page 10: Connectors Style: Main program and subroutines](https://reader036.vdocument.in/reader036/viewer/2022062520/56816380550346895dd4622f/html5/thumbnails/10.jpg)
10
PROCEDURE CALL
![Page 11: Connectors Style: Main program and subroutines](https://reader036.vdocument.in/reader036/viewer/2022062520/56816380550346895dd4622f/html5/thumbnails/11.jpg)
11
Procedure call
• Communication role– Arguments, return values
• Coordination role– Control flow
• The most basic connector– “The Assembly language of sw interaction”
![Page 12: Connectors Style: Main program and subroutines](https://reader036.vdocument.in/reader036/viewer/2022062520/56816380550346895dd4622f/html5/thumbnails/12.jpg)
12
Examples
• Subroutines – Architectural style: Main program and subroutine
• Object-oriented methods• UNIX fork and exec• OS calls (open, read, write, poll, …)• Callbacks
• Basis for Remote Procedure Call
![Page 13: Connectors Style: Main program and subroutines](https://reader036.vdocument.in/reader036/viewer/2022062520/56816380550346895dd4622f/html5/thumbnails/13.jpg)
13
STYLE: MAIN PROGRAM AND SUBROUTINES
![Page 14: Connectors Style: Main program and subroutines](https://reader036.vdocument.in/reader036/viewer/2022062520/56816380550346895dd4622f/html5/thumbnails/14.jpg)
14
Style: Main program and subroutines
• Break down a long program into 1 main and N subroutines
• Subroutines– Self-contained
• No side-effects• Independent of each other
– Functionally meaningful and substantial• Good: detect_and_execute_collisions(me, enemies, walls)
returns True if the game is over• Bad: add_one_to_score(score) returns score+1
![Page 15: Connectors Style: Main program and subroutines](https://reader036.vdocument.in/reader036/viewer/2022062520/56816380550346895dd4622f/html5/thumbnails/15.jpg)
15
Style: Main program and subroutines
main
Subroutine 1 Subroutine 2 Subroutine N…
Function calls
![Page 16: Connectors Style: Main program and subroutines](https://reader036.vdocument.in/reader036/viewer/2022062520/56816380550346895dd4622f/html5/thumbnails/16.jpg)
16
Easy to follow the control flow
main
Subroutine 1 Subroutine 2 Subroutine N…
![Page 17: Connectors Style: Main program and subroutines](https://reader036.vdocument.in/reader036/viewer/2022062520/56816380550346895dd4622f/html5/thumbnails/17.jpg)
17
Style: Main program and subroutines
while keep_going:
keep_going, direction = process_input(direction) mybox = move(mybox, direction) if collide(mybox, borders): mybox, direction = create_box(dims) mybox, pellets = eat_colliding_pellet(mybox, pellets, dims) draw_everything(screen, mybox, pellets, borders) clock.tick(50)
![Page 18: Connectors Style: Main program and subroutines](https://reader036.vdocument.in/reader036/viewer/2022062520/56816380550346895dd4622f/html5/thumbnails/18.jpg)
18
Style: Main program and subroutines
game loop
process_input move draw_everything…
Function calls
Only these functions involve Pygame
![Page 19: Connectors Style: Main program and subroutines](https://reader036.vdocument.in/reader036/viewer/2022062520/56816380550346895dd4622f/html5/thumbnails/19.jpg)
19
Same loop, but replace Pygame display
![Page 20: Connectors Style: Main program and subroutines](https://reader036.vdocument.in/reader036/viewer/2022062520/56816380550346895dd4622f/html5/thumbnails/20.jpg)
20
IP SOCKETS
![Page 21: Connectors Style: Main program and subroutines](https://reader036.vdocument.in/reader036/viewer/2022062520/56816380550346895dd4622f/html5/thumbnails/21.jpg)
21
Socket
• Endpoint of inter-process communication across the network
• Most follow Internet Protocol (IP)• BSD standard IP socket API– Constructor, bind, listen, connect, accept, send, recv, close,
setsockopt, …– poll, select, epoll, or kqueue to know the socket state (is
there data to recv? The other end hung up?)• Client-server paradigm
![Page 22: Connectors Style: Main program and subroutines](https://reader036.vdocument.in/reader036/viewer/2022062520/56816380550346895dd4622f/html5/thumbnails/22.jpg)
22
Client-server paradigm
• Server socket bind() and listen()• Server poll() its sockets periodically• Client socket connect()• Client poll() its socket periodically• Server socket accept()– Creates a new socket for that client connection
• Both sockets send() and recv()• One socket close()• The other’s poll() will notify the closure
![Page 23: Connectors Style: Main program and subroutines](https://reader036.vdocument.in/reader036/viewer/2022062520/56816380550346895dd4622f/html5/thumbnails/23.jpg)
23
Client-server
• The client contacts the server• The server can’t pull a client in!– Neo must sit to connect
![Page 24: Connectors Style: Main program and subroutines](https://reader036.vdocument.in/reader036/viewer/2022062520/56816380550346895dd4622f/html5/thumbnails/24.jpg)
24
Sequence diagram
![Page 25: Connectors Style: Main program and subroutines](https://reader036.vdocument.in/reader036/viewer/2022062520/56816380550346895dd4622f/html5/thumbnails/25.jpg)
25
IP sockets in Python
• http://docs.python.org/2/howto/sockets.html• http://docs.python.org/2/library/socket.html