VSM C++ SDK
Vehicle Specific Modules SDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
file_processor.h
Go to the documentation of this file.
1 // Copyright (c) 2018, Smart Projects Holdings Ltd
2 // All rights reserved.
3 // See LICENSE file for license details.
4 
11 #ifndef _UGCS_VSM_FILE_PROCESSOR_H_
12 #define _UGCS_VSM_FILE_PROCESSOR_H_
13 
14 #include <ugcs/vsm/io_request.h>
16 #include <unistd.h>
17 #include <thread>
18 
19 namespace ugcs {
20 namespace vsm {
21 
33 
34 public:
47 
48  class Native_controller;
49 
51  class Stream: public Io_stream {
53 
54  public:
57 
59  enum class Lock_result {
61  OK,
63  BLOCKED,
65  ERROR
66  };
67 
69  class Mode {
70  public:
72  bool read:1,
73 
75  write:1,
76 
81  extended:1,
82 
95  should_not_exist:1;
96 
103  Mode(const std::string &mode_str);
104  };
105 
110  public:
112  typedef std::unique_ptr<Native_handle> Unique_ptr;
113 
118  public:
121  std::unique_lock<std::mutex> &&lock):
122  stream(stream), lock(std::move(lock))
123  {}
124 
126  Stream_ref_holder(Stream_ref_holder &&) = default;
127 
129  {
130  /* Prevent access to the mutex after stream is released. */
131  if (lock) {
132  lock.unlock();
133  lock.release();
134  }
135  stream = nullptr;
136  }
137  private:
138  Stream::Ptr stream;
139  std::unique_lock<std::mutex> lock;
140  };
141 
142  virtual
143  ~Native_handle()
144  {}
145 
147  void
148  Set_stream(Stream::Ptr stream);
149 
155  virtual void
156  Write() = 0;
157 
163  virtual void
164  Read() = 0;
165 
169  virtual Lock_result
170  Try_lock() = 0;
171 
175  virtual bool
176  Lock() = 0;
177 
183  virtual bool
184  Unlock() = 0;
185 
190  virtual bool
191  Cancel_write() = 0;
192 
197  virtual bool
198  Cancel_read() = 0;
199 
201  virtual void
202  Close() = 0;
203 
205  void
206  Handle_write_abort();
207 
209  void
210  Handle_read_abort();
211 
221  bool is_closed = false;
227  read_active;
228 
229  protected:
238  Set_write_activity(bool is_active,
239  std::unique_lock<std::mutex> &&lock =
240  std::unique_lock<std::mutex>());
241 
250  Set_read_activity(bool is_active,
251  std::unique_lock<std::mutex> &&lock =
252  std::unique_lock<std::mutex>());
253 
255  Native_controller &
257  {
258  return stream->Get_native_controller();
259  }
260  };
261 
271  Stream(File_processor::Ptr processor, const std::string &path, Mode mode,
272  bool maintain_pos, Native_handle::Unique_ptr&& native_handle);
273 
279  Offset
281  {
282  return cur_pos;
283  }
284 
297  Offset
298  Seek(Offset pos, bool is_relative = false);
299 
302 
321  Lock(
322  Lock_handler completion_handler,
324  bool lock = true);
325 
338  Lock_handler completion_handler = Make_dummy_callback<void, Io_result>(),
340  {
341  return Lock(completion_handler, comp_ctx, false);
342  }
343 
344  private:
345  friend class File_processor;
346  friend class Native_handle;
347 
349  typedef struct {
351  bool flock_thread_active = false;
353  bool flock_acquire_requested = false;
355  bool flock_acquired = false;
357  std::condition_variable flock_notifier;
358  } Lock_cb;
359 
360  Lock_cb lock_cb;
361 
363  File_processor::Weak_ptr processor;
365  std::mutex op_mutex;
367  std::list<Read_request::Ptr> read_queue;
369  std::list<Write_request::Ptr> write_queue;
370 
372  Mode mode;
374  bool maintain_pos;
376  Offset cur_pos = 0;
378  Native_handle::Unique_ptr native_handle;
379 
383  virtual Operation_waiter
384  Write_impl(Io_buffer::Ptr buffer,
385  Offset offset,
386  Write_handler completion_handler,
387  Request_completion_context::Ptr comp_ctx) override;
388 
392  virtual Operation_waiter
393  Read_impl(size_t max_to_read, size_t min_to_read, Offset offset,
394  Read_handler completion_handler,
395  Request_completion_context::Ptr comp_ctx) override;
396 
400  virtual Operation_waiter
401  Close_impl(Close_handler completion_handler,
402  Request_completion_context::Ptr comp_ctx) override;
403 
409  void
410  Queue_write(Write_request::Ptr request);
411 
416  void
417  Push_write_queue();
418 
424  void
425  Queue_read(Read_request::Ptr request);
426 
431  void
432  Push_read_queue();
433 
435  void
436  Handle_write();
437 
439  void
440  Handle_read();
441 
443  void
444  Handle_lock();
445 
447  void
448  Handle_unlock();
449 
451  void
452  Handle_close(Request::Ptr request);
453 
455  void
456  Handle_write_cancel(Write_request::Ptr request);
457 
459  void
460  Handle_read_cancel(Read_request::Ptr request);
461 
463  void
464  Handle_lock_cancel(Io_request::Ptr request);
465 
468  void
469  Cancel_lock_operation(bool stop_locker_thread);
470 
476  void
477  Handle_write_completion(Request::Handler completion_handler);
478 
484  void
485  Handle_read_completion(Request::Handler completion_handler);
486 
488  void
489  Handle_write_abort();
490 
492  void
493  Handle_read_abort();
494 
495  Native_controller &
496  Get_native_controller() const;
497 
503  void
504  Complete_request(Io_request::Ptr& request, Io_result result = Io_result::OTHER_FAILURE);
505 
508  void
509  Locker_thread();
510  };
511 
515  class Native_controller {
516  public:
517  virtual
518  ~Native_controller()
519  {}
520 
522  virtual void
523  Enable() = 0;
524 
526  virtual void
527  Disable() = 0;
528 
530  virtual void
532 
534  virtual void
536 
538  static std::unique_ptr<Native_controller>
539  Create();
540  };
541 
543  template <typename... Args>
544  static Ptr
545  Get_instance(Args &&... args)
546  {
547  return singleton.Get_instance(std::forward<Args>(args)...);
548  }
549 
550  File_processor();
551 
577  Open(const std::string &name, const std::string &mode, bool maintain_pos = true);
578 
586  static FILE*
587  Fopen_utf8(const std::string &name, const std::string & mode);
588 
596  static bool
597  Rename_utf8(const std::string &old_name, const std::string &new_name);
598 
603  static bool
604  Remove_utf8(const std::string &name);
605 
613  static int
614  Access_utf8(const std::string &name, int mode);
615 
616 private:
617  friend class Stream;
618 
620  static Singleton<File_processor> singleton;
622  Request_worker::Ptr worker;
626  std::unique_ptr<Native_controller> native_controller;
627 
629  virtual void
630  On_enable() override;
631 
633  virtual void
634  On_disable() override;
635 
637  void
638  Unregister_handle(Stream::Native_handle &handle);
639 
642  Open_native_handle(const std::string &name, const std::string &mode);
643 
644 protected:
646  void
648 };
649 
650 } /* namespace vsm */
651 } /* namespace ugcs */
652 
653 #endif /* _UGCS_VSM_FILE_PROCESSOR_H_ */
Io_request::Ptr cur_lock_request
Current lock request.
Definition: file_processor.h:217
Request worker.
static int Access_utf8(const std::string &name, int mode)
Platform independent method for checking file access permission providing UTF-8 file name...
Operation_waiter Unlock(Lock_handler completion_handler=Make_dummy_callback< void, Io_result >(), Request_completion_context::Ptr comp_ctx=Request_temp_completion_context::Create())
Remove lock from file.
Definition: file_processor.h:337
Native_controller & Get_native_controller() const
Get native controlled of the related stream.
Definition: file_processor.h:256
VSM_DEFINE_EXCEPTION(Exception)
Base class for all File_processor exceptions.
Io_request::Ptr cur_unlock_request
Current unlock request.
Definition: file_processor.h:219
static Ptr Create(Args &&...args)
Create an instance.
Definition: request_temp_completion_context.h:19
Helper class to hold reference to a stream and optionally release a lock before releasing the referen...
Definition: file_processor.h:117
Callback_base< void >::Ptr<> Handler
Callback denoting a handler of the request.
Definition: request_container.h:86
Callback_proxy< void, Io_result > Lock_handler
Default prototype for lock operation completion handler.
Definition: file_processor.h:301
Io_result
Result of I/O operation.
Definition: io_stream.h:37
static bool Rename_utf8(const std::string &old_name, const std::string &new_name)
Platform independent method for renaming a file providing UTF-8 encoded paths.
Stream_ref_holder(Stream::Ptr stream, std::unique_lock< std::mutex > &&lock)
Constructor.
Definition: file_processor.h:120
Mode for file opening.
Definition: file_processor.h:69
std::shared_ptr< Request > Ptr
Pointer type.
Definition: request_container.h:38
Abstract I/O stream interface.
Definition: io_stream.h:66
Operation completed successfully.
std::shared_ptr< Io_request > Ptr
Pointer type.
Definition: io_request.h:22
std::shared_ptr< Request_worker > Ptr
Pointer type.
Definition: request_worker.h:25
virtual void Enable()=0
Enable the controller.
Request execution context.
Definition: request_context.h:24
Some other system failure.
#define VSM_DEFINE_DERIVED_EXCEPTION(__base_class, __exc_class)
Define custom derived exception.
Definition: exception.h:208
static std::unique_ptr< Native_controller > Create()
Create controller instance.
Offset Get_current_pos() const
Get current position in the stream.
Definition: file_processor.h:280
I/O request declaration.
virtual void Unregister_handle(Stream::Native_handle &handle)=0
Unregister previously registered file handle.
void Register_stream(File_processor::Stream::Ptr)
Register opened stream in a processor.
Stream::Ref Open(const std::string &name, const std::string &mode, bool maintain_pos=true)
Open file.
Helper class for proxying callback invocation.
Definition: callback.h:699
Write_request::Ptr cur_write_request
Current write request.
Definition: file_processor.h:215
std::unique_ptr< Native_handle > Unique_ptr
Unique pointer type.
Definition: file_processor.h:112
virtual void Disable()=0
Disable the controller.
std::shared_ptr< Read_request > Ptr
Shared pointer to read request.
Definition: io_request.h:147
Stream::Ptr stream
Related stream.
Definition: file_processor.h:223
Interface for platform native file handle.
Definition: file_processor.h:109
Generic container for queued requests.
Definition: request_container.h:30
Stream class which represents opened file.
Definition: file_processor.h:51
std::weak_ptr< File_processor > Weak_ptr
Pointer type.
Definition: file_processor.h:32
Reference_guard< Stream::Ptr > Ref
Reference type.
Definition: file_processor.h:56
std::shared_ptr< Write_request > Ptr
Shared pointer to write request.
Definition: io_request.h:111
std::shared_ptr< Request_context > Ptr
Pointer type.
Definition: request_context.h:25
Read_request::Ptr cur_read_request
Current read request.
Definition: file_processor.h:213
std::shared_ptr< Io_buffer > Ptr
Pointer type.
Definition: io_buffer.h:34
static FILE * Fopen_utf8(const std::string &name, const std::string &mode)
Platform independent method for opening a standard library file handle providing an UTF-8 encoded pat...
#define DEFINE_COMMON_CLASS(__class_name,...)
Use this macro to define some common attributes for a class.
Definition: utils.h:25
Reference guard objects keep references for managed objects.
Definition: reference_guard.h:31
Stream::Ptr write_active
Holds reference to a stream while write operation is in progress.
Definition: file_processor.h:225
std::shared_ptr< File_processor > Ptr
Pointer type.
Definition: file_processor.h:32
virtual void Register_handle(Stream::Native_handle &handle)=0
Register new opened file handle.
Helper class for implementing singletons.
Definition: singleton.h:69
std::shared_ptr< Stream > Ptr
Pointer type.
Definition: file_processor.h:52
Lock_result
Lock operation result.
Definition: file_processor.h:59
static bool Remove_utf8(const std::string &name)
Platform independent method for removing a file providing UTF-8 encoded path.
Interface for native I/O controller which manages I/O operations for all native handles.
Definition: file_processor.h:515
static Ptr Get_instance(Args &&...args)
Get global or create new processor instance.
Definition: file_processor.h:545
Processor for working with filesystem I/O.
Definition: file_processor.h:31
Helper class for defining derived exceptions.
Definition: exception.h:133
Base class for all VSM exceptions.
Definition: exception.h:22
Class for synchronizing with request execution.
Definition: operation_waiter.h:24