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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/*!
The components required by a plugin host to load/unload plugins.

The primary component of the plugin host's interaction is the [`PluginManager`](struct.PluginManager.html);
this type manages the lifecycle of plugins, as well as opening and closing the necessary dynamic
libraries.

# Example

As the example below shows, the plugin manager is relatively simple in it's interface. However,
as any more complex host may require loading multiple libraries, and different types of plugins,
the [`PluginManagerConfiguration`](../config/struct.PluginManagerConfiguration.html) type is a
higher-level abstraction.

```rust,no_run
use dygpi::manager::PluginManager;
use dygpi::plugin::Plugin;
use std::sync::Arc;

# const EFFECT_PLUGIN_ID: &str = "sound_effects";
# #[derive(Debug)]
# struct SoundEffectPlugin;
# impl Plugin for SoundEffectPlugin {
#     fn plugin_id(&self) -> &String {
#         unimplemented!()
#     }
#     fn on_load(&self) -> dygpi::error::Result<()> { Ok(()) }
#     fn on_unload(&self) -> dygpi::error::Result<()> { Ok(()) }
# }
# impl SoundEffectPlugin {
#     pub fn play(&self) {}
# }
let mut plugin_manager: PluginManager<SoundEffectPlugin> = PluginManager::default();

plugin_manager
    .load_plugins_from("libsound_one.dylib".as_ref())
    .unwrap();

let plugin: Arc<SoundEffectPlugin> = plugin_manager
    .get("sound_one::sound_one::DelayEffect")
    .unwrap();

println!("{}", plugin.plugin_id());

plugin.play();
```

*/

use crate::error::{Error, ErrorKind, Result};
use crate::plugin::{
    compatibility_hash, CompatibilityFn, Plugin, PluginRegistrar, PluginRegistrationFn,
    COMPATIBILITY_FN_NAME, PLUGIN_REGISTRATION_FN_NAME,
};
use libloading::{Library, Symbol};
use search_path::SearchPath;
use std::collections::HashMap;
use std::env;
use std::ffi::OsString;
use std::path::{Path, PathBuf};
use std::sync::{Arc, RwLock};

// ------------------------------------------------------------------------------------------------
// Public Types
// ------------------------------------------------------------------------------------------------

///
/// The plugin manager loads and unloads plugins from a library which is dynamically opened and
/// closed as necessary.
///
#[derive(Debug)]
pub struct PluginManager<T>
where
    T: Plugin,
{
    search_path: SearchPath,
    registration_fn_name: Vec<u8>,
    plugins: RwLock<HashMap<String, LoadedPlugin<T>>>,
}

#[cfg(target_os = "macos")]
/// File name extension commonly used for a dynamic library.
pub const PLATFORM_DYLIB_EXTENSION: &str = "dylib";

#[cfg(target_os = "linux")]
/// File name extension commonly used for a dynamic library.
pub const PLATFORM_DYLIB_EXTENSION: &str = "so";

#[cfg(target_os = "windows")]
/// File name extension commonly used for a dynamic library.
pub const PLATFORM_DYLIB_EXTENSION: &str = "dll";

#[cfg(target_os = "windows")]
/// Prefix for dynamic libraries, if any.
pub const PLATFORM_DYLIB_PREFIX: &str = "";

#[cfg(not(target_os = "windows"))]
/// Prefix for dynamic libraries, if any.
pub const PLATFORM_DYLIB_PREFIX: &str = "lib";

// ------------------------------------------------------------------------------------------------
// Private Types
// ------------------------------------------------------------------------------------------------

#[derive(Clone, Debug)]
struct LoadedPlugin<T>
where
    T: Plugin,
{
    plugin: Arc<T>,
    in_library: Arc<LoadedLibrary>,
}

#[derive(Debug)]
struct LoadedLibrary {
    file_name: PathBuf,
    library: Library,
}

// ------------------------------------------------------------------------------------------------
// Public Functions
// ------------------------------------------------------------------------------------------------

///
/// Given a file name, or path with a file name, return a new path that formats the file name
/// according to common platform conventions. `PluginManager` does not use this function directly,
/// it is up to the client to determine whether to use this before passing a file path to the
/// manager.
///
/// # Example
///
/// The following will return "`libplugins.dylib`" on macos, "`libplugins.so`" on linux, and
/// "`plugins.dll`" on windows.
///
/// ```rust
/// use dygpi::manager::make_platform_dylib_name;
///
/// let dylib_name = make_platform_dylib_name("plugins".as_ref());
/// ```
///
/// If the file name appears to have an extension it will be overwritten by the platform extension.
/// So, the following will replace "`foo`" with the platform extension.
///
/// ```rust
/// use dygpi::manager::make_platform_dylib_name;
///
/// let dylib_name = make_platform_dylib_name("plugins/aplugin.foo".as_ref());
/// ```
///
pub fn make_platform_dylib_name(file_path: &Path) -> PathBuf {
    if let Some(file_stem) = file_path.file_stem() {
        let file_name = if !PLATFORM_DYLIB_PREFIX.is_empty() {
            let mut prefixed = OsString::from(PLATFORM_DYLIB_PREFIX);
            prefixed.push(file_stem);
            prefixed
        } else {
            file_stem.to_os_string()
        };
        let mut file_path = file_path.to_path_buf();
        file_path.set_file_name(file_name);
        let _ = file_path.set_extension(PLATFORM_DYLIB_EXTENSION);
        file_path
    } else {
        file_path.to_path_buf()
    }
}

// ------------------------------------------------------------------------------------------------
// Implementations
// ------------------------------------------------------------------------------------------------

const UTF8_STRING_PANIC: &str = "Invalid UTF8 symbol name when converting to string";

// ------------------------------------------------------------------------------------------------

impl<T> Default for PluginManager<T>
where
    T: Plugin,
{
    fn default() -> Self {
        Self {
            search_path: Default::default(),
            registration_fn_name: PLUGIN_REGISTRATION_FN_NAME.to_vec(),
            plugins: Default::default(),
        }
    }
}

impl<T> Drop for PluginManager<T>
where
    T: Plugin,
{
    fn drop(&mut self) {
        info!("PluginManager::drop()");
        self.unload_all().unwrap();
    }
}

impl<T> PluginManager<T>
where
    T: Plugin,
{
    ///
    /// Construct a new plugin manager and have it use the values of the string slice
    /// as a search path when loading libraries.
    ///
    pub fn new_with_search_path(search_path: SearchPath) -> Self {
        Self {
            search_path,
            registration_fn_name: PLUGIN_REGISTRATION_FN_NAME.to_vec(),
            plugins: Default::default(),
        }
    }

    ///
    /// Load all plugins from the libraries that are specified in the named environment variable.
    ///
    /// The environment variable's value is assumed to be a list of paths separated by the colon,
    /// `':'` character.
    ///
    pub fn load_all_plugins_from_env(&mut self, env_var: &str) -> Result<()> {
        info!("PluginManager::load_all_plugins_from_env({:?})", env_var);
        if let Ok(env_value) = env::var(env_var) {
            for file_name in env_value.split(":") {
                self.load_plugins_from(&PathBuf::from(file_name))?;
            }
        } else {
            warn!("Failed to find environment variable '{}'", env_var);
        }
        Ok(())
    }

    ///
    /// Load all plugins from the libraries specified in the string slice, each value is a file path.
    ///
    pub fn load_plugins_from_all(&mut self, file_names: &[&Path]) -> Result<()> {
        info!("PluginManager::load_all_plugins_from({:?})", file_names);
        for file_name in file_names {
            self.load_plugins_from(file_name)?;
        }
        Ok(())
    }

    ///
    /// Load all plugins from a single library with the provided file name/path.
    ///
    #[allow(unsafe_code)]
    pub fn load_plugins_from(&mut self, file_name: &Path) -> Result<()> {
        info!("PluginManager::load_plugins_from({:?})", file_name);

        let file_name = if (file_name.is_absolute() || file_name.parent().is_some())
            && !self.search_path.is_empty()
        {
            self.find_library(file_name)
        } else {
            file_name.to_path_buf()
        };

        trace!("PluginManager::load_plugins_from() > opening library");
        let library = unsafe {
            Library::new(&file_name).map_err(|e| {
                Error::from(ErrorKind::LibraryOpenFailed(
                    file_name.to_string_lossy().to_string(),
                    Box::new(e),
                ))
            })?
        };

        let loaded_library = LoadedLibrary { file_name, library };

        trace!("PluginManager::load_plugins_from() > checking compatibility");
        self.check_compatibility(&loaded_library)?;

        trace!("PluginManager::load_plugins_from() > registering the plugins");
        self.register_plugins(loaded_library)?;

        Ok(())
    }

    ///
    /// Override the default registration function name
    /// [`PLUGIN_REGISTRATION_FN_NAME`](../plugin/const.PLUGIN_REGISTRATION_FN_NAME.html).
    ///
    /// This function **must** conform to the type
    /// [`PluginRegistrationFn`](../plugin/function.PluginRegistrationFn.html), and must be marked
    /// as `#[no_mangle] pub extern "C"` in the same manner as the standard registration function.
    ///
    /// # Example
    ///
    /// ```rust
    /// use dygpi::plugin::{Plugin, PluginRegistrar};
    /// # #[derive(Debug)]
    /// # struct SoundSourcePlugin;
    /// # impl Plugin for SoundSourcePlugin {
    /// #     fn plugin_id(&self) -> &String {
    /// #         unimplemented!()
    /// #     }
    /// #     fn on_load(&self) -> dygpi::error::Result<()> { Ok(()) }
    /// #     fn on_unload(&self) -> dygpi::error::Result<()> { Ok(()) }
    /// # }
    /// # impl SoundSourcePlugin {
    /// #     pub fn new(id: &str) -> Self { Self {} }
    /// # }
    /// # #[derive(Debug)]
    /// # struct SoundEffectPlugin;
    /// # impl Plugin for SoundEffectPlugin {
    /// #     fn plugin_id(&self) -> &String {
    /// #         unimplemented!()
    /// #     }
    /// #     fn on_load(&self) -> dygpi::error::Result<()> { Ok(()) }
    /// #     fn on_unload(&self) -> dygpi::error::Result<()> { Ok(()) }
    /// # }
    /// # impl SoundEffectPlugin {
    /// #     pub fn new(id: &str) -> Self { Self {} }
    /// # }
    /// # const PLUGIN_NAME: &str = "RandomSource";
    /// # const OTHER_PLUGIN_NAME: &str = "DelayEffect";
    ///
    /// #[no_mangle]
    /// pub extern "C" fn register_sources(registrar: &mut PluginRegistrar<SoundSourcePlugin>) {
    ///     registrar.register(SoundSourcePlugin::new(PLUGIN_NAME));
    /// }
    ///
    /// #[no_mangle]
    /// pub extern "C" fn register_effects(registrar: &mut PluginRegistrar<SoundEffectPlugin>) {
    ///     registrar.register(SoundEffectPlugin::new(OTHER_PLUGIN_NAME));
    /// }
    /// ```
    ///
    pub fn set_registration_fn_name(&mut self, name: &[u8]) {
        self.registration_fn_name = name.to_vec()
    }

    ///
    /// Returns `true` if the plugin manager has no plugins registered, else `false`.
    ///
    pub fn is_empty(&self) -> bool {
        self.plugins.read().unwrap().is_empty()
    }

    ///
    /// Return the number of plugins registered in this plugin manager.
    ///
    pub fn len(&self) -> usize {
        self.plugins.read().unwrap().len()
    }

    ///
    /// Returns `true` if this plugin manager has a registered plugin with the provided plugin
    /// identifier, else `false`.
    pub fn contains(&self, plugin_id: &str) -> bool {
        let plugins = self.plugins.read().unwrap();
        plugins.contains_key(plugin_id)
    }

    ///
    /// Returns the plugin with the provided plugin identifier, if one exists, else `None`.
    pub fn get(&self, plugin_id: &str) -> Option<Arc<T>> {
        let plugins = self.plugins.read().unwrap();
        plugins.get(plugin_id).map(|p| p.plugin.clone())
    }

    ///
    /// Return all the plugins registered in this plugin manager as a vector.
    ///
    pub fn plugins(&self) -> Vec<Arc<T>> {
        let plugins = self.plugins.read().unwrap();
        plugins.values().map(|p| p.plugin.clone()).collect()
    }

    ///
    /// Unload all plugins, and associated libraries, that are currently registered in this
    /// plugin manager.
    ///
    pub fn unload_all(&mut self) -> Result<()> {
        info!("PluginManager::unload_all()");
        let plugin_names: Vec<String> = {
            let plugins = self.plugins.write().unwrap();
            plugins.iter().map(|(n, _)| n).cloned().collect()
        };
        for name in plugin_names {
            self.unload_plugin(&name)?;
        }
        Ok(())
    }

    ///
    /// Unload the plugin identified by the provided plugin identifier, if one exists. Note that
    /// this method will also close the plugin library if no other plugins are using it.
    ///
    pub fn unload_plugin(&mut self, plugin_name: &str) -> Result<()> {
        info!("PluginManager::unload_plugin({:?})", plugin_name);
        let mut plugins = self.plugins.write().unwrap();
        if let Some(plugin) = plugins.remove(plugin_name) {
            trace!("PluginManager::unload_plugin() > calling plugin `on_unload`");
            plugin.plugin.on_unload()?;
            if Arc::strong_count(&plugin.in_library) == 1 {
                trace!("PluginManager::unload_plugin() > closing library");
                let in_library = Arc::try_unwrap(plugin.in_library).unwrap();
                if let Err(e) = in_library.library.close() {
                    error!(
                        "Error closing library {:?}; {}",
                        in_library.file_name.to_string_lossy().to_string(),
                        e
                    );
                    return Err(ErrorKind::LibraryCloseFailed(
                        in_library.file_name.to_string_lossy().to_string(),
                        Box::new(e),
                    )
                    .into());
                }
            }
        }
        Ok(())
    }

    // --------------------------------------------------------------------------------------------

    fn find_library(&self, file_name: &Path) -> PathBuf {
        trace!("PluginManager::find_library() > checking search path for library");
        self.search_path
            .find_file(file_name)
            .unwrap_or(file_name.to_path_buf())
    }

    #[allow(unsafe_code)]
    fn check_compatibility(&self, library: &LoadedLibrary) -> Result<()> {
        let compatibility_fn = unsafe {
            let loader_fn: Symbol<'_, CompatibilityFn> =
                library.library.get(COMPATIBILITY_FN_NAME).map_err(|e| {
                    Error::from(ErrorKind::SymbolNotFound(
                        String::from_utf8(COMPATIBILITY_FN_NAME.to_vec()).expect(UTF8_STRING_PANIC),
                        Box::new(e),
                    ))
                })?;
            loader_fn
        };
        trace!("PluginManager::check_compatibility() > fetching library compatibility hash");
        let lib_compatibility_hash: u64 = compatibility_fn();
        trace!("PluginManager::check_compatibility() > fetching local compatibility hash");
        let local_compatibility_hash: u64 = compatibility_hash();
        if lib_compatibility_hash != local_compatibility_hash {
            error!(
                "Version incompatibility {:?} != {:?}",
                lib_compatibility_hash, local_compatibility_hash
            );
            return Err(ErrorKind::IncompatibleLibraryVersion(
                library.file_name.to_string_lossy().to_string(),
            )
            .into());
        }
        trace!("PluginManager::check_compatibility() > compatibility version check passed");
        Ok(())
    }

    #[allow(unsafe_code)]
    fn register_plugins(&mut self, from_library: LoadedLibrary) -> Result<()> {
        trace!(
            "PluginManager::register_plugins(_, {:?})",
            &from_library.file_name
        );
        let load_fn = unsafe {
            let loader_fn: Symbol<'_, PluginRegistrationFn<T>> = from_library
                .library
                .get(self.registration_fn_name.as_slice())
                .map_err(|e| {
                    Error::from(ErrorKind::SymbolNotFound(
                        String::from_utf8(self.registration_fn_name.clone())
                            .expect(UTF8_STRING_PANIC),
                        Box::new(e),
                    ))
                })?;
            loader_fn
        };

        trace!(
            "PluginManager::register_plugins() > calling `{}`",
            String::from_utf8(self.registration_fn_name.clone()).expect(UTF8_STRING_PANIC)
        );
        let mut registrar = PluginRegistrar::default();
        load_fn(&mut registrar);

        let mut registry = self.plugins.write().unwrap();

        let from_library = Arc::new(from_library);

        for plugin in registrar
            .plugins()
            .map_err(|e| Error::from(ErrorKind::PluginRegistration(e)))?
        {
            info!("PluginManager::register_plugins() > calling plugin `on_load`");
            plugin.on_load()?;
            if let Some(_) = registry.insert(
                plugin.plugin_id().to_string(),
                LoadedPlugin {
                    plugin,
                    in_library: from_library.clone(),
                },
            ) {
                warn!("New plugin replaced a plugin with the same ID");
            }
        }

        Ok(())
    }
}

// ------------------------------------------------------------------------------------------------
// Unit Tests
// ------------------------------------------------------------------------------------------------

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

    #[cfg(target_os = "macos")]
    const EXPECTED_FILE: &str = "libmy_lib.dylib";

    #[cfg(target_os = "linux")]
    const EXPECTED_FILE: &str = "libmy_lib.so";

    #[cfg(target_os = "windows")]
    const EXPECTED_FILE: &str = "my_lib.dll";

    #[test]
    fn test_make_dylib_name() {
        let file_name = make_platform_dylib_name("my_lib".as_ref());
        assert_eq!(file_name.to_str().unwrap(), EXPECTED_FILE);
        let file_name = make_platform_dylib_name("my_lib.foo".as_ref());
        assert_eq!(file_name.to_str().unwrap(), EXPECTED_FILE);
    }
}