VSM C++ SDK
Vehicle Specific Modules SDK
file_processor.h
Go to the documentation of this file.
1 // Copyright (c) 2014, Smart Projects Holdings Ltd
2 // All rights reserved.
3 // See LICENSE file for license details.
4 
11 #ifndef FILE_PROCESSOR_H_
12 #define FILE_PROCESSOR_H_
13 
14 #include <ugcs/vsm/io_request.h>
16 
17 #include <thread>
18 #include <unistd.h>
19 
20 namespace ugcs {
21 namespace vsm {
22 
34 public:
47 
48  class Native_controller;
49 
51  class Stream: public Io_stream {
53  public:
56 
58  enum class Lock_result {
60  OK,
62  BLOCKED,
64  ERROR
65  };
66 
68  class Mode {
69  public:
71  bool read:1,
72 
74  write:1,
75 
80  extended:1,
81 
94  should_not_exist:1;
95 
102  Mode(const std::string &mode_str);
103  };
104 
109  public:
110 
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  protected:
237  Set_write_activity(bool is_active,
238  std::unique_lock<std::mutex> &&lock =
239  std::unique_lock<std::mutex>());
240 
249  Set_read_activity(bool is_active,
250  std::unique_lock<std::mutex> &&lock =
251  std::unique_lock<std::mutex>());
252 
254  Native_controller &
256  {
257  return stream->Get_native_controller();
258  }
259  };
260 
270  Stream(File_processor::Ptr processor, const std::string &path, Mode mode,
271  bool maintain_pos, Native_handle::Unique_ptr&& native_handle);
272 
278  Offset
280  {
281  return cur_pos;
282  }
283 
296  Offset
297  Seek(Offset pos, bool is_relative = false);
298 
301 
320  Lock( Lock_handler completion_handler,
323  bool lock = true);
324 
336  Unlock( Lock_handler completion_handler =
337  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  };
512 
516  class Native_controller {
517  public:
518  virtual
519  ~Native_controller()
520  {}
521 
523  virtual void
524  Enable() = 0;
525 
527  virtual void
528  Disable() = 0;
529 
531  virtual void
532  Register_handle(Stream::Native_handle &handle) = 0;
533 
535  virtual void
536  Unregister_handle(Stream::Native_handle &handle) = 0;
537 
539  static std::unique_ptr<Native_controller>
540  Create();
541  };
542 
544  template <typename... Args>
545  static Ptr
546  Get_instance(Args &&... args)
547  {
548  return singleton.Get_instance(std::forward<Args>(args)...);
549  }
550 
551  File_processor();
552 
578  Open(const std::string &name,const std::string &mode, bool maintain_pos = true);
579 
587  static FILE*
588  Fopen_utf8(const std::string &name, const std::string & mode);
589 
597  static bool
598  Rename_utf8(const std::string &old_name, const std::string &new_name);
599 
604  static bool
605  Remove_utf8(const std::string &name);
606 
614  static int
615  Access_utf8(const std::string &name, int mode);
616 
617 private:
618  friend class Stream;
619 
621  static Singleton<File_processor> singleton;
623  Request_worker::Ptr worker;
627  std::unique_ptr<Native_controller> native_controller;
628 
630  virtual void
631  On_enable() override;
632 
634  virtual void
635  On_disable() override;
636 
638  void
639  Unregister_handle(Stream::Native_handle &handle);
640 
643  Open_native_handle(const std::string &name, const std::string &mode);
644 
645 protected:
647  void
649 };
650 
651 } /* namespace vsm */
652 } /* namespace ugcs */
653 
654 #endif /* FILE_PROCESSOR_H_ */
UGCS root namespace.
Definition: android-linux/ugcs/vsm/platform_sockets.h:27
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:336
Exception class with one parameter.
Definition: exception.h:88
Native_controller & Get_native_controller() const
Get native controlled of the related stream.
Definition: file_processor.h:255
void Disable()
Disable the container.
int64_t Offset
Offset for read/write operations.
Definition: io_stream.h:62
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
STL namespace.
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:85
Callback_proxy< void, Io_result > Lock_handler
Default prototype for lock operation completion handler.
Definition: file_processor.h:300
Io_result
Result of I/O operation.
Definition: io_stream.h:25
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:68
std::shared_ptr< Request > Ptr
Pointer type.
Definition: request_container.h:38
Abstract I/O stream interface.
Definition: io_stream.h:54
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
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:210
Offset Get_current_pos() const
Get current position in the stream.
Definition: file_processor.h:279
#define VSM_DEFINE_EXCEPTION(__exc_class,...)
Define custom exception type.
Definition: exception.h:202
I/O request declaration.
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:691
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
std::shared_ptr< Read_request > Ptr
Shared pointer to read request.
Definition: io_request.h:148
Stream::Ptr stream
Related stream.
Definition: file_processor.h:223
Interface for platform native file handle.
Definition: file_processor.h:108
Generic container for queued requests.
Definition: request_container.h:31
Stream class which represents opened file.
Definition: file_processor.h:51
std::weak_ptr< File_processor > Weak_ptr
Pointer type.
Definition: file_processor.h:33
std::shared_ptr< Write_request > Ptr
Shared pointer to write request.
Definition: io_request.h:112
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:33
void Enable()
Enable the container.
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:58
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:516
static Ptr Get_instance(Args &&...args)
Get global or create new processor instance.
Definition: file_processor.h:546
Processor for working with filesystem I/O.
Definition: file_processor.h:32
static Ptr Create(Args &&...args)
Create an instance.
Definition: file_processor.h:33
Helper class for defining derived exceptions.
Definition: exception.h:135
Class for synchronizing with request execution.
Definition: operation_waiter.h:24