libraries/iosrv/server/endpoint.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // endpoint.cpp
       
     2 // 
       
     3 // Copyright (c) 2006 - 2010 Accenture. All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of the "Eclipse Public License v1.0"
       
     6 // which accompanies this distribution, and is available
       
     7 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 // 
       
     9 // Initial Contributors:
       
    10 // Accenture - Initial contribution
       
    11 //
       
    12 
       
    13 #include "server.h"
       
    14 #include "endpoint.h"
       
    15 #include "log.h"
       
    16 
       
    17 #ifdef IOSRV_LOGGING
       
    18 #define ENDPOINT_NAME TName endPointName(Name())
       
    19 #define READER_NAME(x) TName readerName((x).IorName())
       
    20 #define WRITER_NAME(x) TName writerName((x).IowName())
       
    21 #else
       
    22 #define ENDPOINT_NAME
       
    23 #define READER_NAME(x)
       
    24 #define WRITER_NAME(x)
       
    25 #endif
       
    26 
       
    27 CIoEndPoint::~CIoEndPoint()
       
    28 	{
       
    29 	ENDPOINT_NAME;
       
    30 	LOG(CIoLog::Printf(_L("End point \"%S\" (0x%08x) destroying"), &endPointName, this));
       
    31 	iAttachedReaders.Close();
       
    32 	}
       
    33 	
       
    34 TName CIoEndPoint::IoepName() const
       
    35 	{
       
    36 	return Name();
       
    37 	}
       
    38 	
       
    39 TBool CIoEndPoint::IoepIsType(RIoHandle::TType aType) const
       
    40 	{
       
    41 	return IsType(aType);
       
    42 	}
       
    43 
       
    44 void CIoEndPoint::IorepAttachL(MIoReader& aReader, RIoEndPoint::TReadMode aMode)
       
    45 	{
       
    46 	ENDPOINT_NAME;
       
    47 	READER_NAME(aReader);
       
    48 	LOG(CIoLog::Printf(_L("Attaching reader \"%S\" (0x%08x) to end point \"%S\" (0x%08x)"), &readerName, &aReader, &endPointName, this));
       
    49 	User::LeaveIfError(Open());
       
    50 	CleanupClosePushL(*this);
       
    51 	if (aMode == RIoEndPoint::EForeground)
       
    52 		{
       
    53 		User::LeaveIfError(iAttachedReaders.Insert(&aReader, 0));
       
    54 		}
       
    55 	else
       
    56 		{
       
    57 		User::LeaveIfError(iAttachedReaders.Append(&aReader));
       
    58 		}
       
    59 	TInt err = HandleReaderAttached(aReader);
       
    60 	if (err!=KErrNone)
       
    61 		{
       
    62 		TInt index = FindReader(aReader);
       
    63 		iAttachedReaders.Remove(index);
       
    64 		User::Leave(err);
       
    65 		}
       
    66 	if (aMode == RIoEndPoint::EForeground)
       
    67 		{
       
    68 		HandleForegroundReaderChanged();
       
    69 		}
       
    70 	CleanupStack::Pop(this);
       
    71 	}
       
    72 
       
    73 void CIoEndPoint::IorepDetach(MIoReader& aReader)
       
    74 	{
       
    75 	ENDPOINT_NAME;
       
    76 	READER_NAME(aReader);
       
    77 	TInt index = FindReader(aReader);
       
    78 	if (index >= 0)
       
    79 		{
       
    80 		LOG(CIoLog::Printf(_L("Detaching reader \"%S\" (0x%08x) from end point \"%S\" (0x%08x)"), &readerName, &aReader, &endPointName, this));
       
    81 		iAttachedReaders.Remove(index);
       
    82 		HandleReaderDetached(aReader);
       
    83 		if ((index == 0) && (iAttachedReaders.Count() > 0))
       
    84 			{
       
    85 			HandleForegroundReaderChanged();
       
    86 			}
       
    87 		Close();
       
    88 		}
       
    89 	else
       
    90 		{
       
    91 		LOG(CIoLog::Printf(_L("Reader \"%S\" (0x%08x) not attached to end point \"%S\" (0x%08x) - detach request ignored"), &readerName, &aReader, &endPointName, this));
       
    92 		}
       
    93 	}
       
    94 
       
    95 void CIoEndPoint::IorepSetForegroundReaderL(MIoReader& aReader)
       
    96 	{
       
    97 	TInt index = FindReader(aReader);
       
    98 	User::LeaveIfError(index);
       
    99 	if (index > 0)
       
   100 		{
       
   101 		iAttachedReaders.Remove(index);
       
   102 		iAttachedReaders.Insert(&aReader, 0); // Shouldn't fail due to the above removal.
       
   103 		HandleForegroundReaderChanged();
       
   104 		}
       
   105 	}
       
   106 
       
   107 TBool CIoEndPoint::IorepIsForegroundL(const MIoReader& aReader) const
       
   108 	{
       
   109 	TInt index = FindReader(aReader);
       
   110 	User::LeaveIfError(index);
       
   111 	return (index == 0);
       
   112 	}
       
   113 	
       
   114 void CIoEndPoint::IowepAttachL(MIoWriter& aWriter)
       
   115 	{
       
   116 	User::LeaveIfError(Open());
       
   117 	++iAttachedWriterCount;
       
   118 	ENDPOINT_NAME;
       
   119 	WRITER_NAME(aWriter);
       
   120 	LOG(CIoLog::Printf(_L("Attached writer \"%S\" (0x%08x) to end point \"%S\" (0x%08x) (%d writers now attached)"), &writerName, &aWriter, &endPointName, this, iAttachedWriterCount));
       
   121 	TInt err = HandleWriterAttached(aWriter);
       
   122 	if (err!=KErrNone)
       
   123 		{
       
   124 		--iAttachedWriterCount;
       
   125 		User::Leave(err);
       
   126 		}
       
   127 	}
       
   128 
       
   129 void CIoEndPoint::IowepDetach(MIoWriter& aWriter)
       
   130 	{
       
   131 	--iAttachedWriterCount;
       
   132 	ENDPOINT_NAME;
       
   133 	WRITER_NAME(aWriter);
       
   134 	LOG(CIoLog::Printf(_L("Detached writer \"%S\" (0x%08x) from end point \"%S\" (0x%08x) (%d writers now attached)"), &writerName, &aWriter, &endPointName, this, iAttachedWriterCount));
       
   135 	HandleWriterDetached(aWriter);
       
   136 	Close();
       
   137 	}
       
   138 
       
   139 TInt CIoEndPoint::HandleReaderAttached(MIoReader&)
       
   140 	{
       
   141 	return KErrNone;
       
   142 	}
       
   143 
       
   144 void CIoEndPoint::HandleReaderDetached(MIoReader&)
       
   145 	{
       
   146 	}
       
   147 	
       
   148 void CIoEndPoint::ForegroundReaderChanged()
       
   149 	{
       
   150 	}
       
   151 
       
   152 TInt CIoEndPoint::HandleWriterAttached(MIoWriter&)
       
   153 	{
       
   154 	return KErrNone;
       
   155 	}
       
   156 
       
   157 void CIoEndPoint::HandleWriterDetached(MIoWriter&)
       
   158 	{
       
   159 	}
       
   160 
       
   161 void CIoEndPoint::HandleForegroundReaderChanged()
       
   162 	{
       
   163 	MIoReader* fg = AttachedReader();
       
   164 	if (fg)
       
   165 		{
       
   166 		fg->IorReaderChange(RIoReadHandle::EGainedForeground);
       
   167 		}
       
   168 	ForegroundReaderChanged();
       
   169 	}
       
   170 
       
   171 CIoEndPoint::CIoEndPoint()
       
   172 	{
       
   173 	}
       
   174 
       
   175 TInt CIoEndPoint::NumAttachedReaders() const
       
   176 	{
       
   177 	return iAttachedReaders.Count();
       
   178 	}
       
   179 
       
   180 MIoReader* CIoEndPoint::AttachedReader(TInt aIndex)
       
   181 	{
       
   182 	if ((aIndex >= 0) && (aIndex < iAttachedReaders.Count()))
       
   183 		{
       
   184 		return iAttachedReaders[aIndex];
       
   185 		}
       
   186 	return NULL;
       
   187 	}
       
   188 
       
   189 TInt CIoEndPoint::NumAttachedWriters() const
       
   190 	{
       
   191 	return iAttachedWriterCount;
       
   192 	}
       
   193 
       
   194 TInt CIoEndPoint::FindReader(MIoReader& aReader) const
       
   195 	{
       
   196 	const TInt numReaders = iAttachedReaders.Count();
       
   197 	for (TInt i = 0; i < numReaders; ++i)
       
   198 		{
       
   199 		if (iAttachedReaders[i] == &aReader)
       
   200 			{
       
   201 			return i;
       
   202 			}
       
   203 		}
       
   204 	return KErrNotFound;
       
   205 	}
       
   206 
       
   207 TInt CIoEndPoint::FindReader(const MIoReader& aReader) const
       
   208 	{
       
   209 	return FindReader(const_cast<MIoReader&>(aReader));
       
   210 	}