Add FCP command interface.
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / fcp / Command.java
1 /*
2  * Sone - Command.java - Copyright © 2011 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.freenet.fcp;
19
20 import freenet.support.SimpleFieldSet;
21 import freenet.support.api.Bucket;
22
23 /**
24  * Implementation of an FCP interface for other clients or plugins to
25  * communicate with Sone.
26  *
27  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
28  */
29 public interface Command {
30
31         /**
32          * Executes the command, returning a reply that will be sent back to the
33          * requesting plugin.
34          *
35          * @param parameters
36          *            The parameters of the comand
37          * @param data
38          *            The data of the command (may be {@code null})
39          * @param accessType
40          *            The access type
41          * @return A reply to send back to the plugin
42          */
43         public Reply execute(SimpleFieldSet parameters, Bucket data, AccessType accessType);
44
45         /**
46          * The access type of the request.
47          *
48          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
49          */
50         public static enum AccessType {
51
52                 /** Access from another plugin. */
53                 DIRECT,
54
55                 /** Access via restricted FCP. */
56                 RESTRICTED_FCP,
57
58                 /** Access via FCP with full access. */
59                 FULL_FCP,
60
61         }
62
63         /**
64          * Interface for command replies.
65          *
66          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
67          */
68         public static class Reply {
69
70                 /** The reply parameters. */
71                 private final SimpleFieldSet replyParameters;
72
73                 /** The reply data, may be {@code null}. */
74                 private final byte[] data;
75
76                 /** The data bucket, may be {@code null}. */
77                 private final Bucket bucket;
78
79                 /**
80                  * Creates a new reply with the given parameters.
81                  *
82                  * @param replyParameters
83                  *            The reply parameters
84                  */
85                 public Reply(SimpleFieldSet replyParameters) {
86                         this(replyParameters, null, null);
87                 }
88
89                 /**
90                  * Creates a new reply with the given parameters.
91                  *
92                  * @param replyParameters
93                  *            The reply parameters
94                  * @param data
95                  *            The data of the reply (may be {@code null})
96                  */
97                 public Reply(SimpleFieldSet replyParameters, byte[] data) {
98                         this(replyParameters, data, null);
99                 }
100
101                 /**
102                  * Creates a new reply with the given parameters.
103                  *
104                  * @param replyParameters
105                  *            The reply parameters
106                  * @param bucket
107                  *            The bucket of the reply (may be {@code null})
108                  */
109                 public Reply(SimpleFieldSet replyParameters, Bucket bucket) {
110                         this(replyParameters, null, bucket);
111                 }
112
113                 /**
114                  * Creates a new reply with the given parameters.
115                  *
116                  * @param replyParameters
117                  *            The reply parameters
118                  * @param data
119                  *            The data of the reply (may be {@code null})
120                  * @param bucket
121                  *            The bucket of the reply (may be {@code null})
122                  */
123                 private Reply(SimpleFieldSet replyParameters, byte[] data, Bucket bucket) {
124                         this.replyParameters = replyParameters;
125                         this.data = data;
126                         this.bucket = bucket;
127                 }
128
129                 /**
130                  * Returns the reply parameters.
131                  *
132                  * @return The reply parameters
133                  */
134                 public SimpleFieldSet getReplyParameters() {
135                         return replyParameters;
136                 }
137
138                 /**
139                  * Returns whether the reply has reply data.
140                  *
141                  * @see #getData()
142                  * @return {@code true} if this reply has data, {@code false} otherwise
143                  */
144                 public boolean hasData() {
145                         return data != null;
146                 }
147
148                 /**
149                  * Returns the data of the reply.
150                  *
151                  * @return The data of the reply
152                  */
153                 public byte[] getData() {
154                         return data;
155                 }
156
157                 /**
158                  * Returns whether the reply has a data bucket.
159                  *
160                  * @see #getBucket()
161                  * @return {@code true} if the reply has a data bucket, {@code false}
162                  *         otherwise
163                  */
164                 public boolean hasBucket() {
165                         return bucket != null;
166                 }
167
168                 /**
169                  * Returns the data bucket of the reply.
170                  *
171                  * @return The data bucket of the reply
172                  */
173                 public Bucket getBucket() {
174                         return bucket;
175                 }
176
177         }
178
179 }