tobicore
7.0.0
Main Page
Modules
Classes
Files
Examples
File List
All
Classes
Functions
Variables
Typedefs
Enumerator
Friends
Groups
Pages
TPInterface.cpp
1
/*
2
Copyright (C) 2009-2011 EPFL (Ecole Polytechnique Fédérale de Lausanne)
3
Michele Tavella <michele.tavella@epfl.ch>
4
5
This program is free software: you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation, either version 3 of the License, or
8
(at your option) any later version.
9
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
14
15
You should have received a copy of the GNU General Public License
16
along with this program. If not, see <http://www.gnu.org/licenses/>.
17
*/
18
19
#ifndef TPINTERFACE_CPP
20
#define TPINTERFACE_CPP
21
22
#include "TPInterface.hpp"
23
24
TPInterface::TPInterface
(
void
) {
25
this->
_socket
= NULL;
26
this->
_endpoint
= NULL;
27
this->
_com
= NULL;
28
}
29
30
TPInterface::~TPInterface
(
void
) {
31
}
32
33
int
TPInterface::Plug
(
const
std::string &ip,
const
std::string& port,
int
mode) {
34
switch
(mode) {
35
case
TPInterface::AsServer
:
36
return
this->ConfAsServer(ip, port);
37
break
;
38
case
TPInterface::AsClient
:
39
return
this->ConfAsClient(ip, port);
40
break
;
41
}
42
return
TPInterface::ErrorGeneric
;
43
}
44
45
void
TPInterface::Unplug
(
void
) {
46
if
(this->
_socket
== NULL)
47
return
;
48
49
this->
_socket
->
Close
();
50
delete
this->
_socket
;
51
this->
_socket
= NULL;
52
53
this->
_com
= NULL;
54
55
if
(this->
_socket
!= NULL) {
56
delete
this->
_endpoint
;
57
this->
_endpoint
= NULL;
58
}
59
}
60
61
62
int
TPInterface::ConfAsServer(
const
std::string &ip,
const
std::string& port) {
63
if
(this->
_socket
!= NULL)
64
return
TPInterface::ErrorBound
;
65
66
bool
status =
true
;
67
this->
_socket
=
new
TPSocket
(
TPSocket::TCP
);
68
status &= this->
_socket
->
Open
(
true
);
69
status &= this->
_socket
->
Bind
(ip, port);
70
status &= this->
_socket
->
Listen
();
71
if
(status ==
false
) {
72
this->
Unplug
();
73
return
TPInterface::ErrorSocket
;
74
}
75
76
this->
_endpoint
=
new
TPSocket
(
TPSocket::TCP
);
77
status = this->
_socket
->
Accept
(this->
_endpoint
);
78
if
(status ==
false
) {
79
this->
Unplug
();
80
return
TPInterface::ErrorEndpoint
;
81
}
82
83
this->
_com
= this->
_endpoint
;
84
return
TPInterface::Successful
;
85
}
86
87
int
TPInterface::ConfAsClient(
const
std::string &ip,
const
std::string& port) {
88
if
(this->
_socket
!= NULL)
89
return
TPInterface::ErrorBound
;
90
91
bool
status =
true
;
92
this->
_socket
=
new
TPSocket
(
TPSocket::TCP
);
93
status &= this->
_socket
->
Open
(
false
);
94
status &= this->
_socket
->
Connect
(ip, port);
95
if
(status ==
false
) {
96
this->
Unplug
();
97
return
TPInterface::ErrorSocket
;
98
}
99
100
this->
_socket
->
IsConnected
();
101
if
(this->
_socket
->
IsConnected
() ==
false
) {
102
this->
Unplug
();
103
return
TPInterface::ErrorSocket
;
104
}
105
106
this->
_com
= this->
_socket
;
107
return
TPInterface::Successful
;
108
}
109
110
bool
TPInterface::IsPlugged(
void
) {
111
if
(this->
_com
== NULL)
112
return
false
;
113
return
this->
_com
->
IsConnected
();
114
}
115
116
#endif
tobiplatform
TPInterface.cpp
Generated on Tue Oct 23 2012 13:15:34 for tobicore by
1.8.1.2