001 /**
002 * Jetrix TetriNET Server
003 * Copyright (C) 2001-2003 Emmanuel Bourg
004 *
005 * This program is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU General Public License
007 * as published by the Free Software Foundation; either version 2
008 * of the License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License
016 * along with this program; if not, write to the Free Software
017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018 */
019
020 package net.jetrix.tools.patcher;
021
022 import java.io.*;
023 import java.net.*;
024 import java.util.*;
025 import java.util.zip.*;
026
027 /**
028 * Jetrix Update - downloads new files from the patch server.
029 *
030 * @author Emmanuel Bourg
031 * @version $Revision: 794 $, $Date: 2009-02-17 20:08:39 +0100 (Tue, 17 Feb 2009) $
032 */
033 public class JetrixUpdate
034 {
035 private List<String> update = new ArrayList<String>();
036 private String basedir = "http://tetrinet.lfjr.net/jetrix/autoupdate/"; // should read this from a property file
037 private String newsFileName = "news.txt";
038
039 private boolean downloadFailed = false;
040 private boolean displayNews = false;
041
042 public static void main(String[] argv) throws Exception
043 {
044 JetrixUpdate jetrixUpdate = new JetrixUpdate();
045 jetrixUpdate.run();
046 }
047
048 public void run() throws Exception
049 {
050 getUpdate();
051
052 for (int i = 0; i < update.size(); i++)
053 {
054 StringTokenizer st = new StringTokenizer(update.get(i), " \t");
055 String fileName = st.nextToken();
056 long chksum = Long.parseLong(st.nextToken());
057
058 long localSum = getFileCRC32(fileName);
059
060 if (chksum != localSum)
061 {
062 downloadFile(fileName, chksum);
063 if (fileName.equals(newsFileName))
064 {
065 displayNews = true;
066 }
067 }
068 }
069
070 if (displayNews)
071 {
072 displayNews();
073 }
074
075 if (downloadFailed)
076 {
077 System.out.println("\nDownload failed. Please run again this update.\nContact the update server administrator if the problem persists.");
078 }
079 else
080 {
081 System.out.println("\nUpdate completed");
082 }
083 }
084
085
086 private void getUpdate() throws IOException
087 {
088 URL updateList = new URL(basedir + "update.crc");
089
090 System.out.println("Connecting to update server...");
091
092 HttpURLConnection conn = (HttpURLConnection) updateList.openConnection();
093 BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
094
095 System.out.println("Reading update file...");
096
097 String line = br.readLine();
098
099 while (line != null)
100 {
101 update.add(line);
102 line = br.readLine();
103 }
104 }
105
106
107 private void downloadFile(String filename, long remoteFileCRC) throws IOException
108 {
109 URL updateList = new URL(basedir + filename.replace('\\', '/'));
110
111 HttpURLConnection conn = (HttpURLConnection) updateList.openConnection();
112 BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
113
114 File localFile = new File(filename + ".new");
115 File parent = localFile.getParentFile();
116 if (parent != null)
117 {
118 parent.mkdirs();
119 }
120
121 FileOutputStream fos = new FileOutputStream(localFile);
122
123 System.out.print("Receiving " + filename);
124
125 int b = bis.read();
126
127 while (b != -1)
128 {
129 fos.write(b);
130 b = bis.read();
131 }
132
133 fos.close();
134 bis.close();
135
136 // CRC Check
137 long downloadedFileCRC = getFileCRC32(localFile);
138
139 String blank = new String(new char[50 - filename.length()]);
140
141 if (downloadedFileCRC != remoteFileCRC)
142 {
143 System.out.println(blank + " [FAILED]");
144 downloadFailed = true;
145 localFile.delete();
146 }
147 else
148 {
149 System.out.println(blank + " [ OK ]");
150 localFile.renameTo(new File(filename));
151 }
152 }
153
154 /**
155 * Return the CRC32 value of the specified file.
156 */
157 private long getFileCRC32(File f) throws IOException
158 {
159 if (f.exists() && f.isFile())
160 {
161 FileInputStream fis = new FileInputStream(f);
162
163 CRC32 check = new CRC32();
164
165 int b = fis.read();
166
167 while (b != -1)
168 {
169 b = fis.read();
170 check.update(b);
171 }
172
173 fis.close();
174
175 return check.getValue();
176 }
177 else
178 {
179 return 0;
180 }
181 }
182
183 /**
184 * Return the CRC32 value of the specified file.
185 */
186 private long getFileCRC32(String filename) throws IOException
187 {
188 File f = new File(filename);
189 return getFileCRC32(f);
190 }
191
192 /**
193 * Display the content of the news file.
194 */
195 private void displayNews() throws IOException
196 {
197 System.out.println("\n");
198
199 BufferedReader br = new BufferedReader(new FileReader(newsFileName));
200
201 String line = br.readLine();
202
203 while (line != null)
204 {
205 System.out.println(line);
206 line = br.readLine();
207 }
208
209 br.close();
210 }
211
212 }