Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions GS ACK Request/gsserver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#include "gsserver.h"
#include <errno.h>
#include <signal.h>
#include <iostream>
#include <string>

#ifdef _WIN32
#include <winsock.h>
#include <windows.h>

#include<stdio.h>
#include<string.h>
#include<winsock2.h>
#include<Ws2tcpip.h>
#include<signal.h>

#ifndef INET6_ADDRSTRLEN
#define INET6_ADDRSTRLEN 46
#endif

void GsServer::waitNet(unsigned millis){
Sleep(millis);
}
#else
#include <unistd.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/wait.h>
void GsServer::waitNet(unsigned millis){
usleep(millis * 1000);
}
#endif


GsServer::GsServer(messagebox *myMessageBox): networkListener(this,myMessageBox){
this->myMessageBox = myMessageBox;
port = 27015;
target = QHostAddress::LocalHost;
}

GsServer::~GsServer(){

}

void GsServer::openServer(){
running = true;
}

void GsServer::openServer(QHostAddress target, unsigned int port){
this->target = target;
this->port = port;
openServer();
}

void GsServer::startServer(){
std::cout << "Server starting..." << std::endl;
if (running == false){
std::cout << "Need to open server before it can start" << std::endl;
return;
}
this->start();
networkListener.start();
}

void GsServer::closeServer(){

}

void GsServer::run(){
running = true;

Protocol::ActionPacket setHome;
setHome.SetAction(Protocol::ActionType::SetHome);

Protocol::ActionPacket start;
start.SetAction(Protocol::ActionType::Start);

sendPacket(&setHome,0);
sendPacket(&start,1);

while (running){
if (!outPackets.isEmpty()){
sendNextPacket();
}
QThread::msleep(2000);
}
}

void GsServer::sendPacket(Protocol::Packet* packet){
sendPacket(packet,DEFAULT_PRIORITY);
}

void GsServer::sendPacket(Protocol::Packet* packet, unsigned int priority){
outPackets.enqueue(packet,priority);
}

void GsServer::sendNextPacket() {
printf("sending message!\n");

Protocol::Packet* packet = outPackets.getNextPacket();
QByteArray datagram;
QDataStream out(&datagram, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_3);

// Allocate storage for the packet in the for of u_int8_t
unsigned char storage[PACKET_LENGTH];

// Convert the packet into bytes and store into storage
size_t packet_size = packet->GetBytes(storage, PACKET_LENGTH);

// Send bytes inside storage to out datastream
for(size_t i =0; i < packet_size; i++) {
out << storage[i];
}

// Send datagram through UDP socket
outSocket.writeDatagram(datagram, target, port);


//TEST CODE TO SIMULATE INCOMING ACK PACKETS
//COMMENT OUT TO USE ACTUAL ACK PACKETS

///@todo implement actual ack packet transfer from message box to outPackets.recievAckPacket(AckPacket)

Protocol::AckPacket *ack = new Protocol::AckPacket;
ack->set_timestamp(packet->get_timestamp());
outPackets.recieveAckPacket(ack);

//END TEST CODE
}
146 changes: 146 additions & 0 deletions GS ACK Request/gsserver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#ifndef GSSERVER_H
#define GSSERVER_H

#include <QQueue>
#include <QPair>
#include <QThread>
#include <QUdpSocket>
#include "serverqueue.h"
//#include "net.h"
#include "networklistener.h"
#include "messagebox.h"

#define DEFAULT_PRIORITY 10

const static QString UAV_IP_ADDRESS = "localhost";


/**
GsServer is the server object for the ground station. The current
implementation will be changing, but this is the first working version.

I will be cleaning the code and making frequent implementation changes in
the near future. The functions and variables however will probably remain
stable.

@author Jordan Dickson
@date 1-29-1016
@version 2.0
*/
class GsServer : public QThread{
//Q_OBJECT
public:
/**
Creates a new server hosted on the localhost (127.0.0.1) on port 3495.

@param myMessageBox the messagebox used for outgoing packets
*/
GsServer(messagebox *myMessageBox);

/**
Destructor for GsServer
*/
~GsServer();

/**
Override run to implement thread as a server.
*/
void run();

void startServer();

/**
Opens ther server by hosting it on the default port and listening for
connections.
*/
void openServer();

/**
Opens ther server by hosting it on a socket and listening for
connections.
*/
void openServer(QHostAddress target, unsigned int port);

/**
Closes the server by dropping all current connections and ending the
listener thread.
*/
void closeServer();

/**
waits for a specified number of milliseconds

@param millis the number of milliseconds to wait
*/
void waitNet(unsigned millis);

/**
* @brief sendPacket adds a packet to the send queue for this server. The
* default priority is 10. The packet will be sent as soon as the socket
* is free for sending. Lowest prioirty is sent first.
* @param packet the packet to be added into the send queue
* @author Jordan Dickson
* @date Feb 5 2016
*/
void sendPacket(Protocol::Packet* packet);

/**
* @brief sendPacket adds a packet to the send queue for this server. The
* packet will be sent as soon as the socket is free for sending. Lowest
* priority is sent first.
* @param packet the packet to be added into the send queue
* @param priority the priority of the packet (lowest number sent first)
* @date Feb 5 2016
*/
void sendPacket(Protocol::Packet* packet, unsigned int priority);

/**
file descriptor for the UAV. Used to send information over the network.
*/
int uav_fd;

/**
the maximum size of a packet that will be sent by this server.
*/
unsigned char maxPackSize = 255;

/**
The NetworkListener used by this server to process incoming signals.
*/
NetworkListener networkListener;

private:

bool running;

/* Alvin Truong added on 16-1-27*/
const static int PACKET_LENGTH = 1000;
const static int SEND_PORT = 20715;
const static int LISTEN_PORT = 20715;
static int NUM_RECV_PACKETS;


/**
Stores the port number of this server.
*/
unsigned short port;

/**
Sends the most significant datagram.
*/
void sendNextPacket();

/**
* @brief myMessageBox is the messagebox from mapexecution that will be used for
* outgoing packets.
*/
messagebox *myMessageBox;

QUdpSocket outSocket;

QHostAddress target;

ServerQueue outPackets;
};
#endif // GSSERVER_H

Loading