summaryrefslogtreecommitdiff
path: root/sources/server/NetCalendarServer.java
blob: 7d670faffa5125dc04a5f575b40e300caaa827c2 (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
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
/* NetCalendar 2006, 2009 (c) Dipl.-Inform. (FH) Paul C. Buetow
 * http://buetow.org - netcalendar@dev.buetow.org
 */

/**
 *
 */
package server;

import java.io.*;
import java.net.*;
import java.util.*;
import javax.net.*;
import javax.net.ssl.*;

import shared.*;
import shared.remotecall.*;

/**
 * This is the main class of the server part of the netcalendar suite.
 * It contains the server socket part and hold a references to the calendar database.
 * @author Paul C. Buetow
 */
public class NetCalendarServer extends Thread {
    private int iPort;
    private String sWorkdir;

    /**
     * Creates a NetCalendarServer instance and runs it as a thread.
     * @param iPort Specifies the server port of the server to be used.
     * @param sWorkdir Specifies the working directory of the server.
     */
    public NetCalendarServer(int iPort, String sWorkdir) {
        this.iPort = iPort;
        this.sWorkdir = sWorkdir;

        start();
    }

    /**
     * This method initializes a new server socket.
     * @return Returns the server socket object.
     */
    private ServerSocket makeServerSocket() throws IOException {
        if (!Config.getBooleanValue("use_ssl"))
            return new ServerSocket(iPort);

        ServerSocketFactory sslSocketFactory = SSLServerSocketFactory.getDefault();
        return sslSocketFactory.createServerSocket(iPort);
    }

    /**
     * This method specifies the start method of the Thread. Its setting up the server port.
     */
    public void run() {
        ServerSocket serverSocket = null;
        Socket socket = null;

        // Read and parse the whole calendar database from file!
        CalendarFormatParser parser = new CalendarFormatParser();
        parser.setWorkdir(sWorkdir);
        parser.start();

        CalendarDatabase calendarDatabase = new CalendarDatabase(parser.getCategories());

        try {
            serverSocket = makeServerSocket();

            while (true) {
                // This will wait for a connection to be made to this socket.
                socket = serverSocket.accept();
                // serverSocket.accept();
                socket.setKeepAlive(true);

                // Recieve the client's request object
                InputStream inputStream = socket.getInputStream();
                ObjectInput objectInput = new ObjectInputStream(inputStream);
                ClientRequest clientRequest = (ClientRequest) objectInput.readObject();

                if (!clientRequest.checkPassphrase(Config.getStringValue("passphrase", false))) {
                    Main.infoMessage("Server: Client refused, wrong passphrase!");

                    // Check if a event has been modified!
                } else if (clientRequest.requestsNewEvents()) {
                    Main.infoMessage("Server: Client requests new events");

                    // Get all calendar events which match the request
                    Vector vecEvents = calendarDatabase.getMatchingEvents(clientRequest);
                    ServerResponse serverResponse = new ServerResponse(vecEvents);

                    OutputStream outputStream = socket.getOutputStream();
                    ObjectOutput objectOutput = new ObjectOutputStream(outputStream);
                    objectOutput.writeObject(serverResponse);
                    objectOutput.flush();
                    objectOutput.close();

                } else if (clientRequest.actionIs(ClientRequest.MODIFY_EVENT)) {
                    Main.infoMessage("Server: Client wants to modify an event");
                    calendarDatabase.modifyEvent(clientRequest);
                    // if (Config.getBooleanValue("client_run"))
                    calendarDatabase.flush();

                } else if (clientRequest.actionIs(ClientRequest.DELETE_EVENT)) {
                    Main.infoMessage("Server: Client wants to delete an event");
                    calendarDatabase.deleteEvent(clientRequest);
                    // if (Config.getBooleanValue("client_run"))
                    calendarDatabase.flush();

                } else if (clientRequest.actionIs(ClientRequest.ADD_EVENT)) {
                    Main.infoMessage("Server: Client wants to add an event");
                    calendarDatabase.addEvent(clientRequest);
                    // if (Config.getBooleanValue("client_run"))
                    calendarDatabase.flush();

                } else if (clientRequest.actionIs(ClientRequest.RENAME_CATEGORY)) {
                    Main.infoMessage("Server: Client wants to rename a category");
                    calendarDatabase.renameCategory(clientRequest);
                    // if (Config.getBooleanValue("client_run"))
                    calendarDatabase.flush();

                } else if (clientRequest.actionIs(ClientRequest.DELETE_CATEGORY)) {
                    Main.infoMessage("Server: Client wants to delete a category (NYI)");
                    calendarDatabase.deleteCategory(clientRequest);
                    // if (Config.getBooleanValue("client_run"))
                    calendarDatabase.flush();

                } else if (clientRequest.actionIs(ClientRequest.RELOAD_DATABASE)) {
                    // Read and parse the whole calendar database from file!
                    parser = new CalendarFormatParser();
                    parser.setWorkdir(sWorkdir);
                    parser.start();

                    calendarDatabase = new CalendarDatabase(parser.getCategories());

                } else if (clientRequest.actionIs(ClientRequest.FLUSH_DATABASE)) {
                    Main.infoMessage("Server: Client wants to flush the database");
                    calendarDatabase.flush();

                } else if (clientRequest.actionIs(ClientRequest.SHUTDOWN_SERVER)) {
                    Main.infoMessage("Server: Client wants the server to shut down");
                    calendarDatabase.flush();
                    socket.close();

                    if (Config.getBooleanValue("client_run")) {
                        Main.infoMessage("Server: Shutting down the server thread");
                        socket.close();
                        serverSocket.close();
                        break;
                    } else {
                        Main.exit(0);
                    }
                }

                socket.close();
            }

        } catch (ClassNotFoundException e) {
            Main.infoMessage("Error: Server error during serialization: " + e.getMessage());

        } catch (IOException e) {
            Main.infoMessage("Error: Server error during serialization: " + e.getMessage());
        }
    }
}