Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Extending & Embedding Python

Reply
Thread Tools

Extending & Embedding Python

 
 
Marco Meoni
Guest
Posts: n/a
 
      01-12-2006
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:acketManager()
{

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);
}
--------------------------------------------------------------------------------------------

 
Reply With Quote
 
 
 
 
David Murmann
Guest
Posts: n/a
 
      01-12-2006
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.
 
Reply With Quote
 
 
 
 
Marco Meoni
Guest
Posts: n/a
 
      01-12-2006
> 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...)

 
Reply With Quote
 
Marco Meoni
Guest
Posts: n/a
 
      01-12-2006
About SWIG:
This program has a lot of commands...
In your opinion what is the best settings for my use?
Thanks...
Marco

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
after embedding and extending python (using swig) problem importing (non-core) modules stefan Python 3 12-08-2004 07:11 PM
Embedding/Extending Python at the same time... Hugh Macdonald Python 0 11-19-2004 04:21 PM
Extending/Embedding python Alicia Haumann Python 4 09-08-2004 11:36 PM
embedding and extending issues (with Boost.Python) Faheem Mitha Python 0 06-28-2004 06:44 PM
extending and embedding python with C# Luke Python 3 10-24-2003 04:43 PM



Advertisments