00001
00007 #ifndef IPC_MENSAGEIRO_H
00008 #define IPC_MENSAGEIRO_H
00009
00010 #include <string>
00011
00012 #include <Erros/erros.H>
00013 #include <Utilitarios/cadeia.H>
00014
00015 extern "C" {
00016 #include <sys/msg.h>
00017 #include <sys/sem.h>
00018 #include <unistd.h>
00019 #include <pwd.h>
00020 }
00021
00022 namespace IPC {
00023
00034 class Erro : public Erros::Erro {
00035 public:
00038 Erro(std::string const& mensagem);
00039
00042 Erro();
00043
00044 private:
00045 std::string mensagem;
00046 };
00047
00089 class Mensageiro {
00090 public:
00108 Mensageiro(key_t const chave = getuid());
00109
00114 ~Mensageiro();
00115
00123 std::string meuIdentificador() const;
00124
00132 std::string identificadorDoOutro() const;
00133
00143 void leMensagem();
00144
00156 template <typename Tipo>
00157 Tipo const mensagemLida() const;
00158
00168 template <typename Tipo>
00169 void envia(Tipo& mensagem) const;
00170
00180 template <typename Tipo>
00181 void envia(Tipo const& mensagem) const;
00182
00192 template <typename Char>
00193 void envia(Char*& mensagem) const;
00194
00205 template <typename Char>
00206 void envia(Char const*& mensagem) const;
00207
00217 template <typename Char>
00218 void envia(Char* const& mensagem) const;
00219
00230 template <typename Char>
00231 void envia(Char const* const& mensagem) const;
00232
00242 template <typename Char, int dimensao>
00243 inline void envia(Char const (& mensagem)[dimensao]) const;
00244
00245 private:
00246 std::string tipo_dos_dados;
00247 std::string mensagem;
00248 int meu_PID;
00249 int PID_do_outro;
00250 int meu_UID;
00251 int UID_do_outro;
00252 int identificador_da_caixa_do_correio;
00253 int identificador_do_semaforo;
00254 bool sou_um_servidor;
00255 static bool existe_instancia;
00256
00257
00258 Mensageiro(Mensageiro const&);
00259
00260
00261 Mensageiro& operator =(Mensageiro const&);
00262
00263
00264
00265
00266
00267 int leBasicoInt(int const tipo, bool const deve_esperar = true) const;
00268
00269
00270
00271
00272
00273
00274 std::string leBasico(int const tipo,
00275 bool const deve_esperar = true) const;
00276
00277
00278
00279
00280 void escreveBasicoInt(int const tipo, int const valor);
00281
00282
00283
00284 void escreveBasico(int const tipo, std::string const& mensagem) const;
00285
00286
00287
00288 void estabeleceCaixaDoCorreio(key_t const chave);
00289
00290
00291 void removeCaixaDoCorreio();
00292
00293
00294
00295 void estabeleceSemaforoDeAcesso(key_t const chave);
00296
00297
00298
00299 void entraEmAcessoExclusivo() const;
00300
00301
00302
00303 void saiDeAcessoExclusivo() const;
00304
00312 bool cumpreInvariante() const;
00313 };
00314 }
00315
00316 #include <IPC++/mensageiro_impl.H>
00317
00318 #endif //IPC_MENSAGEIRO_H