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
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkSocketCollection.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkSocketCollection.h"
#include "vtkCollectionIterator.h"
#include "vtkObjectFactory.h"
#include "vtkSocket.h"
vtkStandardNewMacro(vtkSocketCollection);
//-----------------------------------------------------------------------------
vtkSocketCollection::vtkSocketCollection()
{
this->SelectedSocket = 0;
}
//-----------------------------------------------------------------------------
vtkSocketCollection::~vtkSocketCollection()
{
}
//-----------------------------------------------------------------------------
void vtkSocketCollection::AddItem(vtkSocket* soc)
{
this->Superclass::AddItem(soc);
}
//-----------------------------------------------------------------------------
int vtkSocketCollection::SelectSockets(unsigned long msec /*=0*/)
{
// clear last selected socket.
this->SelectedSocket = 0;
int max = this->GetNumberOfItems();
if (max <= 0)
{
vtkErrorMacro("No sockets to select.");
return -1;
}
int* socket_indices = new int[max];
int* sockets_to_select = new int[max];
int no_of_sockets = 0;
vtkCollectionIterator* iter = this->NewIterator();
int index = 0;
for (iter->InitTraversal(); !iter->IsDoneWithTraversal();
iter->GoToNextItem(), index++)
{
vtkSocket* soc = vtkSocket::SafeDownCast(iter->GetCurrentObject());
if (!soc->GetConnected())
{
// skip not-connected sockets.
continue;
}
int sockfd = soc->GetSocketDescriptor();
sockets_to_select[no_of_sockets] = sockfd;
socket_indices[no_of_sockets] = index;
no_of_sockets++;
}
if (no_of_sockets == 0)
{
vtkErrorMacro("No alive sockets!");
delete []sockets_to_select;
delete []socket_indices;
return -1;
}
int res = vtkSocket::SelectSockets(sockets_to_select, no_of_sockets,
msec, &index);
int actual_index = -1;
if (index != -1)
{
actual_index = socket_indices[index];
}
iter->Delete();
delete []sockets_to_select;
delete []socket_indices;
if (res <= 0 || index == -1)
{
return res;
}
this->SelectedSocket = vtkSocket::SafeDownCast(
this->GetItemAsObject(actual_index));
return 1;
}
//-----------------------------------------------------------------------------
void vtkSocketCollection::RemoveItem(vtkObject* a)
{
if (this->SelectedSocket && this->SelectedSocket == a)
{
this->SelectedSocket = 0;
}
this->Superclass::RemoveItem(a);
}
//-----------------------------------------------------------------------------
void vtkSocketCollection::RemoveItem(int i)
{
if (this->SelectedSocket && this->GetItemAsObject(i) == this->SelectedSocket)
{
this->SelectedSocket = 0;
}
this->Superclass::RemoveItem(i);
}
//-----------------------------------------------------------------------------
void vtkSocketCollection::ReplaceItem(int i, vtkObject* a)
{
if (this->SelectedSocket && this->GetItemAsObject(i) == this->SelectedSocket)
{
this->SelectedSocket = 0;
}
this->Superclass::ReplaceItem(i, a);
}
//-----------------------------------------------------------------------------
void vtkSocketCollection::RemoveAllItems()
{
this->SelectedSocket = 0;
this->Superclass::RemoveAllItems();
}
//-----------------------------------------------------------------------------
void vtkSocketCollection::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
|