peers.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from sydent.replication.peer import RemotePeer
  16. class PeerStore:
  17. def __init__(self, sydent):
  18. self.sydent = sydent
  19. def getPeerByName(self, name):
  20. cur = self.sydent.db.cursor()
  21. res = cur.execute("select p.name, p.port, "
  22. "p.lastSentAssocsId, p.lastSentInviteTokensId, p.lastSentEphemeralKeysId, "
  23. "p.shadow, pk.alg, pk.key from peers p, peer_pubkeys pk "
  24. "where p.name = ? and pk.peername = p.name and p.active = 1", (name,))
  25. serverName = None
  26. port = None
  27. lastSentAssocsId = None
  28. lastSentInviteTokensId = None
  29. lastSentEphemeralKeysId = None
  30. pubkeys = {}
  31. for row in res.fetchall():
  32. serverName = row[0]
  33. port = row[1]
  34. lastSentAssocsId = row[2]
  35. lastSentInviteTokensId = row[3]
  36. lastSentEphemeralKeysId = row[4]
  37. shadow = row[5]
  38. pubkeys[row[6]] = row[7]
  39. if len(pubkeys) == 0:
  40. return None
  41. p = RemotePeer(self.sydent, serverName, pubkeys)
  42. p.lastSentAssocsId = lastSentAssocsId
  43. p.lastSentInviteTokensId = lastSentInviteTokensId
  44. p.lastSentEphemeralKeysId = lastSentEphemeralKeysId
  45. p.shadow = True if shadow else False
  46. if port:
  47. p.port = port
  48. return p
  49. def getAllPeers(self):
  50. cur = self.sydent.db.cursor()
  51. res = cur.execute("select p.name, p.port, "
  52. "p.lastSentAssocsId, p.lastSentInviteTokensId, p.lastSentEphemeralKeysId, "
  53. "p.shadow, pk.alg, pk.key from peers p, peer_pubkeys pk "
  54. "where pk.peername = p.name and p.active = 1")
  55. peers = []
  56. peername = None
  57. port = None
  58. lastSentAssocsId = 0
  59. lastSentInviteTokensId = 0
  60. lastSentEphemeralKeysId = 0
  61. pubkeys = {}
  62. for row in res.fetchall():
  63. if row[0] != peername:
  64. if len(pubkeys) > 0:
  65. p = RemotePeer(self.sydent, peername, pubkeys)
  66. p.lastSentAssocsId = lastSentAssocsId
  67. p.lastSentInviteTokensId = lastSentInviteTokensId
  68. p.lastSentEphemeralKeysId = lastSentEphemeralKeysId
  69. if port:
  70. p.port = port
  71. peers.append(p)
  72. pubkeys = {}
  73. peername = row[0]
  74. port = row[1]
  75. lastSentAssocsId = row[2]
  76. lastSentInviteTokensId = row[3]
  77. lastSentEphemeralKeysId = row[4]
  78. shadow = row[5]
  79. pubkeys[row[6]] = row[7]
  80. if len(pubkeys) > 0:
  81. p = RemotePeer(self.sydent, peername, pubkeys)
  82. p.lastSentAssocsId = lastSentAssocsId
  83. p.lastSentInviteTokensId = lastSentInviteTokensId
  84. p.lastSentEphemeralKeysId = lastSentEphemeralKeysId
  85. p.shadow = True if shadow else False
  86. if port:
  87. p.port = port
  88. peers.append(p)
  89. pubkeys = {}
  90. return peers
  91. def setLastSentIdAndPokeSucceeded(self, peerName, ids, lastPokeSucceeded):
  92. """Set last successful replication of data to this peer.
  93. If an id for a replicated database table is None, the last sent value
  94. will not be updated.
  95. :param peerName: The name of the peer.
  96. :type peerName: str
  97. :param ids: A Dictionary of ids that represent the last database
  98. table ids that were replicated to this peer.
  99. :type ids: Dict[str, int]
  100. :param lastPokeSucceeded: The time of when the last successful
  101. replication succeeded (even if no actual replication of data was
  102. necessary).
  103. :type lastPokeSucceeded: int
  104. """
  105. cur = self.sydent.db.cursor()
  106. if ids["sg_assocs"]:
  107. cur.execute("update peers set lastSentAssocsId = ?, lastPokeSucceededAt = ? "
  108. "where name = ?", (ids["sg_assocs"], lastPokeSucceeded, peerName))
  109. if ids["invite_tokens"]:
  110. cur.execute("update peers set lastSentInviteTokensId = ?, lastPokeSucceededAt = ? "
  111. "where name = ?", (ids["invite_tokens"], lastPokeSucceeded, peerName))
  112. if ids["ephemeral_public_keys"]:
  113. cur.execute("update peers set lastSentEphemeralKeysId = ?, lastPokeSucceededAt = ? "
  114. "where name = ?", (ids["ephemeral_public_keys"], lastPokeSucceeded, peerName))
  115. self.sydent.db.commit()