]> git.donarmstrong.com Git - biopieces.git/blob - code_perl/Maasha/BGB/Session.pm
changed BGB session format to JSON
[biopieces.git] / code_perl / Maasha / BGB / Session.pm
1 package Maasha::BGB::Session;
2
3 # Copyright (C) 2009 Martin A. Hansen.
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (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, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 # http://www.gnu.org/copyleft/gpl.html
20
21
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
23
24
25 # Routines for session handling of the Biopieces Browser.
26
27
28 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
29
30
31 use warnings;
32 use strict;
33 use Data::Dumper;
34 use Digest::MD5;
35 use JSON;
36 use Maasha::Common;
37 use Maasha::Filesys;
38
39 use vars qw( @ISA @EXPORT );
40
41 @ISA = qw( Exporter );
42
43
44 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
45
46
47 sub session_new
48 {
49     # Martin A. Hansen, December 2009.
50   
51     # Create a new session id which is md5 hashed.
52
53     # Returns a string.
54
55     my ( $sid );
56
57     $sid = Digest::MD5::md5_hex( Maasha::Common::get_sessionid() );
58
59     return $sid;
60 }
61
62
63 sub session_restore
64 {
65     # Martin A. Hansen, December 2009.
66     
67     # Parses a tab seperated session file and returns the data
68     # as a hash with user as key, and the rest of the columns as
69     # a hash.
70
71     my ( $file,   # session file
72        ) = @_;
73
74     # Returns a hashref.
75
76     my ( $fh, $json, $json_text, $session );
77
78     local $/ = undef;
79
80     $fh = Maasha::Filesys::file_read_open( $file );
81
82     $json_text = <$fh>;
83
84     close $fh;
85
86     $json = new JSON;
87
88     $session = $json->decode( $json_text );
89
90     return wantarray ? %{ $session } : $session;
91 }
92
93
94 sub session_store
95 {
96     # Martin A. Hansen, December 2009.
97
98     # Stores a session hash to file.
99
100     my ( $file,      # file to store in.
101          $session,   # session to store.
102        ) = @_;
103
104     # Returns nothing.
105
106     my ( $json, $json_text, $fh );
107
108     $json = new JSON;
109
110     $json_text = $json->pretty->encode( $session );
111
112     $fh = Maasha::Filesys::file_write_open( $file );
113
114     print $fh $json_text;
115
116     close $fh;
117 }
118
119
120 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
121
122
123 1;
124
125 __END__