Ticket #1281: chat.diff

File chat.diff, 4.8 KB (added by warner, at 2010-12-25T00:45:30Z)

zooko and I threw some code together at pycon2009, might be a starting point

  • docs/configuration.txt

    diff -rN -u old-irc/docs/configuration.txt new-irc/docs/configuration.txt
    old new  
    279279 larger than N.
    280280
    281281
     282== Chat Service Configuration ==
     283
     284[chat]
     285enabled = (boolean, optional)
     286
     287 Tahoe client nodes can particpate in a simple IRC-style chat service. When
     288 enabled, each node's Welcome page will show a transcript of recent messages,
     289 and a panel to send messages to the group. The default is False.
     290
     291publish_activity = (boolean, optional)
     292
     293 If this is set to True, then a summary of local node activity will be
     294 published to the chat channel. This means all file upload and download
     295 operations will be announced, with a rough filesize and abbreviated storage
     296 index string. It also includes directory reads/writes. No confidential
     297 information will be transmitted (i.e. no readcaps). The intent is to
     298 increase the visibility of the community centered around this grid: members
     299 can tell that other members are using the grid.
     300
     301
    282302== Storage Server Configuration ==
    283303
    284304[storage]
  • src/allmydata/chat.py

    diff -rN -u old-irc/src/allmydata/chat.py new-irc/src/allmydata/chat.py
    old new  
     1from allmydata.util.observer import ObserverList
     2
     3class ChatClient(Referenceable):
     4    MAX_RECENT_MESSAGES = 100
     5
     6    def __init__(self):
     7        self.ic = ??
     8        self.local_subscribers = ObserverList()
     9        self.recent_messages = [] # list of (timestamp, from, message)
     10
     11    def broadcast(self, message):
     12        for rref in self.ic.get_all_connections_for("chat"):
     13            rref.callRemoteOnly("broadcast", me, message)
     14
     15    def remote_broadcast(self, fromwho, message):
     16        now = time.time()
     17        self.recent_messages.append( (now, fromwho, message) )
     18        self.local_subscribers.notify(fromwho, message)
     19
     20    def subscribe_to_incoming_messages(self, observer):
     21        return self.local_subscribers.subscribe(observer)
     22    def unsubscribe_to_incoming_messages(self, observer):
     23        self.local_subscribers.unsubscribe(observer)
     24
     25    def get_recent_messages(self):
     26        return self.recent_messages # please don't modify this
     27       
  • src/allmydata/test/test_chat.py

    diff -rN -u old-irc/src/allmydata/test/test_chat.py new-irc/src/allmydata/test/test_chat.py
    old new  
     1
     2import os.path
     3from twisted.trial import unittest
     4from twisted.internet import defer
     5from allmydata.test.common import SystemTestMixin
     6
     7class Chat(SystemTestMixin, unittest.TestCase):
     8
     9    def _enable_chat(self, clientdir):
     10        cfgfn = os.path.join(clientdir, "tahoe.cfg")
     11        oldcfg = open(cfgfn, "r").read()
     12        f = open(cfgfn, "wt")
     13        f.write(oldcfg)
     14        f.write("\n")
     15        f.write("[chat]\n")
     16        f.write("enabled = True\n")
     17        f.write("\n")
     18        f.close()
     19        return None
     20
     21    def _enable_chat_and_publish(self, clientdir):
     22        cfgfn = os.path.join(clientdir, "tahoe.cfg")
     23        oldcfg = open(cfgfn, "r").read()
     24        f = open(cfgfn, "wt")
     25        f.write(oldcfg)
     26        f.write("\n")
     27        f.write("[chat]\n")
     28        f.write("enabled = True\n")
     29        f.write("publish_activity = True\n")
     30        f.write("\n")
     31        f.close()
     32        return None
     33                     
     34    def test_chat(self):
     35        self.basedir = "chat/Chat/chat"
     36
     37        hooks = {0: self._enable_chat,
     38                 1: self._enable_chat_and_publish }
     39        self.set_up_grid(num_servers=0, client_config_hooks=hooks)
     40        c0 = self.g.clients[0]
     41        c0.chatter.broadcast("Hello World")
     42        c1 = self.g.clients[1]
     43
     44        d = defer.Deferred()
     45        EXPECTED = 3
     46        def _got_message(fromwho, message):
     47            messages = c1.chatter.get_recent_messages()
     48            if len(messages) == EXPECTED:
     49                d.callback(messages)
     50        c1.chatter.subscribe_to_incoming_messages(_got_message)
     51
     52        def _check(messages):
     53            self.failUnless("Hello World" in messages, messages)
     54        d.addCallback(_check)
     55        return d
  • src/allmydata/util/observer.py

    diff -rN -u old-irc/src/allmydata/util/observer.py new-irc/src/allmydata/util/observer.py
    old new  
    8282    def __init__(self):
    8383        self._watchers = []
    8484
    85     def subscribe(self, observer):
     85    def subscribe(self, observer):   # todo *args, **kwargs
    8686        self._watchers.append(observer)
    8787
    8888    def unsubscribe(self, observer):