Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
DannyVancura committed Dec 28, 2017
1 parent 4530f9f commit af9f021
Show file tree
Hide file tree
Showing 28 changed files with 1,560 additions and 7 deletions.
22 changes: 22 additions & 0 deletions Core/Connections/Connection.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// Connections.h
// FlightKit
//
// Created by Daniel Vancura on 12/27/17.
// Copyright © 2017 Daniel Vancura. All rights reserved.
//

#ifndef Connections_h
#define Connections_h

namespace Connection {
class Listener;
class Instance;

namespace Xplane {
class XplaneConnection;
class XplaneListener;
}
}

#endif /* Connections_h */
24 changes: 24 additions & 0 deletions Core/Connections/Instance.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// FlightSimConnection.hpp
// FlightKit
//
// Created by Daniel Vancura on 12/27/17.
// Copyright © 2017 Daniel Vancura. All rights reserved.
//

#ifndef FlightSimConnection_hpp
#define FlightSimConnection_hpp

#include <stdio.h>
#include "Connection.hpp"

class Connection::Instance {
public:
Connection::Listener *listener;

Instance(){}
virtual int establishConnection() = 0;
virtual void disconnect() = 0;
};

#endif
24 changes: 24 additions & 0 deletions Core/Connections/Listener.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// Listener.h
// FlightKit
//
// Created by Daniel Vancura on 12/27/17.
// Copyright © 2017 Daniel Vancura. All rights reserved.
//

#ifndef Listener_h
#define Listener_h

#include <stdio.h>
#include "Airplane.hpp"
#include "Instance.hpp"

class Connection::Listener {
public:
Listener(){}
virtual void didEstablishConnection(Connection::Instance *connection)=0;
virtual void didReceiveAirplaneData(Data::Airplane airplaneData)=0;
virtual void didReceiveOtherAircraft(Data::Airplane otherAircraft[])=0;
};

#endif /* Listener_h */
82 changes: 82 additions & 0 deletions Core/Connections/Xplane/XplaneConnection.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//
// Xplane.cpp
// FlightKit
//
// Created by Daniel Vancura on 12/27/17.
// Copyright © 2017 Daniel Vancura. All rights reserved.
//

#include "XplaneConnection.hpp"
#include "Listener.hpp"
#include <sys/socket.h>
#include <stdio.h>
#include <unistd.h>
#include <netdb.h>
#include <string>

Connection::Xplane::XplaneConnection::XplaneConnection() {

}

Connection::Xplane::XplaneConnection::XplaneConnection(int receivePort) {
this->receivePort = receivePort;
}

Connection::Xplane::XplaneConnection::~XplaneConnection() {
disconnect();
}

int Connection::Xplane::XplaneConnection::establishConnection() {
socketInfo = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

int optVal = 1;
int optionResult = setsockopt(socketInfo, SOL_SOCKET, SO_REUSEADDR, &optVal ,sizeof(int));
if (optionResult < 0) {
perror("Failed to set options.");
return optionResult;
}

if (socketInfo < 0) {
perror("Failed to create socket.");
return socketInfo;
}

memset((char *) &ownAddress, 0, sizeof(ownAddress));
ownAddress.sin_family = AF_INET;
ownAddress.sin_addr.s_addr = INADDR_ANY;
ownAddress.sin_port = htons(receivePort);

addrLen = sizeof(remoteAddress);

int bindResult = bind(socketInfo, (struct sockaddr *) &ownAddress, sizeof(ownAddress));
if (bindResult < 0) {
perror("Failed to bind to socket.");
return bindResult;
}
isActive = true;
std::thread receiveThread(&Connection::Xplane::XplaneConnection::receiveData, this);

receiveThread.detach();

if (listener) {
listener->didEstablishConnection(this);
}

return socketInfo;
}

void Connection::Xplane::XplaneConnection::receiveData() {
while (isActive) {
ssize_t bytesReceived = recvfrom(socketInfo, buffer, BUFFER_SIZE, 0, (struct sockaddr *) &remoteAddress, &addrLen);
if (bytesReceived > 0) {
buffer[bytesReceived] = 0;
} else {
perror("Failed to receive.");
}
}
}

void Connection::Xplane::XplaneConnection::disconnect() {
isActive = false;
close(socketInfo);
}
54 changes: 54 additions & 0 deletions Core/Connections/Xplane/XplaneConnection.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// Xplane.hpp
// FlightKit
//
// Created by Daniel Vancura on 12/27/17.
// Copyright © 2017 Daniel Vancura. All rights reserved.
//

#ifndef Xplane_hpp
#define Xplane_hpp

#include <stdio.h>
#include <netinet/in.h>
#include "Instance.hpp"
#include <thread>

#define BUFFER_SIZE 4096

class Connection::Xplane::XplaneConnection : public Connection::Instance {
private:
int socketInfo;
struct sockaddr_in ownAddress;
struct sockaddr_in remoteAddress;
socklen_t addrLen;
bool isActive = false;

unsigned char buffer[BUFFER_SIZE];

void receiveData();

public:
int receivePort = 49000;
Connection::Listener *listener;

XplaneConnection();
XplaneConnection(int receivePort);
~XplaneConnection();
int establishConnection();
void disconnect();

Connection::Xplane::XplaneConnection & operator= (const Connection::Xplane::XplaneConnection &other) {
if (this != &other) {
socketInfo = other.socketInfo;
ownAddress = other.ownAddress;
remoteAddress = other.remoteAddress;
addrLen = other.addrLen;
memcpy(&buffer, &other.buffer, BUFFER_SIZE);
receivePort = other.receivePort;
}
return *this;
}
};

#endif /* Xplane_hpp */
21 changes: 21 additions & 0 deletions Core/Connections/Xplane/XplaneListener.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// XplaneListener.cpp
// FlightKit
//
// Created by Daniel Vancura on 12/28/17.
// Copyright © 2017 Daniel Vancura. All rights reserved.
//

#include "XplaneListener.hpp"

void Connection::Xplane::XplaneListener::didEstablishConnection(Connection::Instance *connection) {
connectionCallback(connection);
}

void Connection::Xplane::XplaneListener::didReceiveAirplaneData(Data::Airplane airplaneData) {
airplaneDataCallback(airplaneData);
}

void Connection::Xplane::XplaneListener::didReceiveOtherAircraft(Data::Airplane otherAircraft[]) {
otherAircraftDataCallback(otherAircraft);
}
28 changes: 28 additions & 0 deletions Core/Connections/Xplane/XplaneListener.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// XplaneListener.hpp
// FlightKit
//
// Created by Daniel Vancura on 12/28/17.
// Copyright © 2017 Daniel Vancura. All rights reserved.
//

#ifndef XplaneListener_hpp
#define XplaneListener_hpp

#include <stdio.h>
#include "Connection.hpp"
#include "Listener.hpp"
#include <functional>

class Connection::Xplane::XplaneListener: public Connection::Listener {
public:
std::function<void(Connection::Instance *instance)> connectionCallback = {};
std::function<void(Data::Airplane airplaneData)> airplaneDataCallback = {};
std::function<void(Data::Airplane otherAircraft[])> otherAircraftDataCallback = {};

void didEstablishConnection(Connection::Instance *connection);
void didReceiveAirplaneData(Data::Airplane airplaneData);
void didReceiveOtherAircraft(Data::Airplane otherAircraft[]);
};

#endif /* XplaneListener_hpp */
13 changes: 13 additions & 0 deletions Core/Data/Airplane.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// Airplane.cpp
// FlightKit
//
// Created by Daniel Vancura on 12/27/17.
// Copyright © 2017 Daniel Vancura. All rights reserved.
//

#include "Airplane.hpp"

size_t Data::Airplane::numberOfEngines() {
return engines.size();
}
56 changes: 56 additions & 0 deletions Core/Data/Airplane.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// Airplane.hpp
// FlightKit
//
// Created by Daniel Vancura on 12/27/17.
// Copyright © 2017 Daniel Vancura. All rights reserved.
//

#ifndef Airplane_hpp
#define Airplane_hpp

#include <stdio.h>
#include <array>
#include "Engine.hpp"

class Data::Airplane {
public:
// MARK: - Airplane info
/// The airplane's callsign.
char *callsign;
char *name;

// MARK: - Position Data
double latitude;
double longitude;
double altitudeIndicatedAGL;
double altitudeIndicatedMSL;
double altitudeTrueAGL;
double altitudeTrueMSL;

// MARK: - Trajectory Data
double indicatedAirspeed;
double trueAirspeed;
double trueGroundspeed;
double magneticHeading;
double trueHeading;
double verticalVelocity;
double pitch;
double roll;
double headingTrue;
double headingMag;

// MARK: - Engines
std::array<Engine, 0> engines;

// MARK: - Functions

/**
Returns the number of engines on this aircraft.
@return Number of engines.
*/
size_t numberOfEngines();
};

#endif /* Airplane_hpp */
21 changes: 21 additions & 0 deletions Core/Data/Data.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Data.h
// FlightKit
//
// Created by Daniel Vancura on 12/27/17.
// Copyright © 2017 Daniel Vancura. All rights reserved.
//

#ifndef Data_h
#define Data_h

namespace Data {
class Airplane;
class Engine;

namespace Xplane {
class Buffer;
}
}

#endif /* Data_h */
9 changes: 9 additions & 0 deletions Core/Data/Engine.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//
// Engine.cpp
// FlightKit
//
// Created by Daniel Vancura on 12/27/17.
// Copyright © 2017 Daniel Vancura. All rights reserved.
//

#include "Engine.hpp"
Loading

0 comments on commit af9f021

Please sign in to comment.