summaryrefslogtreecommitdiff
path: root/2005/i/robert/src/socket/test_server.cc
blob: 40224a708063035f456f0b00e994726b33ba0c17 (plain)
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
// test_socket.cc
// robert - programme du robot 2005. {{{
//
// Copyright (C) 2005 Dufour J�r�my
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// Contact :
//        Web: %WEB%
//      Email: <dufourj@efrei.fr>
// }}}
#include "server_socket.hh"
#include "address.hh"
#include "socket_text.hh"
#include "data/data_input_file.hh"
#include "data/data_buffer.hh"
#include "image/raw_reader.hh"
#include "image/image.hh"

#include <iostream>
#include <fstream>
#include <stdexcept>

int
main (int argc, char **argv)
{
    if (argc == 3)
      {
	try 
	  {
	    // Serveur
	    ServerSocket ss (atoi (argv[1]));
	    Address a;
	    SocketText st (ss, a);
	    st.nonblock ();
	    // DataBuffer de reception
	    DataBuffer db;
	    // Reception des donn�es
	    while (!st.recv ());
	    st >> db;
	    if (db.type () != DataBuffer::Image)
		throw std::runtime_error ("Echec de la transmission");
	    RawReader reader (db, 360, 296, Image::rgb);
	    Image img (360, 296, Image::rgb);
	    img.read (reader);
 	    std::ofstream file (argv[2]);
 	    file.write ((char*) img.getBuf (), img.getBufSize ());
	    
// 	    // R�cup�ration des donn�es du DataBuffer
// 	    std::vector <char> v (db.size ());
// 	    db.read ((uint8_t*) &v[0], db.size ());
// 	    // Ecriture dans le fichier
// 	    std::ofstream file (argv[2]);
// 	    file.write (&v[0], v.size ());
// 	    DataBuffer db1;
// 	    // Reception des donn�es
// 	    while (!st.recv ());
// 	    st >> db1;
// 	    std::cout << ">> Re�u " << db1.size () << " bits." << std::endl;
// 	    if (db1.type () != DataBuffer::Image)
// 		throw std::runtime_error ("Echec de la transmission");
	  }
	catch (const std::runtime_error &r)
	  {
	    std::cerr << argv[0] << ": " << r.what () << std::endl;
	    return 1;
	  }
      }
    else
      {
	std::cerr << "Syntaxe: " << argv[0] << " port out_file" << std::endl;
	return 1;
      }
    return 0;
}