1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
|
/*=========================================================================
*
* Copyright Insight Software Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#include "gdcmULConnection.h"
#include <algorithm> // std::find
#include <socket++/echo.h>
namespace gdcm
{
namespace network
{
ULConnection::ULConnection(const ULConnectionInfo& inConnectInfo)
{
mCurrentState = eSta1Idle;
mSocket = NULL;
mEcho = NULL;
mInfo = inConnectInfo;
TransferSyntaxSub ts1;
ts1.SetNameFromUID( UIDs::ImplicitVRLittleEndianDefaultTransferSyntaxforDICOM );
SetCStoreTransferSyntax( ts1 );
}
ULConnection::~ULConnection()
{
if (mEcho != NULL)
{
delete mEcho;
mEcho = NULL;
}
if (mSocket != NULL)
{
delete mSocket;
mSocket = NULL;
}
}
EStateID ULConnection::GetState() const
{
return mCurrentState;
}
void ULConnection::SetState(const EStateID& inState)
{
mCurrentState = inState;
}
//echo* ULConnection::GetProtocol(){
std::iostream* ULConnection::GetProtocol()
{
if (mEcho)
{
return mEcho;
}
//be careful-- the socket doesn't get removed when the protocol is stopped,
//because then subsequent cstores will break.
if (mSocket)
{
return mSocket;
}
return NULL;
}
ARTIMTimer& ULConnection::GetTimer()
{
return mTimer;
}
const ULConnectionInfo &ULConnection::GetConnectionInfo() const
{
return mInfo;
}
void ULConnection::SetMaxPDUSize(uint32_t inSize)
{
mMaxPDUSize = inSize;
}
uint32_t ULConnection::GetMaxPDUSize() const
{
return mMaxPDUSize;
}
std::vector<PresentationContextRQ> const &
ULConnection::GetPresentationContexts() const
{
return mPresentationContexts;
}
void ULConnection::SetPresentationContexts(
const std::vector<PresentationContext>& inContexts)
{
mPresentationContexts.clear();
for( size_t i = 0; i < inContexts.size(); ++i )
{
PresentationContext const &in = inContexts[i];
mPresentationContexts.push_back( in );
}
}
void ULConnection::SetPresentationContexts(
const std::vector<PresentationContextRQ>& inContexts)
{
mPresentationContexts = inContexts;
}
std::vector<PresentationContextAC> & ULConnection::GetAcceptedPresentationContexts()
{
return mAcceptedPresentationContexts;
}
std::vector<PresentationContextAC> const & ULConnection::GetAcceptedPresentationContexts() const
{
return mAcceptedPresentationContexts;
}
void ULConnection::AddAcceptedPresentationContext(const PresentationContextAC& inPC)
{
mAcceptedPresentationContexts.push_back(inPC);
}
//given a particular data element, presumably the SOP class,
//find the presentation context for that SOP
//NOT YET IMPLEMENTED
PresentationContextRQ ULConnection::FindContext(const DataElement& ) const
{
PresentationContextRQ empty;
assert( 0 && "TODO" );
return empty;
}
bool ULConnection::InitializeConnection()
{
try
{
echo* p = new echo(protocol::tcp);
if (GetConnectionInfo().GetCalledIPPort() == 0)
{
if (!GetConnectionInfo().GetCalledComputerName().empty())
(*p)->connect(GetConnectionInfo().GetCalledComputerName().c_str());
else
(*p)->connect(GetConnectionInfo().GetCalledIPAddress());
}
else
{
if (!GetConnectionInfo().GetCalledComputerName().empty())
(*p)->connect(GetConnectionInfo().GetCalledComputerName().c_str(),
GetConnectionInfo().GetCalledIPPort());
}
//make sure to convert timeouts to platform appropriate values.
(*p)->recvtimeout((int)GetTimer().GetTimeout());
(*p)->sendtimeout((int)GetTimer().GetTimeout());
if (mEcho != NULL)
{
delete mEcho;
mEcho = NULL;
}
if (mSocket != NULL)
{
delete mSocket;
mSocket = NULL;
}
mEcho = p;
}
catch ( sockerr &err )
{
(void)err; //to avoid unreferenced variable warning on release
gdcmWarningMacro( "Unable to open connection with exception " << err.what()
<< std::endl );
return false;
}
catch (std::exception& ex)
{
(void)ex; //to avoid unreferenced variable warning on release
//unable to establish connection, so break off.
gdcmWarningMacro( "Unable to open connection with exception " << ex.what()
<< std::endl );
return false;
}
return true;
}
bool ULConnection::InitializeIncomingConnection()
{
try
{
if (mEcho != NULL)
{
delete mEcho;
mEcho = NULL;
}
if (mSocket != NULL)
{
delete mSocket;
mSocket = NULL;
}
sockinetbuf sin (sockbuf::sock_stream);
// http://hea-www.harvard.edu/~fine/Tech/addrinuse.html
int val = 1;
sin.setopt( SO_REUSEADDR, &val, sizeof(val) );
sin.bind( mInfo.GetCalledIPPort() );
//int theRecvTimeout =
sin.recvtimeout(60);//(int)GetTimer().GetTimeout());
//int theSendTimeout =
sin.sendtimeout(60);//(int)GetTimer().GetTimeout());
sin.listen();
//sin.debug( true );
if (sin.is_readready(60, 0))
{
mSocket = new iosockinet(sin.accept());
}
else
{
gdcmDebugMacro( "Call to is_readready failed" );
SetState(eStaDoesNotExist);
return false; //no connection here, so have to initialize later.
}
SetState(eSta2Open);
/*
if (mSocket != NULL){
delete mSocket;
}
mSocket = new protocol();
sockinetaddr theAddy(GetConnectionInfo().GetCalledComputerName().c_str(),
GetConnectionInfo().GetCalledIPPort());
mSocket->rdbuf()->connect(theAddy);
mSocket->rdbuf()->recvtimeout((int)GetTimer().GetTimeout());
mSocket->rdbuf()->sendtimeout((int)GetTimer().GetTimeout());
*/
}
catch (sockerr& ex)
{
//unable to establish connection, so break off.
(void)ex; //to avoid unreferenced variable warning on release
gdcmErrorMacro("Unable to open connection with exception " << ex.what() <<
" and " << ex.operation() << " on port: " << mInfo.GetCalledIPPort() <<
std::endl);
return false;
}
catch (std::exception& ex)
{
//unable to establish connection, so break off.
(void)ex; //to avoid unreferenced variable warning on release
gdcmErrorMacro("Unable to open connection with exception " << ex.what() << std::endl);
return false;
}
return true;
}
void ULConnection::StopProtocol()
{
if (mEcho != NULL)
{
delete mEcho;
mEcho = NULL;
SetState(eSta1Idle);
}
else
{
//don't actually kill the connection, just kill the association.
//this is just for a cstorescp initialized by a cmove
SetState(eSta2Open);
}
}
const PresentationContextRQ *ULConnection::GetPresentationContextRQByID(uint8_t id) const
{
// one day ULConnection will actually use a AAssociateRQPDU as internal implementation
// for now duplicate code from AAssociateRQPDU::GetPresentationContextFromAbstractSyntax
std::vector<PresentationContextRQ>::const_iterator it = mPresentationContexts.begin();
for( ; it != mPresentationContexts.end(); ++it)
{
if( it->GetPresentationContextID() == id )
{
return &*it;
}
}
return NULL;
}
const PresentationContextAC *ULConnection::GetPresentationContextACByID(uint8_t id) const
{
// one day ULConnection will actually use a AAssociateRQPDU as internal implementation
// for now duplicate code from AAssociateRQPDU::GetPresentationContextFromAbstractSyntax
std::vector<PresentationContextAC>::const_iterator it = mAcceptedPresentationContexts.begin();
for( ; it != mAcceptedPresentationContexts.end(); ++it)
{
if( it->GetPresentationContextID() == id )
{
return &*it;
}
}
return NULL;
}
uint8_t ULConnection::GetPresentationContextIDFromPresentationContext(PresentationContextRQ const & pc) const
{
// one day ULConnection will actually use a AAssociateRQPDU as internal implementation
// for now duplicate code from AAssociateRQPDU::GetPresentationContextIDFromAbstractSyntax
uint8_t ret = 0;
std::vector<PresentationContextRQ>::const_iterator it =
std::find( mPresentationContexts.begin(), mPresentationContexts.end(), pc );
if( it != mPresentationContexts.end() )
{
ret = it->GetPresentationContextID();
}
assert( ret );
return ret;
}
void ULConnection::SetCStoreTransferSyntax( TransferSyntaxSub const & ts )
{
cstorets = ts;
}
TransferSyntaxSub const & ULConnection::GetCStoreTransferSyntax( ) const
{
TransferSyntaxSub ts1;
ts1.SetNameFromUID( UIDs::ImplicitVRLittleEndianDefaultTransferSyntaxforDICOM );
TransferSyntaxSub ts2;
ts2.SetNameFromUID( UIDs::ExplicitVRLittleEndian );
assert( strcmp(cstorets.GetName(), ts1.GetName()) == 0
|| strcmp(cstorets.GetName(), ts2.GetName()) == 0
);
return cstorets;
}
} // end namespace network
} // end namespace gdcm
|