Extending & Embedding Python

Discussion in 'Python' started by Marco Meoni, Jan 12, 2006.

  1. Marco Meoni

    Marco Meoni Guest

    Hi all! I've a problem with a C++ class that has to be included in a
    python application. One way to do it is Extending and Embedding the
    Python Interpreter
    Now i have 2 questions
    1) Is there a one-file version of this tutorial?
    2) Is there anyone that can help me with this problem? The class is
    attached.

    Thanks all.
    Marco

    Attached files:
    ------------------------------------------------------------------------------------------------------
    #ifndef _PACKETMANAGER_H_
    #define _PACKETMANAGER_H_
    #include "Cipher/Cipher.h"

    #define PAYLOAD_LENGHT 5000

    //the our defined packe over the UDP datagram
    struct packet {
    int lenght; // sizeof(payload) = sizeof(int) + strlen(payload)
    unsigned char payload[PAYLOAD_LENGHT]; // the xml string
    } __attribute__ ((packed));


    //! manage the send and receive of udp packet
    /*! by default: send message non-cripted */
    class PacketManager
    {
    private:
    struct packet packet_to_send;
    Cipher * pc;
    int s; //socket descriptor
    public:
    PacketManager();
    virtual ~PacketManager();

    ///Network member function
    bool send(char * IPdest , int port_to);
    unsigned char * receive (char * IPdest, int timeout); //non-blocking
    unsigned char * waitMessage(char * IPfrom); //blocking

    ///Packet Builder member function
    bool build_Packet();
    bool setCipher(Cipher * c);
    Cipher * getCipher();

    ///Set and Get function on all attributes

    bool set_Payload(unsigned char * c, int lenght);
    unsigned char * get_Payload();

    bool set_packet_lenght(unsigned int lenght);
    unsigned int get_packet_lenght();

    void set_Socket(int socket);
    int get_Socket();
    bool buildPacket();
    void setSocket(int socket);
    packet getPacket();
    bool setPacket(unsigned char * c,int lenght);
    void closeSocket();
    };

    #endif //_PACKETMANAGER_H_
    ---------------------------------------------------------------------------------------------------
    #include "Cipher/Cipher.h"
    #include "Cipher/NullCipher.h"

    #include "PacketManager.h"

    #include "messages.h"

    #define PORT_FROM 40004

    //! manage the send and receive of udp packet
    /*! by default: send message non-cripted */
    PacketManager::packetManager()
    {

    pc = new NullCipher();

    s = create_UDP_socket(PORT_FROM);

    }

    PacketManager::~PacketManager()
    {
    close (s);
    delete pc;
    }

    void PacketManager::setSocket(int socket)
    {
    s = socket;
    }

    bool PacketManager::buildPacket()
    {
    pc->encrypt(); //! ciphering
    return true;

    }

    Cipher * PacketManager::getCipher()
    {
    return pc;
    }

    bool PacketManager::setCipher(Cipher * c)
    {
    pc=c;
    return true;
    }

    //! send payload + header to IPdest at port_to
    /*! send payload + header in an UDP packet
    \param IPdest address to witch send the packet
    \param port_to port to witch send the packet
    */
    bool PacketManager::send(char *IPdest,int port_to)

    {


    //unsigned char * message_to_send = new unsigned
    char(packet_to_send.lenght * sizeof(unsigned char));
    unsigned char message_to_send[5000];

    memcpy (message_to_send, &(packet_to_send.lenght) ,
    sizeof(int));

    unsigned char * tmp_payload = message_to_send;
    tmp_payload+=sizeof(int);

    memcpy(tmp_payload,packet_to_send.payload,strlen((char*)packet_to_send.payload));

    // memcpy (message_to_send + sizeof(int), packet_to_send.payload,
    packet_to_send.lenght - sizeof(int)); //! mess =
    [int][payload]



    //memcpy (message_to_send, &packet_to_send ,sizeof (struct
    packet)); //! mess = [packet_to_send]

    packet_to_send.payload[packet_to_send.lenght-4]='\0';

    printf ("Sto per inviare: \nlenght = \t%d \nPayload = \t%s\n",
    packet_to_send.lenght , packet_to_send.payload);

    message_to(s,port_to,IPdest, message_to_send , packet_to_send.lenght
    );

    //delete message_to_send;

    //return 0;

    return true;
    }

    //! wait for a packet, non blocking, don't check for IP
    /*! wait for a packet, non blocking, don't check for IP
    * param IPdest should be the IP from witch receive files
    * param timeout should be the seconds to wait until a message was
    received
    */
    unsigned char * PacketManager::receive(char *IPdest, int timeout)
    {
    info risposta;
    risposta = wait_message(s);
    //!have to check if the message return from IPdest
    return risposta.message;

    }

    //! wait for a packet from IPfrom, blocking
    /*! wait for a packet from IPfrom, blocking, with check on IP
    \param IPfrom the IP of the machine you want to wait for
    */
    unsigned char * PacketManager::waitMessage(char *IPfrom)
    {

    info mess_received;
    bool b = false;

    while (b == false )
    {
    mess_received = wait_message(s);

    in_addr_t ip_manager; //!check if the message return from IPdest
    ip_manager = inet_addr(IPfrom);
    unsigned char * ip_man = new unsigned char(4);
    ip_man = (unsigned char * ) &ip_manager;
    //check
    if ( ip_man[0] == mess_received.ip[0] && \
    ip_man[1] == mess_received.ip[1] && \
    ip_man[2] == mess_received.ip[2] && \
    ip_man[3] == mess_received.ip[3] \
    )
    b = true;

    }

    return mess_received.message;

    }

    bool PacketManager::setPacket(unsigned char * c,int lenght)
    {
    //strcpy ((char *) payload, (char *) c); //! with strcpy you
    can't have \0 in middle of a payload, or it will be truncated

    memcpy(packet_to_send.payload,c,lenght);
    packet_to_send.lenght = lenght + sizeof (int);


    return true;
    }

    packet PacketManager::getPacket()
    {
    return packet_to_send;
    }

    void PacketManager::closeSocket(){

    close(s);
    }
    --------------------------------------------------------------------------------------------
    Marco Meoni, Jan 12, 2006
    #1
    1. Advertising

  2. Marco Meoni schrieb:
    > Hi all! I've a problem with a C++ class that has to be included in a
    > python application. One way to do it is Extending and Embedding the
    > Python Interpreter
    > Now i have 2 questions
    > 1) Is there a one-file version of this tutorial?


    i'm not sure what tutorial you mean. are you talking about the
    Extending and Embedding section of the python manual?

    http://docs.python.org/ext/ext.html

    > 2) Is there anyone that can help me with this problem? The class is
    > attached.


    i haven't read your code, but you might want to take a look at SWIG:

    http://www.swig.org/

    --
    David.
    David Murmann, Jan 12, 2006
    #2
    1. Advertising

  3. Marco Meoni

    Marco Meoni Guest

    > i'm not sure what tutorial you mean. are you talking about the
    > Extending and Embedding section of the python manual?
    >
    > http://docs.python.org/ext/ext.html


    Yes, this section. Is there in one-file version? (pdf, ps, dvi, all in
    1 html, etc...)
    Marco Meoni, Jan 12, 2006
    #3
  4. Marco Meoni

    Marco Meoni Guest

    About SWIG:
    This program has a lot of commands...
    In your opinion what is the best settings for my use?
    Thanks...
    Marco
    Marco Meoni, Jan 12, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luke
    Replies:
    3
    Views:
    513
    Robin Becker
    Oct 24, 2003
  2. Faheem Mitha
    Replies:
    0
    Views:
    420
    Faheem Mitha
    Jun 28, 2004
  3. Alicia Haumann

    Extending/Embedding python

    Alicia Haumann, Sep 2, 2004, in forum: Python
    Replies:
    4
    Views:
    1,237
    Rick Ratzel
    Sep 9, 2004
  4. Hugh Macdonald
    Replies:
    0
    Views:
    284
    Hugh Macdonald
    Nov 19, 2004
  5. stefan
    Replies:
    3
    Views:
    435
    stefan
    Dec 8, 2004
Loading...

Share This Page