Обеспечение всемирной трансляции спортивных шахматных соревнований с применением разработанного в ходе проекта законченного программного продукта

Дипломная работа - Компьютеры, программирование

Другие дипломы по предмету Компьютеры, программирование

t;meta http-equiv=refresh content=4>\n\n");

Snapshot of Chessboard: ");

s.append(Integer.toString(ss.board.get_address()));

\n");

s.append(ss.get_time().toString());

\n");">s.append("\n");

s.append(boardview(ss));

\n\n");

 

if (writefile(snapshotprefix + Integer.toString(ss.board.get_address()) + extension, s.toString()) == false) {

System.err.println("Error: Failed to write snapshotfile.");

return;

}

}

 

static boolean writefile(String filename, String content) {

FileWriter f = null;

for (int i=0; i<10; i=i+1) {

try {

f = new FileWriter(filename);

break;

}

catch (Throwable t) {

System.err.println("Error opening "+filename+" for writing. Retrying...");

}

}

if (f==null) {

System.out.println("Failed to open file "+filename);

return false;

}

try {

f.write(content);

f.close();

}

catch (Exception e) {

System.out.println("Error writing file "+filename+". ("+e.toString()+")");

return false;

}

return true;

}

}

 

rdgtInterpreter.java

---

class rdgtInterpreter extends rdgtProtocol {

 

rdgtDatabase db;

public rdgtInterpreter(rdgtDatabase _db) {

db = _db;

}

 

public boolean interpret(int[] data) {

int len = ((int) data[1] * 0x80) + (int) data[2];

int address = ((int) data[3] * 0x80) + (int) data[4];

String tmp = "Len:" + Integer.toString(len) + " Address:" + Integer.toString(address);

 

if (data[0] == DGT_BUS_PING) {

System.out.println("Received message: PING " + tmp);

} else if (data[0] == DGT_BUS_UPDATE) {

System.out.println("Received message: UPDATE " + tmp);

} else if (data[0] == DGT_BUS_BWTIME) {

System.out.println("Received message: BWTIME " + tmp);

} else {

System.out.println("Received message: (some other message) " + tmp);

}

 

if (check_checksum(data) == false) {

System.out.println("Message failed checksum test");

return false;

}

 

if (data[0] == DGT_BUS_PING) {

if (data.length != 6) { // address+data

System.out.println("Illegal: Got a BUS_PING with wrong size");

return false;

}

db.get_board(address).set_alive(true);

return true;

} else if (data[0] == DGT_BUS_BRD_DUMP) {

if (data.length != (6 + 64)) {

System.out.println("Illegal: Got a BUS_BRD_DUMP with wrong size");

return false;

}

rdgtChessboard c = db.get_board(address);

c.set_boarddump(data, 5);

return true;

} else if (data[0] == DGT_BUS_UPDATE) {

 

byte pr[] = new byte[data.length - 5];

for (int i = 5; i < data.length; i++) {

pr[i - 5] = (byte) data[i];

}

System.out.println(new String(pr));

 

rdgtChessboard c = db.get_board(address);

for (int i = 5; i < data.length - 1; i++) {

if (data[i] == EE_POWERUP) {

}

else if (data[i] == EE_EOF) {

}

else if (data[i] == EE_FOURROWS) {

}

else if (data[i] == EE_EMPTYBOARD) {

c.set_emptyboard();

} else if (data[i] == EE_DOWNLOADED) {

}

else if (data[i] == EE_BEGINPOS) {

}

else if (data[i] == EE_BEGINPOS_ROT) {

}

else if (data[i] == EE_START_TAG) {

}

else if (data[i] == EE_WATCHDOG_ACTION) {

}

else if (data[i] == EE_NOP) {

}

else if (data[i] >= 0x40 && data[i] <= 0x5F) { // 2-byte piece update

c.set_fieldupdate(data[i] & 0x0F, data[i + 1]);

i++;

} else if ((data[i] >= 0x60 && data[i] = 0x70 && data[i] <= 0x79)) {

i += 2;

} else {

System.out.println("Unknown data in DGT_BUS_UPDATE command.");

return false;

}

}

c.set_updated(true);

return true;

}

else if (data[0] == DGT_BUS_BWTIME) {

 

if (data.length != 13) {

System.out.println("Illegal: Got a DGT_BUS_BWTIME with wrong size (!=10)");

return false;

}

 

if ((data[11] & 0x20) != 0) {

System.out.println("(No clock connected)");

return true;

}

 

boolean running = (data[11] & 0x01) != 0;

boolean batteryLow = (data[11] & 0x04) != 0;

 

boolean frontViewLeftSideHigh = (data[11] & 0x02) != 0;

boolean blackPlayersTurn = (data[11] & 0x08) != 0;

boolean whitePlayersTurn = (data[11] & 0x10) != 0;

 

int hoursW = data[5] & 0x0F;

int minutesW = (data[6] & 0x0F) + ((data[6] >> 4) * 10);

int secondsW = (data[7] & 0x0F) + ((data[7] >> 4) * 10);

int hoursB = data[8] & 0x0F;

int minutesB = (data[9] & 0x0F) + ((data[9] >> 4) * 10);

int secondsB = (data[10] & 0x0F) + ((data[10] >> 4) * 10);

 

secondsW = (((hoursW * 60) + minutesW) * 60) + secondsW;

secondsB = (((hoursB * 60) + minutesB) * 60) + secondsB;

db.get_board(address).set_clockdata(running, batteryLow, frontViewLeftSideHigh,

blackPlayersTurn, whitePlayersTurn, secondsW, secondsB);

return true;

} else if (data[0] == DGT_BUS_FROM_START) {

} else if (data[0] == DGT_BUS_START_GAME_WRITTEN) {

} else if (data[0] == DGT_BUS_VERSION) {

}

System.err.println("Unknown message (checksum was ok).");

return true;

}

 

boolean check_checksum(int[] data) {

int sum = 0;

for (int i = 0; i < (data.length - 1); i = i + 1) {

sum = (sum + data[i]) & 0x7F;

}

if (sum == data[data.length - 1]) {

return true;

}

return false;

}

}

rdgtMysql.java

---

class rdgtMysql extends rdgtProtocol {

 

static String server= "localhost";

static String user= "root";

static String pass= "zar1562";

static String dbase= "rdgt_development";

 

static String update_table= "games";

 

static Connection conn = null;

static Statement stmt = null;

 

static boolean rotated=false;

 

static String connect()

{

try

{

Class.forName("com.mysql.jdbc.Driver").newInstance();

conn = DriverManager.getConnection("jdbc:mysql://"+server+"/"+dbase+"?user="+user+"&password="+pass);

return "";

}

catch (SQLException e)

{

return e.getSQLState();

}

catch (Exception e)

{

return "0";

}

}

 

static boolean disconnect()

{

if (conn != null) {

try

{

conn.close();

return true;

}

catch (SQLException SQLE)

{

return false;

}

}

else

return false;

}

 

static String execute(String expr)

{

try

{

stmt = null;

stmt = conn.createStatement();

stmt.executeUpdate(expr);

return "";

}

catch (SQLException e)

{

return e.getSQLState();

}

catch (Exception e)

{

return "0";

}

finally

{

if (stmt != null) {

try

{

stmt.close();

}

catch (SQLException SQLE)

{

;

}

}

}

}

 

static String snapshot2fen (rdgtSnapshot b)

{

StringBuffer s = new StringBuffer(100);

int counter = 0;

for (int i=0; i<8; i++)

{

for (int j=0; j<8; j++)

{

if ((counter != 0) && (b.pieces[(i*8)+j] != EMPTY))

{

s.append(counter);

counter = 0;

}

switch(b.pieces[(i*8)+j])

{

case WPAWN:s.append("P"); break;

case WROOK: s.append("R"); break;

case WKNIGHT: s.append("N"); break;

case WBISHOP: s.append("B"); break;

case WKING: s.append("K"); break;

case WQUEEN: s.append("Q"); break;

case BPAWN: s.append("p"); break;

case BROOK: s.append("r"); break;

case BKNIGHT: s.append("n"); break;