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

Search Discussions

  • David Murmann at Jan 12, 2006 at 2:46 pm

    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.
  • Marco Meoni at Jan 12, 2006 at 2:57 pm

    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 at Jan 12, 2006 at 3:08 pm
    About SWIG:
    This program has a lot of commands...
    In your opinion what is the best settings for my use?
    Thanks...
    Marco

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJan 12, '06 at 2:36p
activeJan 12, '06 at 3:08p
posts4
users2
websitepython.org

2 users in discussion

Marco Meoni: 3 posts David Murmann: 1 post

People

Translate

site design / logo © 2022 Grokbase