Browse Source

feat(prepa): add pseudo-code for v1-v4

Paul Alnet 7 months ago
parent
commit
0b699be672
1 changed files with 132 additions and 0 deletions
  1. 132
    0
      TP-AIPS-2024-ALNET-Paul-preparation-v1-v4.md

+ 132
- 0
TP-AIPS-2024-ALNET-Paul-preparation-v1-v4.md View File

@@ -0,0 +1,132 @@
1
+Use `shift + K` in vim/nvim to open the man page of function undor cursor.
2
+
3
+# tsock_v1 pseudo-code : UDP only
4
+
5
+```
6
+parse and validate arguments.
7
+fail if requests TCP
8
+
9
+if source:
10
+  build local socket -> socket()
11
+  build and resolve destination address -> gethostbyname()
12
+  for loop:
13
+      construire_message()
14
+      send datagrams -> sendto()
15
+  exit()
16
+else:    # puit
17
+  build local socket -> socket()
18
+  build address -> INADDR_ANY
19
+  bind socket to address -> bind()
20
+  while True:   # receive unlimited messages
21
+    receive datagram -> recvfrom()
22
+    afficher_message()
23
+```
24
+  
25
+# tsock_v2 pseudo-code : UDP/TCP
26
+
27
+```
28
+parse and validate arguments.
29
+
30
+if source:
31
+  build local socket -> socket()
32
+  build and resolve destination address -> gethostbyname()
33
+  if TCP:
34
+    open connection to receiver -> connect()
35
+    for loop:
36
+      construire_message()
37
+      send message -> send()
38
+    close connection -> close()
39
+  else:  # UDP
40
+    same as in v1
41
+  exit()
42
+else:    # puit
43
+  build local socket -> socket()
44
+  build address -> INADDR_ANY
45
+  bind socket to address -> bind()
46
+  if TCP :
47
+    set incoming connection queue size -> listen()
48
+    while True:   # receive unlimited connections (one at a time)
49
+      accept an incoming connection -> accept()
50
+      while connection open and new message:
51
+        read incoming stream -> read()
52
+        afficher_message()
53
+      # connection closed by emitter
54
+  else :
55
+    same as in v1
56
+```
57
+
58
+**EDIT** : Turns out v3/v4 pseudocode isn't required. For your viewing pleasure.
59
+
60
+# tsock_v3 pseudo-code : UDP/TCP with custom length & number of messages
61
+
62
+Pretty much the same as tsock_v2 but buffer sizes are dynamically allocated (implies changes
63
+in parameters to read/write functions). Also changes the number of iterations in the for loops.
64
+
65
+# tsock_v4 pseudo-code : Standalone server
66
+
67
+UDP implies on the client listener side sending a hello packet to server to request data. how to know last packet ?
68
+
69
+TODO
70
+
71
+```
72
+parse and validate arguments.
73
+
74
+void send_messages():
75
+  for loop:
76
+    construire_message()
77
+    send message -> send()
78
+
79
+if client:
80
+  build local socket -> socket()
81
+  build and resolve destination address -> gethostbyname()
82
+  if TCP:
83
+    open connection to server -> connect()
84
+    if emitter:
85
+      send_messages()
86
+      close()
87
+    else:  # receiver
88
+      while connection open:
89
+        read()
90
+        afficher_message()
91
+  if UDP:
92
+    if emitter:
93
+      send_messages()
94
+    else:
95
+      send datagram to request data -> send()
96
+      while True: # exit condition ??
97
+        read()
98
+        afficher_message()
99
+
100
+else:  # server
101
+  build local socket -> socket()
102
+  build address -> INADDR_ANY
103
+  bind socket to address -> bind()
104
+  if TCP:
105
+    while True:
106
+      accept() produces new connection socket
107
+      fork() 
108
+      close(listening socket) on child
109
+      close(new connection socket) on parent
110
+      # within child process
111
+      if emitter:
112
+        send_messages()
113
+        close()
114
+      else:  # receiver
115
+        while connection open:
116
+          read()
117
+          afficher_message()
118
+      exit() on child
119
+  else:  # UDP
120
+    if emitter:
121
+      while read():  # receive request datagram
122
+        fork()
123
+        # on child
124
+        build socket if required
125
+        send_messages()
126
+    else:  # receiver
127
+      while True:
128
+        read()
129
+        afficher_message()
130
+```
131
+
132
+When as a TCP server, we may want to reap the dead children processes using waitpid() and errno.

Loading…
Cancel
Save