00001
00002
00003
00004
00005
00006
00007
00008 #include "ShmemDataLink.h"
00009 #include "ShmemTransport.h"
00010 #include "ShmemInst.h"
00011
00012 #include "ace/Log_Msg.h"
00013
00014 #include <cstdlib>
00015
00016 #ifndef __ACE_INLINE__
00017 # include "ShmemDataLink.inl"
00018 #endif
00019
00020 namespace OpenDDS {
00021 namespace DCPS {
00022
00023 ShmemDataLink::ShmemDataLink(ShmemTransport* transport)
00024 : DataLink(transport,
00025 0,
00026 false,
00027 false)
00028 , config_(0)
00029 , peer_alloc_(0)
00030 {
00031 }
00032
00033 bool
00034 ShmemDataLink::open(const std::string& peer_address)
00035 {
00036 peer_address_ = peer_address;
00037 const ACE_TString name = ACE_TEXT_CHAR_TO_TCHAR(peer_address.c_str());
00038 ShmemAllocator::MEMORY_POOL_OPTIONS alloc_opts;
00039
00040 #ifdef ACE_WIN32
00041 const bool use_opts = true;
00042 const ACE_TString name_under = name + ACE_TEXT('_');
00043
00044 HANDLE fm = ACE_TEXT_CreateFileMapping(INVALID_HANDLE_VALUE, 0, PAGE_READONLY,
00045 0, ACE_DEFAULT_PAGEFILE_POOL_CHUNK, name_under.c_str());
00046 void* view;
00047 if (fm == 0 || (view = MapViewOfFile(fm, FILE_MAP_READ, 0, 0, 0)) == 0) {
00048 stop_i();
00049 ACE_ERROR_RETURN((LM_ERROR,
00050 ACE_TEXT("(%P|%t) ERROR: ShmemDataLink::open: ")
00051 ACE_TEXT("peer's shared memory area not found (%C)\n"),
00052 peer_address.c_str()),
00053 false);
00054 }
00055
00056 const size_t* pmax = (const size_t*)(((void**)view) + 2);
00057 alloc_opts.max_size_ = *pmax;
00058 UnmapViewOfFile(view);
00059 CloseHandle(fm);
00060 #else
00061 const bool use_opts = false;
00062 #endif
00063
00064 peer_alloc_ = new ShmemAllocator(name.c_str(), 0 ,
00065 use_opts ? &alloc_opts : 0);
00066
00067 if (-1 == peer_alloc_->find("Semaphore")) {
00068 stop_i();
00069 ACE_ERROR_RETURN((LM_ERROR,
00070 ACE_TEXT("(%P|%t) ERROR: ShmemDataLink::open: ")
00071 ACE_TEXT("peer's shared memory area not found (%C)\n"),
00072 peer_address.c_str()),
00073 false);
00074 }
00075
00076 if (start(static_rchandle_cast<TransportSendStrategy>(send_strategy_),
00077 static_rchandle_cast<TransportStrategy>(recv_strategy_))
00078 != 0) {
00079 stop_i();
00080 ACE_ERROR_RETURN((LM_ERROR,
00081 ACE_TEXT("(%P|%t) ERROR: ")
00082 ACE_TEXT("ShmemDataLink::open: start failed!\n")),
00083 false);
00084 }
00085
00086 VDBG_LVL((LM_INFO, "(%P|%t) ShmemDataLink link %@ open to peer %C\n",
00087 this, peer_address_.c_str()), 1);
00088
00089 return true;
00090 }
00091
00092 void
00093 ShmemDataLink::control_received(ReceivedDataSample& )
00094 {
00095 }
00096
00097 void
00098 ShmemDataLink::stop_i()
00099 {
00100 if (peer_alloc_) {
00101 peer_alloc_->release(0 );
00102 }
00103 delete peer_alloc_;
00104 peer_alloc_ = 0;
00105 }
00106
00107 ShmemAllocator*
00108 ShmemDataLink::local_allocator()
00109 {
00110 return static_rchandle_cast<ShmemTransport>(impl())->alloc();
00111 }
00112
00113 std::string
00114 ShmemDataLink::local_address()
00115 {
00116 return static_rchandle_cast<ShmemTransport>(impl())->address();
00117 }
00118
00119 void
00120 ShmemDataLink::signal_semaphore()
00121 {
00122 return static_rchandle_cast<ShmemTransport>(impl())->signal_semaphore();
00123 }
00124
00125 pid_t
00126 ShmemDataLink::peer_pid()
00127 {
00128 return std::atoi(peer_address_.c_str() + peer_address_.find('-') + 1);
00129 }
00130
00131 }
00132 }