Guest User

Untitled

a guest
Dec 10th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. class server:
  2.  
  3. # list of all TCP connections
  4. tcp_connections = list<tcp_connection>
  5.  
  6. # one tcp_connection have limited count of connections
  7. struct tcp_connection{
  8. list<connection> connections
  9. }
  10.  
  11. # status of logical connection
  12. enum status{
  13. OFFLINE, # client was disconnected, or never connected
  14. ONLINE # client connected and found link with connection_id
  15. }
  16.  
  17. # logical connection inside tcp connection linking two tox clients
  18. struct connection {
  19. # id of logical connection that returns to client
  20. uint id, # id from 16 to 255
  21. # PK's of tox clients
  22. list<Pair<PK, status>> clients
  23. }
  24.  
  25. uint get_free_index(tcp_connection tcp){
  26.  
  27. # find new unique index not placed in connection list
  28. # it can be simple loop of all possible values with checking if this index in list:
  29. # tcp.connections.contains(id)
  30.  
  31. }
  32.  
  33. connection create_new_connection(tcp_connection tcp, PK from, PK dest){
  34.  
  35. # when creating new link (logical connection) we set that requesting client is online
  36. # and requested client is pending - offline
  37. connection new_connection = new connection()
  38. .id(get_free_index())
  39. .clients{
  40. .add(from, status.ONLINE)
  41. .add(dest, status.OFFLINE)
  42. };
  43.  
  44. # put new logical connection to list
  45. tcp.connections.add(new_connection)
  46.  
  47. return new_connection
  48.  
  49. }
  50.  
  51. # when client connected he calls this to get a connection_id for requested PK to talk with
  52. connection get_connection(tcp_connection tcp, PK from, PK dest){
  53.  
  54. # search exisiting connection in all connections
  55. existing_connection = tcp.connections.filter( connect -> connect.clients.contains(from, dest) )
  56. if existing_connection{
  57. # if we found some pending connection
  58. # change status of existing connection from to online
  59. existing_connection.clients
  60. .filter(key, current_status -> key == from && current_status == status.OFFLINE )
  61. = new Pair<>(from, status.ONLINE)
  62. # and then create our connection
  63. # with dest ONLINE because we know that here is online pending connection with dest PK
  64. new_connection = create_new_connection().clients.filter(key, current_status -> key == dest).status
  65. = new Pair<>(dest, status.ONLINE)
  66. # and push it into the current tcp_connection
  67. return new_connection
  68. }
  69.  
  70. # if here is no existing connections create new one with pending dest
  71. return create_new_connection(from, dest)
  72.  
  73. }
  74.  
  75. boolean is_connection_online(connection connect){
  76. # if here is more then one online client connection is online
  77. return connect.clients.count(key, current_status -> current_status == status.ONLINE) > 1
  78. }
Add Comment
Please, Sign In to add comment