1 /*******************************************************************************
2   aiounicast.hh, base class for asynchronous unicast transmission of mpz_t
3 
4    This file is part of LibTMCG.
5 
6  Copyright (C) 2016, 2017, 2018  Heiko Stamer <HeikoStamer@gmx.net>
7 
8    LibTMCG is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12 
13    LibTMCG is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16    GNU General Public License for more details.
17 
18    You should have received a copy of the GNU General Public License
19    along with LibTMCG; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 *******************************************************************************/
22 
23 #ifndef INCLUDED_aiounicast_HH
24 	#define INCLUDED_aiounicast_HH
25 
26 // C and STL header
27 #include <cstdlib>
28 #include <ctime>
29 #include <vector>
30 #include <map>
31 #include <iostream>
32 
33 // GNU multiple precision library
34 #include <gmp.h>
35 
36 class aiounicast
37 {
38 	protected:
39 		const size_t				aio_default_scheduler;
40 		const time_t				aio_default_timeout;
41 		const bool					aio_is_authenticated;
42 		const bool					aio_is_encrypted;
43 		bool						aio_is_initialized;
44 		mpz_t						aio_hide_length;
45 
46 	public:
47 		static const time_t			aio_timeout_none			= 0;
48 		static const time_t			aio_timeout_extremely_short	= 1;
49 		static const time_t			aio_timeout_very_short		= 5;
50 		static const time_t			aio_timeout_short			= 15;
51 		static const time_t			aio_timeout_middle			= 30;
52 		static const time_t			aio_timeout_long			= 90;
53 		static const time_t			aio_timeout_very_long		= 180;
54 		static const time_t			aio_timeout_extremely_long	= 300;
55 		static const time_t			aio_timeout_default			= 42424242;
56 		static const size_t			aio_scheduler_none			= 0;
57 		static const size_t			aio_scheduler_roundrobin	= 1;
58 		static const size_t			aio_scheduler_random		= 2;
59 		static const size_t			aio_scheduler_direct		= 3;
60 		static const size_t			aio_scheduler_default		= 42424242;
61 
62 		const size_t				n;
63 		const size_t				j;
64 
65 		std::map<size_t, int>		fd_in, fd_out;
66 		size_t						numWrite, numRead;
67 		size_t						numEncrypted, numDecrypted;
68 		size_t						numAuthenticated;
69 
aiounicast(const size_t n_in,const size_t j_in,const size_t aio_default_scheduler_in=aio_scheduler_roundrobin,const time_t aio_default_timeout_in=aio_timeout_very_long,const bool aio_is_authenticated_in=true,const bool aio_is_encrypted_in=true)70 		aiounicast
71 			(const size_t n_in,
72 			 const size_t j_in,
73 			 const size_t aio_default_scheduler_in = aio_scheduler_roundrobin,
74 			 const time_t aio_default_timeout_in = aio_timeout_very_long,
75 			 const bool aio_is_authenticated_in = true,
76 			 const bool aio_is_encrypted_in = true):
77 				aio_default_scheduler(aio_default_scheduler_in),
78 				aio_default_timeout(aio_default_timeout_in),
79 				aio_is_authenticated(aio_is_authenticated_in),
80 				aio_is_encrypted(aio_is_encrypted_in),
81 				aio_is_initialized(true),
82 				n(n_in), j(j_in), numWrite(0), numRead(0), numEncrypted(0),
83 				numDecrypted(0), numAuthenticated(0)
84 		{
85 			mpz_init_set_ui(aio_hide_length, 1L);
86 			mpz_mul_2exp(aio_hide_length, aio_hide_length, TMCG_AIO_HIDE_SIZE);
87 		}
88 
89 		virtual bool Send
90 			(mpz_srcptr m,
91 			 const size_t i_in,
92 			 const time_t timeout = aio_timeout_default) = 0;
93 		virtual bool Send
94 			(const std::vector<mpz_srcptr> &m,
95 			 const size_t i_in,
96 			 const time_t timeout = aio_timeout_default) = 0;
97 		virtual bool Receive
98 			(mpz_ptr m,
99 			 size_t &i_out,
100 			 const size_t scheduler = aio_scheduler_default,
101 			 const time_t timeout = aio_timeout_default) = 0;
102 		virtual bool Receive
103 			(std::vector<mpz_ptr> &m,
104 			 size_t &i_out,
105 			 const size_t scheduler = aio_scheduler_default,
106 			 const time_t timeout = aio_timeout_default) = 0;
107 
PrintStatistics(std::ostream & ost)108 		void PrintStatistics
109 			(std::ostream &ost)
110 		{
111 			ost << " numRead = " << numRead << " numWrite = " << numWrite <<
112 				" numEncrypted = " << numEncrypted <<
113 				" numDecrypted = " << numDecrypted <<
114 				" numAuthenticated = " << numAuthenticated;
115 		}
116 
~aiounicast()117 		virtual ~aiounicast
118 			()
119 		{
120 			mpz_clear(aio_hide_length);
121 		}
122 };
123 
124 #endif
125 
126