summaryrefslogtreecommitdiffstats
path: root/servers/fpga_server_lin/src/fpga_conn.cpp
blob: 499d067da81d8bc36f0c5d58c0356022cad1b63f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * Remote Laboratory FPGA Server
 *
 * 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
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * (c) 2012 Timothy Pearson
 * Raptor Engineering
 * http://www.raptorengineeringinc.com
 */

#include <stdlib.h>

#include <klocale.h>

#include "fpga_conn.h"

/* exception handling */
struct exit_exception {
	int c;
	exit_exception(int c):c(c) { }
};

/*
  The FPGASocket class provides a socket that is connected with a client.
  For every client that connects to the server, the server creates a new
  instance of this class.
*/
FPGASocket::FPGASocket(int sock, TQObject *parent, const char *name) :
	TDEKerberosServerSocket(parent, name), m_criticalSection(0), m_stationID(-1), m_config(static_cast<FPGAServer*>(parent)->m_config), m_database(NULL), m_databaseStationsCursor(NULL) {

	setServiceName("remotefpga");

	line = 0;
	connect(this, SIGNAL(connectionClosed()), SLOT(connectionClosedHandler()));
	setSocket(sock);
}

FPGASocket::~FPGASocket() {
	//
}

void FPGASocket::close() {
	TDEKerberosServerSocket::close();
	connectionClosedHandler();
}

void FPGASocket::connectionClosedHandler() {
	printf("[DEBUG] Connection from %s closed\n\r", m_remoteHost.ascii());

	if (m_criticalSection > 0) {
		throw exit_exception(-1);
	}
}

int FPGASocket::initiateKerberosHandshake() {
	if (setUsingKerberos(true) == 0) {
		TQDataStream ds(this);
		ds << TQString("OK");

		return 0;
	}
	else {
		return -1;
	}
}

void FPGASocket::enterCommandLoop() {
	// RAJA FIXME
}

/*
  The FPGAServer class handles new connections to the server. For every
  client that connects, it creates a new FPGASocket -- that instance is now
  responsible for the communication with that client.
*/
FPGAServer::FPGAServer(TQObject* parent, int port, KSimpleConfig* config) :
	TQServerSocket( port, 1, parent ), m_config(config), m_numberOfConnections(0) {

	if ( !ok() ) {
		printf("[ERROR] Failed to bind to port %d\n\r", port);
		exit(1);
	}

	socketDevice()->setAddressReusable(false);

	printf("[INFO] Server started on port %d\n\r", port); fflush(stdout);
}

FPGAServer::~FPGAServer() {
	//
}

void FPGAServer::newConnection(int socket) {
	FPGASocket *s = new FPGASocket(socket, this);
	s->m_remoteHost = s->peerAddress().toString();
	printf("[DEBUG] New connection from %s\n\r", s->m_remoteHost.ascii());
	if (m_numberOfConnections > 0) {
		printf("[DEBUG] Connection from %s closed due to multiple access attempt\n\r", m_remoteHost.ascii());
		s->close();
		delete s;
		s = NULL;
	}
	if (s->initiateKerberosHandshake() != 0) {
		printf("[DEBUG] Connection from %s closed due to Kerberos failure\n\r", m_remoteHost.ascii());
		s->close();
		delete s;
		s = NULL;
	}
	else {
		m_numberOfConnections++;
		connect(s, SIGNAL(connectionClosed()), s, SLOT(deleteLater()));
		connect(s, SIGNAL(connectionClosed()), this, SLOT(remoteConnectionClosed()));
		emit newConnect(s);
		s->enterCommandLoop();
	}
}

void FPGAServer::remoteConnectionClosed() {
	m_numberOfConnections--;
}