-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSystem.java
More file actions
171 lines (148 loc) · 5.37 KB
/
System.java
File metadata and controls
171 lines (148 loc) · 5.37 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package server;
//region LIBRARIES USED
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
//endregion
/**
* @author Paul Johannes Aru
* @version 0.9
* @since 2021-03-16
*/
public class System {
//region SYSTEM PARAMETERS
//Constants:
private static final String DATABASE_NAME = "flight-data.sqlite";
//Variables:
private static List<Integer> reservedRouteIDs;
//endregion
//region SQL ACCESS
/**
* Establishes a Connection to SQLite Database.
* <p>
* Attempts to Establish a Connection to the SQLite
* Database. Database File Name is Specified in the
* Class Constant called DATABASE_NAME.
*
* @return SQLite Connection.
*/
private static Connection accessDatabase() throws SQLException {
Connection conn = DriverManager.getConnection(("jdbc:sqlite:database/"+DATABASE_NAME), "", "");
java.lang.System.out.println("Accessing Database "+DATABASE_NAME+"...");
return conn;
}
//endregion
//region DATABASE PULL DATA
/**
* Request Information from the Database.
* <p>
* Given an SQL Command in String format, the
* method returns an ArrayList of Rows, that in
* and on itself is an ArrayList of String Cells,
* corresponding to the Columns Requested.
*
* @param sqlCommand SQL Command String.
* @return An ArrayList Matrix of String Components.
*/
public synchronized static ArrayList<ArrayList<String>> databasePull(String sqlCommand) {
ArrayList<ArrayList<String>> fetchedData = new ArrayList<ArrayList<String>>();
long logTime = java.lang.System.currentTimeMillis();
int logItems = 0;
try (Connection conn = accessDatabase();
PreparedStatement prep = conn.prepareStatement(sqlCommand)) {
ResultSet dataLine = prep.executeQuery();
int columns = dataLine.getMetaData().getColumnCount()+1;
while (dataLine.next()) {
ArrayList<String> row = new ArrayList<>();
for (int column=1;column<columns;column++) {
row.add(dataLine.getString(column));
}
fetchedData.add(row);
logItems++;
}
} catch (SQLException ex) {
Logger.getLogger(System.class.getName()).log(Level.SEVERE, null, ex);
}
java.lang.System.out.println("Done. Fetched "+logItems+" items in "+(java.lang.System.currentTimeMillis()-logTime)+"ms!");
return fetchedData;
}
//endregion
//region DATABASE PUSH DATA
/**
* Dispatch Information to the Database.
* <p>
* Given an SQL Command in String format, the
* method can either add or modify Database
* information.
*
* @param sqlCommand SQL Command String.
*/
public synchronized static void databasePush(String sqlCommand) {
try (Connection conn = accessDatabase(); // auto close the connection object after try
PreparedStatement prep = conn.prepareStatement(sqlCommand)) {
prep.execute();
} catch (SQLException ex) {
Logger.getLogger(System.class.getName()).log(Level.SEVERE, null, ex);
}
}
//endregion
//region SERVER COMMUNICATIONS
/**
* Endless Loop that Handles Client-Thread Assignment.
* <p>
* Opens a new Socket for the Server. When a Client
* Connects, a New Thread will be Created to handle
* the client and the Server returns to Waiting for
* New Clients.
*/
@SuppressWarnings("InfiniteLoopStatement")
private static void runServer() {
java.lang.System.out.println("Server: Launched System.");
reservedRouteIDs = new ArrayList<>();
try (ServerSocket serverSocket = new ServerSocket(2000)) {
//noinspection InfiniteLoopStatement
while (true) {
java.lang.System.out.println("Server: Ready for Clients.");
try {
Socket socket = serverSocket.accept();
SystemThread clientThread = new SystemThread(socket);
Thread connectionThread = new Thread(clientThread);
connectionThread.start();
} catch (IOException ex) {
java.lang.System.out.println("ERROR: Server Failed to Connect to a Client!!!");
}
}
} catch (IOException ex) {
Logger.getLogger(System.class.getName()).log(Level.SEVERE, null, ex);
java.lang.System.out.println("Server: Halted System.");
}
}
//endregion
//region PARAMETER ACCESS METHODS
/**
* Returns Reserved Route IDs.
*
* @return ID Integer List.
*/
public synchronized static List<Integer> returnReservedRouteIDs() {return reservedRouteIDs;}
/**
* Reserves a Route ID.
*
* @param id ID Integer.
*/
public synchronized static void reserveRouteID(int id) {reservedRouteIDs.add(id);}
//endregion
// MAIN:
public static void main(@SuppressWarnings("CStyleArrayDeclaration") String args[]) {
runServer();
}
}