1
# -*- coding: utf-8 -*-
3
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
4
#Copyright (c) 2005 Ali Afshar aafshar@gmail.com
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:
13
#The above copyright notice and this permission notice shall be included in
14
#all copies or substantial portions of the Software.
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
25
if sys.platform == "win32":
26
raise ImportError("RPC is not compatible with win32")
28
import pida.core.service as service
29
from pida.utils.kiwiutils import gsignal
33
class rpc(service.service):
37
self.__pipename = os.path.join(self.boss.pida_home,
39
'pida.%s' % os.getpid())
44
self.__pipe = reactor(self.__pipename)
45
self.__pipe.connect('received', self.cb_pipe_received)
51
def cb_pipe_received(self, pipe, (address, command, args)):
52
if len(args) == 0 and 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',
59
self.log.debug('remote ping from %s', address)
60
self.__pipe.send(address, 'EEK\1EEK\0')
67
A Symmetrical Unix Domain Socket UDP remote procedure protocol.
77
class reactor(gobject.GObject):
79
gsignal('received', object)
81
def __init__(self, localsocketfile):
82
gobject.GObject.__init__(self)
83
self.socketfile = localsocketfile
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)
94
os.unlink(self.socketfile)
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)
102
def send(self, address, data):
103
self.socket.sendto(data, address)
105
def local(self, address, command, *args):
106
self.emit('received', [address, command, args])
108
def remote(self, command, *args):
109
commandstring = '%s\1%s\0' % (command, '\1'.join(args))
110
self.send(commandstring)
112
def received_data(self, data, address):
113
buf = self.__buffers.setdefault(address, '')
114
self.__buffers[address] = buf + data
116
self.process_data(address)
118
def process_data(self, address):
119
lines = self.__buffers[address].split('\0')
120
self.__buffers[address] = lines.pop()
122
self.received_line(line, address)
124
def received_line(self, line, address):
125
args = line.split('\1')
126
command = args.pop(0)
127
self.local(address, command, *args)
129
gobject.type_register(reactor)