<commit_before>#!/usr/bin/python

# Farsight 2 simple network signalling library for the demo GUI
#
# Copyright (C) 2007 Collabora, Nokia
# @author: <NAME> <<EMAIL>>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
#

#
# This is the signalling code used by fs2-gui.py
#

import sys, os, pwd, os.path
import socket, struct
import gc


try:
    import pygst
    pygst.require('0.10')
        
    import gst
except ImportError, e:
    raise SystemExit("Gst-Python couldn't be found! (%s)" % (e[0]))

try:
    import farsight
except:
    sys.path.append(os.path.join(os.path.dirname(__file__),
                                 '..', '..', 'python', '.libs'))
    import farsight

import gobject

class FsUIConnect:
    ERROR = 0
    CODECS = 1
    CANDIDATE = 2
    CANDIDATES_DONE = 3
    INTRO = 4

    def __reset(self):
        self.type = None
        self.media = None
        self.size = struct.calcsize("!IIIIII")
        self.data = ""
        self.dest = -1
        self.src = -1
 
    
    def __init__(self, sock, callbacks, myid=0):
        self.sock = sock
        self.__reset()
        self.callbacks = callbacks
        self.myid = myid
        self.partid = 1
        sock.setblocking(0)
        gobject.io_add_watch(self.sock.fileno(), gobject.IO_IN,
                             self.__data_in)
        gobject.io_add_watch(self.sock.fileno(),
                             gobject.IO_ERR | gobject.IO_HUP,
                             self.__error)

    def __error(self, source, condition):
        print "have error"
        if (self.src >= 0):
            self.callbacks[self.ERROR](self.src)
        else:
            self.callbacks[self.ERROR](self.partid)
        return False

    def __data_in(self, source, condition):
        data = self.sock.recv(self.size-len(self.data))

        if len(data) == 0:
            print "received nothing"
            if (self.src >= 0):
                self.callbacks[self.ERROR](self.src)
            else:
                self.callbacks[self.ERROR](self.partid)
            return False
        
        self.data += data
        if len(self.data) == self.size:
            if self.type is not None:
                if self.type == self.CODECS:
                    data = self.__codecs_from_string(data)
                elif self.type == self.CANDIDATE:
                    data = self.__candidate_from_string(data)
                else:
                    data = self.data
                self.callbacks[self.type](self.src, self.dest,
                                          self.media, data)
                self.__reset()
            else:
                (check,
                 self.src,
                 self.dest,
                 self.type,
                 self.media,
                 self.size) = struct.unpack("!IIIIII", self.data)
                if check != 0xDEADBEEF:
                    print "CORRUPTION"
                    sys.exit(1)
                if self.myid > 1 and self.dest != self.myid:
                    print "GOT MESSAGE FOR %d, but I am %d" % (self.dest,
                                                               self.myid)
                    sys.exit(1)
                self.data=""
                if self.size == 0:
                    self.callbacks[self.type](self.src, self.dest,
                                              self.media, data)
                    self.__reset()
        return True

    def __send_data(self, dest, type, media=0, data="", src=None):
        if src is None: src = self.myid
        if src == 0 and type != self.INTRO: raise Exception
        try:
            self.sock.sendall(struct.pack("!IIIIII",
                                          0xDEADBEEF,
                                          int(src),
                                          int(dest),
                                          int(type),<commit_msg>Send None data if there is no data<commit_after>#!/usr/bin/python

# Farsight 2 simple network signalling library for the demo GUI
#
# Copyright (C) 2007 Collabora, Nokia
# @author: <NAME> <<EMAIL>>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
#

#
# This is the signalling code used by fs2-gui.py
#

import sys, os, pwd, os.path
import socket, struct
import gc


try:
    import pygst
    pygst.require('0.10')
        
    import gst
except ImportError, e:
    raise SystemExit("Gst-Python couldn't be found! (%s)" % (e[0]))

try:
    import farsight
except:
    sys.path.append(os.path.join(os.path.dirname(__file__),
                                 '..', '..', 'python', '.libs'))
    import farsight

import gobject

class FsUIConnect:
    ERROR = 0
    CODECS = 1
    CANDIDATE = 2
    CANDIDATES_DONE = 3
    INTRO = 4

    def __reset(self):
        self.type = None
        self.media = None
        self.size = struct.calcsize("!IIIIII")
        self.data = ""
        self.dest = -1
        self.src = -1
 
    
    def __init__(self, sock, callbacks, myid=0):
        self.sock = sock
        self.__reset()
        self.callbacks = callbacks
        self.myid = myid
        self.partid = 1
        sock.setblocking(0)
        gobject.io_add_watch(self.sock.fileno(), gobject.IO_IN,
                             self.__data_in)
        gobject.io_add_watch(self.sock.fileno(),
                             gobject.IO_ERR | gobject.IO_HUP,
                             self.__error)

    def __error(self, source, condition):
        print "have error"
        if (self.src >= 0):
            self.callbacks[self.ERROR](self.src)
        else:
            self.callbacks[self.ERROR](self.partid)
        return False

    def __data_in(self, source, condition):
        data = self.sock.recv(self.size-len(self.data))

        if len(data) == 0:
            print "received nothing"
            if (self.src >= 0):
                self.callbacks[self.ERROR](self.src)
            else:
                self.callbacks[self.ERROR](self.partid)
            return False
        
        self.data += data
        if len(self.data) == self.size:
            if self.type is not None:
                if self.type == self.CODECS:
                    data = self.__codecs_from_string(data)
                elif self.type == self.CANDIDATE:
                    data = self.__candidate_from_string(data)
                else:
                    data = self.data
                self.callbacks[self.type](self.src, self.dest,
                                          self.media, data)
                self.__reset()
            else:
                (check,
                 self.src,
                 self.dest,
                 self.type,
                 self.media,
                 self.size) = struct.unpack("!IIIIII", self.data)
                if check != 0xDEADBEEF:
                    print "CORRUPTION"
                    sys.exit(1)
                if self.myid > 1 and self.dest != self.myid:
                    print "GOT MESSAGE FOR %d, but I am %d" % (self.dest,
                                                               self.myid)
                    sys.exit(1)
                self.data=""
                if self.size == 0:
                    self.callbacks[self.type](self.src, self.dest,
                                              self.media, None)
                    self.__reset()
        return True

    def __send_data(self, dest, type, media=0, data="", src=None):
        if src is None: src = self.myid
        if src == 0 and type != self.INTRO: raise Exception
        try:
            self.sock.sendall(struct.pack("!IIIIII",
                                          0xDEADBEEF,
                                          int(src),
                                          int(dest),
                                          int(type),
{'fraction_non_alphanumeric': 0.09534246575342466, 'fraction_numerical': 0.009643835616438357, 'mean_word_length': 1.4505907626208379, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5597098', 'n_tokens_mistral': 2622, 'n_tokens_neox': 2473, 'n_words': 728}