Skip to content

Commit

Permalink
Revert "Thread safety to autoqueue. Change UdpLink::map to UdpLink::c…
Browse files Browse the repository at this point in the history
…lients."
  • Loading branch information
Jester565 authored Nov 20, 2016
1 parent 163236a commit 9b58b93
Show file tree
Hide file tree
Showing 81 changed files with 3,500 additions and 1,550 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/build/
/win32/
/linux/
tests/*

# Test files not required.
/CommProto/src/main.cpp
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ if (test)
# C++ testing.
add_subdirectory(tests/classes/cpp/udp/)
add_subdirectory(tests/classes/cpp/xbee/)
add_subdirectory(tests/classes/cpp/serial/)
# Vistionary testing toolset.
add_subdirectory(unit)
endif()
Expand Down
2 changes: 1 addition & 1 deletion CommProto/include/CommProto/abstractpacket.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class COMM_EXPORT AbstractPacket {
virtual AbstractPacket* Create() = 0;

template<typename Type>
static Type& GetValue(AbstractPacket& packet) {
static Type& GetValue(AbstractPacket& packet) NOEXCEPT {
try {
return dynamic_cast< Type& >( packet );
} catch ( std::bad_cast e ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
Linux serial configuration.
Copyright (C) 2016 Michael Wallace, Kartik Soni, Mario Garcia.
Copyright (C) 2016 Michael Wallace, Kartik Soni, Mario Garcia, Alex Craig.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -28,11 +28,6 @@
#include <errno.h>
#include <termios.h>


#define ClosePort close



struct serial_info {

speed_t baudrate;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
Windows serial configuration.
Copyright (C) 2016 Michael Wallace, Kartik Soni, Mario Garcia.
Copyright (C) 2016 Michael Wallace, Kartik Soni, Mario Garcia, Alex Craig.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -24,10 +24,6 @@

typedef HANDLE serial_h;


#define ClosePort CloseHandle


struct serial_info {

speed_t baudrate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <fcntl.h>
Expand Down
1 change: 0 additions & 1 deletion CommProto/include/CommProto/architecture/os/arch.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
// Apple : OSX
#if defined(__APPLE__) && defined(__MACH__)
#undef COM_TARGET_OS
#define COM_TARGET_OS COM_OS_APPLE
#define COM_TARGET_OS COM_OS_APPLE
#endif // __APPLE__ && __MACH__

Expand Down
12 changes: 9 additions & 3 deletions CommProto/include/CommProto/architecture/os/include_defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,20 @@
c(c& obj) = delete; \
c& operator=(c& obj) = delete;


#define NOEXCEPT noexcept
/*
TODO(): This will need to be defined with windows.
*/
#if COM_TARGET_OS == COM_OS_WINDOWS
#define FORCE_INLINE __forceinline
#define FORCE_INLINE __forceinline
#if defined(_MSC_VER) && defined(NOEXCEPT)
#if _MSC_VER <= 1800
#undef NOEXCEPT
#define NOEXCEPT
#endif
#endif
#else
#define FORCE_INLINE __inline __attribute__((always_inline))
#define FORCE_INLINE __inline __attribute__((always_inline))
#endif // COM_TARGET_OS == COM_OS_WINDOWS

#endif // __INCLUDE_DEFINES_H
4 changes: 2 additions & 2 deletions CommProto/include/CommProto/architecture/pipes.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ _INTERFACE_ COMM_EXPORT IPipe {
NOTE(Garcia): For Win32, "\\\\.\\pipename" is used, whereas, linux is something else.
*/
virtual bool Connect(std::string path, PipeMode mode) = 0;
virtual bool Read(char* buf, uint32_t len) = 0;
virtual bool Write(char* buf, uint32_t& len) = 0;
virtual bool Read(char* buf, uint32_t& len) = 0;
virtual bool Write(char* buf, uint32_t len) = 0;
virtual pipe_t& GetReadHandle() = 0;
virtual pipe_t& GetWriteHandle() = 0;
virtual PipeStatus GetStatus() = 0;
Expand Down
39 changes: 38 additions & 1 deletion CommProto/include/CommProto/architecture/win32_pipes.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,47 @@ namespace architecture {


class COMM_EXPORT Pipe : public IPipe {
COMM_DISALLOW_COPYING(Pipe);
public:
Pipe() { }

/**
Creates an anonymous local pipe, used for local communications to CommProtocol.
@param mode The mode of which you want this pipe to be using. This allows reading
one way, or writing one way, or none, or both.
*/
bool Create(PipeMode mode) override {
bool success = false;
// Create an anonymous local pipe for windows.
switch (mode) {
case READ:
break;
case WRITE:
break;
default:
};
success = CreatePipe(rpipe, wpipe, NULL, 0);
return success;
}


bool CreateNamed(std::string path, PipeMode mode) override;
bool Connect(std::string path, PipeMode mode) override;
bool Read(char* buf, uint32_t& len) override;
bool Write(char* buf, uint32_t len) override;
bool Close() override {
CloseHandle(rpipe);
CloseHandle(wpipe);
}
pipe_t& GetReadHandle() override { return rpipe; }
pipe_t& GetWriteHandle() override { return wpipe; }
PipeStatus GetStatus() override { return status; }
PipeType GetType() override { return type; }
private:
pipe_t pipe;
pipe_t rpipe;
pipe_t wpipe;
PipeType type;
PipeStatus status;
};
} // architecture
} // comnet
Expand Down
18 changes: 14 additions & 4 deletions CommProto/include/CommProto/commnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ _COMNET_ABSTRACT_ class COMM_EXPORT CommNode {
Polymorphic Destructor.
*/
virtual ~CommNode() {
for (int32_t i = 0; i < recv_queue->GetSize(); ++i) {
AbstractPacket* abs = recv_queue->Front();
recv_queue->Dequeue();
free_pointer(abs);
}
for (int32_t i = 0; i < send_queue->GetSize(); ++i) {
ObjectStream* obj = send_queue->Front();
send_queue->Dequeue();
free_pointer(obj);
}
free_pointer(recv_queue);
free_pointer(send_queue);
}
Expand Down Expand Up @@ -118,7 +128,7 @@ _COMNET_ABSTRACT_ class COMM_EXPORT CommNode {
/**
Send the packet to the specified destination address.
*/
virtual bool Send(AbstractPacket* packet, uint8_t dest_id) = 0;
virtual bool Send(AbstractPacket& packet, uint8_t dest_id) = 0;
/**
Check for packet if received. This is called manually by user, yet the node should
be able to Run automatically checking for received packets. Any packets linked to a
Expand All @@ -132,9 +142,9 @@ _COMNET_ABSTRACT_ class COMM_EXPORT CommNode {
Initialize connection.
*/
virtual bool InitConnection(transport_protocol_t conn_type,
const char* port,
const char* address,
uint32_t baud_rate = 0) = 0;
const char* port,
const char* address,
uint32_t baud_rate = 0) = 0;

/**
Add a communication address.
Expand Down
145 changes: 74 additions & 71 deletions CommProto/include/CommProto/comms.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,102 +20,105 @@
#define COMMS_H

#include <CommProto/tools/data_structures/interface/interface_queue.h>
#include <CommProto/network/commslink.h> //communication layer interface/abstract base class
#include <CommProto/debug/error_status.h>
#include <CommProto/console/console.h>
#include <CommProto/architecture/os/include_defines.h>
#include <CommProto/architecture/os/comm_mutex.h>
#include <CommProto/architecture/os/comm_thread.h>
#include <CommProto/architecture/macros.h>//str_lgnth(char*, int)
#include <CommProto/abstractpacket.h>
#include <CommProto/network/commslink.h>
#include <CommProto/commnode.h>
#include <CommProto/headerpacket.h>//Header which declares packet structs
#include <CommProto/encryption/aes_encryption.h>

#include <iostream>//testing only
#include <stdint.h>//needed for bit size variables
#include <CommProto/encryption/encryptor.h>
#include <CommProto/encryption/decryptor.h>


namespace comnet {


using namespace std;
using namespace comnet::tools::datastructures;
using namespace comnet::serialization;
using namespace comnet::network;
using namespace comnet::architecture::os;
using namespace std;
using namespace comnet::tools::datastructures;
using namespace comnet::serialization;
using namespace comnet::network;
using namespace comnet::architecture::os;
/**
Comms is a standard CommNode node object. It handles elementary and intermediate
commands and functionality in order to work to the user's specifications of communications.
*/
_COMNET_PUBLIC_API_ class COMM_EXPORT Comms : public CommNode {
private:
CommMutex send_mutex;
CommMutex recv_mutex;
CommMutex console_mutex;
/** AES encryption class for encrypting and decrypting buffer stream not header*/
//AesEncryption aes_encryption;
CommMutex send_mutex;
CommMutex recv_mutex;
CommMutex console_mutex;
/** AES encryption class for encrypting and decrypting buffer stream not header*/
//AesEncryption aes_encryption;

/** Length of data buffer for communication stream */
uint32_t rx_length;
/** Length of data buffer for communication stream */
uint32_t rx_length;

/** Thread to Run communication data */
CommThread comm_thread_send;
CommThread comm_thread_recv;
CommThread console_thread;
/** Thread to Run communication data */
CommThread comm_thread_send;
CommThread comm_thread_recv;
CommThread console_thread;

/** Method to Run in communication thread */
void CommunicationHandlerSend();
void CommunicationHandlerRecv();
/** Method to Run in communication thread */
void CommunicationHandlerSend();
void CommunicationHandlerRecv();

/** Polymorphic (Base Class) Communication link for connection code*/
CommsLink *conn_layer;
/** Polymorphic (Base Class) Communication link for connection code*/
CommsLink *conn_layer;


/** Encryption class used to encrypt and dectrypt data */
AesEncryption comm_encryption;
/** Encryption class used to encrypt and dectrypt data */
encryption::CommEncryptor encrypt;
encryption::CommDecryptor decrypt;

public:
/** Constructor */
Comms(uint8_t platform_id);
/** Destructor */
~Comms();

/** input c string as the form of encrytion key*/
bool LoadKey(char* key);
/** load file which contatins the encryption key by the file name*/
bool LoadKeyFromFile(char*keyFileName);

/** Method will init the connection device and return false if connection failed to init properly
enum connection type to use serial, UDP, zigbee, ect
port number is the comport or UDP port used will differ from windows and Unix for comports COM05 or /dev/ttyUSB05
baud-rate is not used for for UDP which is not needed but for serial and zigbee it is needed for baud rate */
bool InitConnection(transport_protocol_t conn_type, const char* port, const char* address = NULL, uint32_t baudrate = 0);

/** The address entered will be paired for communication by destination ID
Adding address can be a UDP IPV4 or hex MAC address for zigbee
Adding an address is not need for serial and will default to ""*/
bool AddAddress(uint8_t dest_id, const char* address = NULL, uint16_t port = 0);

/** Removing an address removes the known association of address and destination ID by using id*/
bool RemoveAddress(uint8_t dest_id);

bool Send(AbstractPacket* packet, uint8_t dest_id);
AbstractPacket* Receive(uint8_t& source_id);

/** Method to start communication*/
void Run();
/** Method to toggle Pause communication*/
void Pause();
/** Method to Stop communication*/
void Stop();
// Sets up the home console.
bool SetupConsole(uint16_t port, const char* addr = nullptr) { return false; }
/** Constructor */
Comms(uint8_t platform_id);
/** Destructor */
~Comms();

/** input c string as the form of encrytion key*/
bool LoadKey(char* key);
/** load file which contatins the encryption key by the file name*/
bool LoadKeyFromFile(char*keyFileName);

/** Method will init the connection device and return false if connection failed to init properly
enum connection type to use serial, UDP, zigbee, ect
port number is the comport or UDP port used will differ from windows and Unix for comports COM05 or /dev/ttyUSB05
baud-rate is not used for for UDP which is not needed but for serial and zigbee it is needed for baud rate */
bool InitConnection(transport_protocol_t conn_type,
const char* port,
const char* address = NULL,
uint32_t baudrate = 0) override;

/** The address entered will be paired for communication by destination ID
Adding address can be a UDP IPV4 or hex MAC address for zigbee
Adding an address is not need for serial and will default to ""*/
bool AddAddress(uint8_t dest_id, const char* address = NULL, uint16_t port = 0) override;

/** Removing an address removes the known association of address and destination ID by using id*/
bool RemoveAddress(uint8_t dest_id) override;

bool Send(AbstractPacket& packet, uint8_t dest_id) override;
AbstractPacket* Receive(uint8_t& source_id) override;

/** Method to start communication*/
void Run() override;
/** Method to toggle Pause communication*/
void Pause() override;
/** Method to Stop communication*/
void Stop() override;
// Sets up the home console.
bool SetupConsole(uint16_t port, const char* addr = nullptr) { return false; }

protected:
// Nothing yet.
void LogToConsoles();
};//End Comms class
// Nothing yet.
void LogToConsoles();
private:

void HandlePacket(error_t error, AbstractPacket* packet);
};//End Comms class
} // namespace Comnet
#endif//End if COMMS_H
Loading

7 comments on commit 9b58b93

@CheezBoiger
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jester565 It still kept your changes to UDPlink and the AutoQueue I believe

@Jester565
Copy link
Collaborator Author

@Jester565 Jester565 commented on 9b58b93 Nov 20, 2016 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CheezBoiger
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jester565 uh oh I think autoqueue didn't make it now that i looked.

@Jester565
Copy link
Collaborator Author

@Jester565 Jester565 commented on 9b58b93 Nov 20, 2016 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CheezBoiger
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jester565 Yea it's not threadsafe, don't worry I added it back in. I'll sync the pinger branch and let you know when to pull

@Jester565
Copy link
Collaborator Author

@Jester565 Jester565 commented on 9b58b93 Nov 20, 2016 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CheezBoiger
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no probs! You are set to go with pinger now!

Please sign in to comment.