00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include <basix/storage.hpp>
00014 #include <basix/system.hpp>
00015 namespace mmx {
00016
00017 static port connect_to_server ();
00018
00019
00020
00021
00022
00023 class disk_storage_rep: public storage_rep {
00024 private:
00025 port server;
00026 string root;
00027
00028 public:
00029 void write (const string& var, const string& val) const;
00030 string read (const string& var) const;
00031 void lock (const string& var) const;
00032 void unlock (const string& var) const;
00033
00034 storage get_branch (const string& br) const;
00035 bool has_branch (const string& br) const;
00036 string get_file (const string& name) const;
00037 bool has_file (const string& name) const;
00038
00039 vector<string> retrieve_data () const;
00040 vector<string> retrieve_branches () const;
00041 vector<string> retrieve_files () const;
00042
00043 public:
00044 inline disk_storage_rep (const string& root2):
00045 server (connect_to_server ()), root (root2) {}
00046 inline disk_storage_rep (const port& server2, const string& root2):
00047 server (server2), root (root2) {}
00048 inline ~disk_storage_rep () {}
00049 friend class storage;
00050 };
00051
00052
00053
00054
00055
00056 static port
00057 connect_to_server () {
00058 port server= socket_client_port ("localhost", 3124);
00059 if (error_flag (server)) {
00060 ASSERT (file_exists (path_name ("$PATH", "storage_server")),
00061 "storage server cannot be started");
00062 system ("storage_server&");
00063 nat pause= 1;
00064 while (error_flag (server)) {
00065 (void) wait_port_event (pause);
00066 server= socket_client_port ("localhost", 3124);
00067 pause *= 2;
00068 ASSERT (pause < 5000,
00069 "storage server cannot be started");
00070 }
00071 }
00072 binary_write<string> (server, "connect");
00073 return server;
00074 }
00075
00076 storage
00077 disk_storage () {
00078 return (storage_rep*) new disk_storage_rep (user_dir () * "/cache");
00079 }
00080
00081 storage
00082 disk_storage (const string& root) {
00083 return (storage_rep*) new disk_storage_rep (root);
00084 }
00085
00086 storage
00087 disk_storage (const port& server, const string& root) {
00088 return (storage_rep*) new disk_storage_rep (server, root);
00089 }
00090
00091
00092
00093
00094
00095 void
00096 disk_storage_rep::write (const string& var, const string& val) const {
00097 binary_write<string> (server, root);
00098 binary_write<string> (server, "write");
00099 binary_write<string> (server, var);
00100 binary_write<string> (server, val);
00101 }
00102
00103 string
00104 disk_storage_rep::read (const string& var) const {
00105 binary_write<string> (server, root);
00106 binary_write<string> (server, "read");
00107 binary_write<string> (server, var);
00108 return binary_read<string> (server);
00109 }
00110
00111 void
00112 disk_storage_rep::lock (const string& var) const {
00113 binary_write<string> (server, root);
00114 binary_write<string> (server, "lock");
00115 binary_write<string> (server, var);
00116 }
00117
00118 void
00119 disk_storage_rep::unlock (const string& var) const {
00120 binary_write<string> (server, root);
00121 binary_write<string> (server, "unlock");
00122 binary_write<string> (server, var);
00123 }
00124
00125 storage
00126 disk_storage_rep::get_branch (const string& br) const {
00127 binary_write<string> (server, root);
00128 binary_write<string> (server, "get_branch");
00129 binary_write<string> (server, br);
00130 return disk_storage (server, binary_read<string> (server));
00131 }
00132
00133 bool
00134 disk_storage_rep::has_branch (const string& br) const {
00135 binary_write<string> (server, root);
00136 binary_write<string> (server, "has_branch");
00137 binary_write<string> (server, br);
00138 return binary_read<bool> (server);
00139 }
00140
00141 string
00142 disk_storage_rep::get_file (const string& name) const {
00143 binary_write<string> (server, root);
00144 binary_write<string> (server, "get_file");
00145 binary_write<string> (server, name);
00146 return binary_read<string> (server);
00147 }
00148
00149 bool
00150 disk_storage_rep::has_file (const string& name) const {
00151 binary_write<string> (server, root);
00152 binary_write<string> (server, "has_file");
00153 binary_write<string> (server, name);
00154 return binary_read<bool> (server);
00155 }
00156
00157 vector<string>
00158 disk_storage_rep::retrieve_data () const {
00159 binary_write<string> (server, root);
00160 binary_write<string> (server, "retrieve_data");
00161 return binary_read<vector<string> > (server);
00162 }
00163
00164 vector<string>
00165 disk_storage_rep::retrieve_branches () const {
00166 binary_write<string> (server, root);
00167 binary_write<string> (server, "retrieve_branches");
00168 return binary_read<vector<string> > (server);
00169 }
00170
00171 vector<string>
00172 disk_storage_rep::retrieve_files () const {
00173 binary_write<string> (server, root);
00174 binary_write<string> (server, "retrieve_files");
00175 return binary_read<vector<string> > (server);
00176 }
00177
00178
00179
00180
00181
00182 storage
00183 cached_storage (const storage& st) {
00184 return st;
00185 }
00186
00187 }