ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: ZChat
Submitter: Stephen Chappell (other recipes)
Last Updated: 2006/07/07
Version no: 1.0
Category: Network

 

Not Rated yet


Description:

The following four programs provide an example of how the z_service module can be used. This recipe demonstrates a chatting program built off of the easy to use framework provided by the Client, Server, and Sync_Server classes. These abstractions are meant to make networked function calls intuitive and easy to set up.

Source: Text Source

#==============================================================================#
Server.py
#==============================================================================#
import time

class Server:

    def __init__(self, max_age):
        self.__max_age = max_age * 60
        self.__message = {}
        self.__ticket = 1

    def add(self, ID, message):
        self.__message[self.__ticket] = time.clock(), ID, message
        self.__ticket += 1

    def query(self, ID, ticket):
        if not self.__message:
            return 0, []
        minimum = min(self.__message)
        if ticket < minimum:
            ticket = minimum
        messages = []
        for index in range(ticket, self.__ticket):
            clock, message_ID, message = self.__message[index]
            if message_ID != ID:
                messages.append('%s: %s' % (message_ID, message))
        return self.__ticket, messages

    def serve(self):
        while True:
            if not self.__message:
                time.sleep(self.__max_age)
            else:
                time.sleep(self.__max_age + self.__message[min(self.__message)][0] - time.clock())
                del self.__message[min(self.__message)]

import z_service

def main():
    chat_server = Server(5)
    rpc_server = z_service.Sync_Server('', 9000)
    rpc_server.add_service('add', chat_server.add)
    rpc_server.add_service('query', chat_server.query)
    chat_server.serve()

if __name__ == '__main__':
    main()
#==============================================================================#
Input.py
#==============================================================================#
import z_service

def main():
    name = raw_input('Name: ')
    client = z_service.Client(raw_input('Host: '), 9000)
    while True:
        client('add', name, raw_input('Say: '))

if __name__ == '__main__':
    main()
#==============================================================================#
Output.py
#==============================================================================#
import z_service, time

def main():
    name = raw_input('Name: ')
    client = z_service.Client(raw_input('Host: '), 9000)
    ticket = 0
    while True:
        temp_ticket, messages = client('query', name, ticket)
        if temp_ticket:
            ticket = temp_ticket
            for message in messages:
                print message
        time.sleep(5)

if __name__ == '__main__':
    main()
#==============================================================================#
Logger.py
#==============================================================================#
import z_service, time

def main():
    log = file(raw_input('Filename: '), 'w')
    client = z_service.Client(raw_input('Host: '), 9000)
    ticket = 0
    while True:
        temp_ticket, messages = client('query', None, ticket)
        if temp_ticket:
            ticket = temp_ticket
            for message in messages:
                log.write(message + '\n')
        time.sleep(60 * 4)

if __name__ == '__main__':
    main()

Discussion:

Server.py should be started first.
Input.py should be started to post messages to the server.
Output.py should be started to receive messages from the server.
Logger.py should be started to log the conversation.



Add comment

No comments.



Highest rated recipes:

1. A simple XML-RPC server

2. Web service accessible ...

3. IPy Notify

4. Changing return value ...

5. Quantum Superposition

6. Pickle objects under ...

7. Generalized delegates ...

8. Reorder a sequence (uses ...

9. Setting Win32 System ...

10. ObjectMerger




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.