~ubuntu-branches/ubuntu/natty/pida/natty

« back to all changes in this revision

Viewing changes to pida/services/rpc.py

  • Committer: Bazaar Package Importer
  • Author(s): Jan Luebbe
  • Date: 2007-09-05 17:54:09 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070905175409-ty9f6qpuctyjv1sd
Tags: 0.5.1-2
* Depend on librsvg2-common, which is not pulled in by the other depends
  (closes: #394860)
* gvim is no alternative for python-gnome2 and python-gnome2-extras
  (closes: #436431)
* Pida now uses ~/.pida2, so it can no longer be confused by old
  configurations (closes: #421378)
* Culebra is no longer supported by upstream (closes: #349009)
* Update manpage (closes: #440375)
* Update watchfile (pida is now called PIDA)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*- 
2
 
 
3
 
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
4
 
#Copyright (c) 2005 Ali Afshar aafshar@gmail.com
5
 
 
6
 
#Permission is hereby granted, free of charge, to any person obtaining a copy
7
 
#of this software and associated documentation files (the "Software"), to deal
8
 
#in the Software without restriction, including without limitation the rights
9
 
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 
#copies of the Software, and to permit persons to whom the Software is
11
 
#furnished to do so, subject to the following conditions:
12
 
 
13
 
#The above copyright notice and this permission notice shall be included in
14
 
#all copies or substantial portions of the Software.
15
 
 
16
 
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
 
#SOFTWARE.
23
 
 
24
 
import sys
25
 
if sys.platform == "win32":
26
 
    raise ImportError("RPC is not compatible with win32")
27
 
 
28
 
import pida.core.service as service
29
 
from pida.utils.kiwiutils import gsignal
30
 
import gobject
31
 
import os
32
 
 
33
 
class rpc(service.service):
34
 
 
35
 
 
36
 
    def init(self):
37
 
        self.__pipename = os.path.join(self.boss.pida_home,
38
 
                                        'sockets',
39
 
                                        'pida.%s' % os.getpid())
40
 
        self.__pipe = None
41
 
        self.call('start')
42
 
 
43
 
    def cmd_start(self):
44
 
        self.__pipe = reactor(self.__pipename)
45
 
        self.__pipe.connect('received', self.cb_pipe_received)
46
 
        self.__pipe.start()
47
 
        
48
 
    def cmd_stop(self):
49
 
        self.stop()
50
 
 
51
 
    def cb_pipe_received(self, pipe, (address, command, args)):
52
 
        if len(args) == 0 and command:
53
 
            filename = command
54
 
            self.log.debug('remote file open %s', filename)
55
 
            self.__pipe.send(address, 'OK\1OK\0')
56
 
            self.boss.call_command('buffermanager', 'open_file',
57
 
                                   filename=filename)
58
 
        else:
59
 
            self.log.debug('remote ping from %s', address)
60
 
            self.__pipe.send(address, 'EEK\1EEK\0')
61
 
        self.call('start')
62
 
 
63
 
    def stop(self):
64
 
        self.__pipe.stop()
65
 
 
66
 
"""
67
 
A Symmetrical Unix Domain Socket UDP remote procedure protocol.
68
 
"""
69
 
 
70
 
# gobject import
71
 
import gobject
72
 
 
73
 
# system imports
74
 
import os
75
 
import socket
76
 
 
77
 
class reactor(gobject.GObject):
78
 
 
79
 
    gsignal('received', object)
80
 
 
81
 
    def __init__(self, localsocketfile):
82
 
        gobject.GObject.__init__(self)
83
 
        self.socketfile = localsocketfile
84
 
        self.__buffers = {}
85
 
         
86
 
    def start(self):
87
 
        if os.path.exists(self.socketfile):
88
 
            os.remove(self.socketfile)
89
 
        self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
90
 
        self.socket.bind(self.socketfile)
91
 
        gobject.io_add_watch(self.socket, gobject.IO_IN, self.cb_read)
92
 
    
93
 
    def stop(self):
94
 
        os.unlink(self.socketfile)
95
 
 
96
 
    def cb_read(self, socket, condition):
97
 
        if condition == gobject.IO_IN:
98
 
            data, address = socket.recvfrom(6024)
99
 
            self.received_data(data, address)
100
 
        return True
101
 
 
102
 
    def send(self, address, data):
103
 
        self.socket.sendto(data, address)
104
 
 
105
 
    def local(self, address, command, *args):
106
 
        self.emit('received', [address, command, args])
107
 
 
108
 
    def remote(self, command, *args):
109
 
        commandstring = '%s\1%s\0' % (command, '\1'.join(args))
110
 
        self.send(commandstring)
111
 
 
112
 
    def received_data(self, data, address):
113
 
        buf = self.__buffers.setdefault(address, '')
114
 
        self.__buffers[address] = buf + data
115
 
        print self.__buffers
116
 
        self.process_data(address)
117
 
 
118
 
    def process_data(self, address):
119
 
        lines = self.__buffers[address].split('\0')
120
 
        self.__buffers[address] = lines.pop()
121
 
        for line in lines:
122
 
            self.received_line(line, address)
123
 
 
124
 
    def received_line(self, line, address):
125
 
        args = line.split('\1')
126
 
        command = args.pop(0)
127
 
        self.local(address, command, *args)
128
 
 
129
 
gobject.type_register(reactor)
130
 
 
131
 
Service = rpc