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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// create_options.rs
//
// The set of options for creating an MQTT client.
// This file is part of the Eclipse Paho MQTT Rust Client library.
//

/*******************************************************************************
 * Copyright (c) 2017 Frank Pagliughi <fpagliughi@mindspring.com>
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v1.0 which accompany this distribution.
 *
 * The Eclipse Public License is available at
 *    http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 *   http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *    Frank Pagliughi - initial implementation and documentation
 *******************************************************************************/

use std::fmt;

use ffi;
use client_persistence::ClientPersistence;

/*
Remember the C constants (c_uint)
  MQTTCLIENT_PERSISTENCE_DEFAULT = 0
  MQTTCLIENT_PERSISTENCE_NONE    = 1
  MQTTCLIENT_PERSISTENCE_USER    = 2
*/

pub enum PersistenceType {
	File,
	None,
	User(Box<Box<ClientPersistence>>),
}

impl fmt::Debug for PersistenceType {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		match *self {
			PersistenceType::File => write!(f, "File"),
			PersistenceType::None => write!(f, "None"),
			PersistenceType::User(_) => write!(f, "User"),
		}
	}
}

/////////////////////////////////////////////////////////////////////////////
//							Create Options
/////////////////////////////////////////////////////////////////////////////

/**
 * The options for creating an MQTT client.
 */
#[derive(Debug)]
pub struct CreateOptions {
	pub copts: ffi::MQTTAsync_createOptions,
	pub server_uri: String,
	pub client_id: String,
	pub persistence: PersistenceType,
}

impl CreateOptions {
	pub fn new() -> CreateOptions {
		CreateOptions::default()
	}
}

impl<'a> From<&'a str> for CreateOptions {
	fn from(server_uri: &'a str) -> Self {
		let mut opts = CreateOptions::default();
		opts.server_uri = server_uri.to_string();
		opts
	}
}

impl<'a, 'b> From<(&'a str, &'b str)> for CreateOptions {
	fn from((server_uri, client_id): (&'a str, &'b str)) -> Self {
		let mut opts = CreateOptions::default();
		opts.server_uri = server_uri.to_string();
		opts.client_id = client_id.to_string();
		opts
	}
}

impl Default for CreateOptions {
	fn default() -> CreateOptions {
		CreateOptions {
			copts: ffi::MQTTAsync_createOptions::default(),
			server_uri: "".to_string(),
			client_id: "".to_string(),
			persistence: PersistenceType::File,
		}
	}
}

/////////////////////////////////////////////////////////////////////////////
//								Builder
/////////////////////////////////////////////////////////////////////////////

pub struct CreateOptionsBuilder {
	copts: ffi::MQTTAsync_createOptions,
	server_uri: String,
	client_id: String,
	persistence: PersistenceType,
}

impl CreateOptionsBuilder {
	pub fn new() -> CreateOptionsBuilder {
		CreateOptionsBuilder {
			copts: ffi::MQTTAsync_createOptions::default(),
			server_uri: "".to_string(),
			client_id: "".to_string(),
			persistence: PersistenceType::File,
		}
	}

	pub fn server_uri<S>(mut self, server_uri: S) -> CreateOptionsBuilder
			where S: Into<String> {
		self.server_uri = server_uri.into();
		self
	}

	pub fn client_id<S>(mut self, client_id: S) -> CreateOptionsBuilder
			where S: Into<String> {
		self.client_id = client_id.into();
		self
	}

	pub fn persistence(mut self, persist: PersistenceType) -> CreateOptionsBuilder {
		self.persistence = persist;
		self
	}

	pub fn user_persistence<T>(mut self, persistence: T) -> CreateOptionsBuilder 
			where T: ClientPersistence + 'static
	{
		let persistence: Box<Box<ClientPersistence>> = Box::new(Box::new(persistence));
		self.persistence = PersistenceType::User(persistence);
		self
	}

	pub fn max_buffered_messages(mut self, n: i32) -> CreateOptionsBuilder {
		self.copts.maxBufferedMessages = n;
		self.copts.sendWhileDisconnected = if n == 0 { 0 } else { 1 };
		self
	}

	pub fn finalize(self) -> CreateOptions {
		CreateOptions {
			copts: self.copts,
			server_uri: self.server_uri,
			client_id: self.client_id,
			persistence: self.persistence,
		}
	}
}

/////////////////////////////////////////////////////////////////////////////
//								Unit Tests
/////////////////////////////////////////////////////////////////////////////

#[cfg(test)]
mod tests {
	use super::*;

	// Rust options should be the same as the C options
	#[test]
	fn test_default() {
		let opts = CreateOptions::default();
		let copts = ffi::MQTTAsync_createOptions::default();

		// First, make sure C options valid
		assert_eq!([ 'M' as i8, 'Q' as i8, 'C' as i8, 'O' as i8], copts.struct_id);
		assert_eq!(0, copts.struct_version);	// Currently supported version

		assert_eq!(copts.struct_id, opts.copts.struct_id);
		assert_eq!(copts.struct_version, opts.copts.struct_version);
		assert_eq!(copts.sendWhileDisconnected, opts.copts.sendWhileDisconnected);
		assert_eq!(copts.maxBufferedMessages, opts.copts.maxBufferedMessages);

		assert_eq!("", &opts.server_uri);
		assert_eq!("", &opts.client_id);
		//assert_eq!(PersistenceType::File, opts.persistence);
	}

	#[test]
	fn test_from_string() {
		const HOST: &'static str = "localhost";
		let opts = CreateOptions::from(HOST);
		let copts = ffi::MQTTAsync_createOptions::default();

		assert_eq!([ 'M' as i8, 'Q' as i8, 'C' as i8, 'O' as i8], opts.copts.struct_id);
		assert_eq!(0, opts.copts.struct_version);	// Currently supported version
		assert_eq!(copts.sendWhileDisconnected, opts.copts.sendWhileDisconnected);
		assert_eq!(copts.maxBufferedMessages, opts.copts.maxBufferedMessages);

		assert_eq!(HOST, &opts.server_uri);
		assert_eq!("", &opts.client_id);
		//assert_eq!(PersistenceType::File, opts.persistence);
	}


	#[test]
	fn test_from_tuple() {
		const HOST: &'static str = "localhost";
		const ID: &'static str = "bubba";
		let opts = CreateOptions::from((HOST,ID));
		let copts = ffi::MQTTAsync_createOptions::default();

		assert_eq!([ 'M' as i8, 'Q' as i8, 'C' as i8, 'O' as i8], opts.copts.struct_id);
		assert_eq!(0, opts.copts.struct_version);	// Currently supported version
		assert_eq!(copts.sendWhileDisconnected, opts.copts.sendWhileDisconnected);
		assert_eq!(copts.maxBufferedMessages, opts.copts.maxBufferedMessages);

		assert_eq!(HOST, &opts.server_uri);
		assert_eq!(ID, &opts.client_id);
		//assert_eq!(PersistenceType::File, opts.persistence);
	}

	#[test]
	fn test_defaultbuilder() {
		let opts = CreateOptionsBuilder::new().finalize();
		let copts = ffi::MQTTAsync_createOptions::default();

		// First, make sure C options valid
		assert_eq!([ 'M' as i8, 'Q' as i8, 'C' as i8, 'O' as i8], copts.struct_id);
		assert_eq!(0, copts.struct_version);	// Currently supported version

		assert_eq!(copts.struct_id, opts.copts.struct_id);
		assert_eq!(copts.struct_version, opts.copts.struct_version);
		assert_eq!(copts.sendWhileDisconnected, opts.copts.sendWhileDisconnected);
		assert_eq!(copts.maxBufferedMessages, opts.copts.maxBufferedMessages);

		assert_eq!("", &opts.server_uri);
		assert_eq!("", &opts.client_id);
		//assert_eq!(PersistenceType::File, opts.persistence);
	}

	#[test]
	fn test_builder() {
		const HOST: &'static str = "localhost";
		const ID: &'static str = "bubba";
		const MAX_BUF_MSGS: i32 = 100;

		let opts = CreateOptionsBuilder::new()
						.server_uri(HOST)
						.client_id(ID)
						// TODO: Test persistence
						.max_buffered_messages(MAX_BUF_MSGS)
						.finalize();

		assert_eq!([ 'M' as i8, 'Q' as i8, 'C' as i8, 'O' as i8], opts.copts.struct_id);
		assert_eq!(0, opts.copts.struct_version);	// Currently supported version

		assert_eq!(HOST, &opts.server_uri);
		assert_eq!(ID, &opts.client_id);
		//assert_eq!(PersistenceType::File, opts.persistence);
		assert!(0 != opts.copts.sendWhileDisconnected);
		assert_eq!(MAX_BUF_MSGS, opts.copts.maxBufferedMessages);
	}
}