source: trunk/gui/vizservers/nanovis/socket/ServerSocket.cpp @ 226

Last change on this file since 226 was 226, checked in by mmc, 18 years ago
  • Added code for Wei's visualization server.
  • Fixed the energyLevels widget so that it doesn't barf when the user attempts to download its contents.
File size: 2.4 KB
Line 
1/*
2 * ----------------------------------------------------------------------
3 * Implementation of the ServerSocket class
4 *
5 * ======================================================================
6 *  AUTHOR:  Wei Qiao <qiaow@purdue.edu>
7 *           Purdue Rendering and Perceptualization Lab (PURPL)
8 *
9 *  Copyright (c) 2004-2006  Purdue Research Foundation
10 *
11 *  See the file "license.terms" for information on usage and
12 *  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 * ======================================================================
14 */
15#include "ServerSocket.h"
16#include "SocketException.h"
17
18
19ServerSocket::ServerSocket ( int port )
20{
21  connected = false;
22  if ( ! Socket::create() )
23    {
24      throw SocketException ( "Could not create server socket." );
25    }
26
27  if ( ! Socket::bind ( port ) )
28    {
29      throw SocketException ( "Could not bind to port." );
30    }
31
32  if ( ! Socket::listen() )
33    {
34      throw SocketException ( "Could not listen to socket." );
35    }
36
37}
38
39ServerSocket::~ServerSocket()
40{
41  connected = false;
42}
43
44
45const ServerSocket& ServerSocket::operator << ( const std::string& s ) const
46{
47  if ( ! Socket::send ( s ) )
48    {
49      throw SocketException ( "Could not write to socket." );
50    }
51
52  return *this;
53
54}
55
56
57const ServerSocket& ServerSocket::operator >> ( std::string& s ) const
58{
59  if ( ! Socket::recv ( s ) )
60    {
61      throw SocketException ( "Could not read from socket." );
62    }
63
64  return *this;
65}
66
67bool ServerSocket::send (char* s, int size) const
68{
69  //printf("ServerSocket::send: 0\n");
70  bool ret = Socket::send (s, size);
71  //printf("ServerSocket::send: 1\n");
72  if (!ret)
73    {
74  //printf("ServerSocket::send: 2\n");
75      throw SocketException ( "Could not write to socket." );
76    }
77
78  return ret;
79}
80
81int ServerSocket::recv ( char* s, int size) const
82{
83  bool ret = Socket::recv (s, size);
84  if (!ret)
85    {
86      throw SocketException ( "Could not read from socket." );
87    }
88
89  return ret;
90}
91
92bool ServerSocket::accept ( ServerSocket& sock )
93{
94/*
95  if ( ! Socket::accept ( sock ) )
96    {
97      throw SocketException ( "Could not accept socket." );
98    }
99*/
100   bool ret =  Socket::accept( sock );
101   sock.set_connected(ret);
102   return ret;
103}
104
105void ServerSocket::set_non_blocking(bool val){
106        Socket::set_non_blocking(val);
107}
108
109bool ServerSocket::is_connected(){
110        return connected;
111}
112
113bool ServerSocket::set_connected(bool val){
114        return connected = val;
115}
Note: See TracBrowser for help on using the repository browser.