""" This file is based on simpleOSC 0.2 by Daniel Holth.
This file has been modified by Simon Schampijer.

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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
"""

import OSC
import socket

class OscApi:
    def __init__(self):
       """ inits manager and outsocket
       """
       # globals
       self.addressManager = 0
       self.ioSocket = 0
       self.ioSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
       self.addressManager = OSC.CallbackManager()

    def createListener(self, ipAddr, port):
        """create and return an inbound socket
        """
        self.ioSocket.bind((ipAddr, port))
        self.ioSocket.setblocking(0) # if not waits for msgs to arrive blocking other events
        return self.ioSocket

    def bind(self, func, oscaddress):
        """ bind certains oscaddresses with certain functions in address manager
        """
        self.addressManager.add(func, oscaddress)

    def recvhandler(self, data, addr):
        self.addressManager.handle(data, addr)     


#############----send----####################

    def createBinaryMsg(self, oscAddress, dataArray):
        """create and return general type binary OSC msg"""
        m = OSC.OSCMessage()
        m.setAddress(oscAddress)

        if len(dataArray) != 0:
            for x in dataArray:  ## append each item of the array to the message
                m.append(x)

        return m.getBinary() # get the actual OSC to send

    def sendOSC(self, stufftosend, ipAddr, port): # outSocket, 
        """ send OSC msg or bundle as binary"""
        self.ioSocket.sendto(stufftosend, (ipAddr, port))


############################### send message

    def sendMsg(self, oscAddress, dataArray, ipAddr, port):#, outSocket):
        """create and send normal OSC msgs"""
        msg = self.createBinaryMsg(oscAddress, dataArray)
        self.sendOSC(msg, ipAddr, port)  # outSocket, 

############################### bundle stuff + send bundle
    def createBundle(self):
        """create bundled type of OSC messages"""
        b = OSC.OSCMessage()
        b.setAddress("")
        b.append("#bundle")
        b.append(0)
        b.append(0)
        return b

    def appendToBundle(self, bundle, oscAddress, dataArray):
        """create OSC mesage and append it to a given bundle"""
        OSCmsg = self.createBinaryMsg(oscAddress, dataArray)
        bundle.append(OSCmsg, 'b')

    def sendBundle(self, bundle, ipAddr, port):#, outSocket):
        """convert bundle to a binary and send it"""
        self.sendOSC(bundle.message, ipAddr, port) # outSocket

