package com.ericsson.tic.vi.pseudo; import java.io.BufferedReader; import java.net.DatagramSocket; import java.net.SocketException; import java.util.LinkedList; import java.io.InputStreamReader; import java.io.IOException; import java.net.DatagramPacket; import java.lang.InterruptedException; import java.net.InetAddress; import java.util.Random; import com.ericsson.tic.vi.*; /** * This program pretends to be the TIC-prototype. It is used for testing the VI. * This piece of software is NOT part of the deliverable. * Trying to read this code may cause headaches. You have been warned. * * @version 1.0 (2008-12-04) * @author Sami Matilainen */ public class TICTest extends Thread { private int portListen = VI.DEFAULT_UDPOUT_PORT; private int portVI = VI.DEFAULT_UDPIN_PORT; private boolean serverOn = false; static String space = " "; static String nullChar = "\0"; String hostVI = "localhost"; public RawBuffer standardInputBuffer; public StandardInputListener standardInputListener; private int messageInputCapacity = 16; private int standardInputCapacity = 16; private final int MESSAGE_MAX_WIDTH = 128; char[] tempBuffer; static int scenario = 0; static int totalCategories = 6; static int totalNodes = 0; static int totalRelations = 0; public static int totalynodeValues = 8; public int counter = 0; public static int[][][][] xnodeVal; public static int[][][][][] ynodeVal; public static int C; public static int A; public static String[][] movieCategories = { {"blah", "action"}, {"blah", "horror"}, {"blah", "sci-fi"}, {"blah", "comedy"}, {"blah", "western"}, {"blah", "other"} }; public TICTest() { standardInputBuffer = new RawBuffer( MESSAGE_MAX_WIDTH, standardInputCapacity); standardInputListener = new StandardInputListener(standardInputBuffer); //standardInputListener.start(); ServerTimer stimer = new ServerTimer(this); } public void checkInputBuffer(RawBuffer buffer) { buffer.swap(); while (true) { try { tempBuffer = buffer.fetch(); String ret = parse(new String(tempBuffer)); if (ret == null) { // do nothing please } else if (ret.equals("on")) { serverOn = true; } else if (ret.equals("off")) { serverOn = false; } else if (ret.equals("reset")) { reset(); } else { send(ret); } } catch (Exception e) { break; } } } public void run() { while (true) { try { this.sleep(10000); } catch (InterruptedException e) {} } } public void sendMessages() { /* if (scenario == 0) { try { this.sleep(400); } catch (InterruptedException e) { } } */ if (serverOn) { String strA = ""; String strB = ""; String catA = ""; String catB = ""; for (int c = 0; c < C; c++) { for (int a = 0; a < 3; a++) { strA = "xnodestat " + "xnodemanager" + (c+1) + (a+1) + "a"; strB = "xnodestat " + "xnodemanager" + (c+1) + (a+1) + "b"; for (int j = 0; j < totalCategories; j++) { if (j == totalCategories-1) { catA = strA + " " + movieCategories[j][1] + " " + 0.0f; catB = strB + " " + movieCategories[j][1] + " " + 0.0f; if (c == 0 && a == 0) { catA = strA + " " + movieCategories[j][1] + " " + 1.0f; } } else { catA = strA + " " + movieCategories[j][1] + " " + xnodeVal[c][a][j][0]; catB = strB + " " + movieCategories[j][1] + " " + xnodeVal[c][a][j][1]; } // make sure xnodemanager doesn't report zero values! if (xnodeVal[c][a][j][0] != 0) { send(catA); } if (xnodeVal[c][a][j][1] != 0) { send(catB); } try { this.sleep(5); } catch (InterruptedException e) {} } strA = "ynodestat " + "ynodegenerator" + (c+1) + (a+1) + "a"; strB = "ynodestat " + "ynodegenerator" + (c+1) + (a+1) + "b"; for (int j = 0; j < totalCategories; j++) { catA = strA + " " + movieCategories[j][1]; catB = strB + " " + movieCategories[j][1]; for (int i = 0; i < totalynodeValues; i++) { if (j == totalCategories-1) { catA = catA + " " + 0.0f; catB = catB + " " + 0.0f; if (c == 5 && a == 0 && i == 0) { catA = catA + " " + 1.0f; catB = catB + " " + 0.0f; } } else { catA = catA + " " + ynodeVal[c][a][j][i][0]/(i+j+1); catB = catB + " " + ynodeVal[c][a][j][i][1]/(i+j+1); } } try { this.sleep(5); } catch (InterruptedException e) {} send(catA); send(catB); } send("routerutil crouter2_crouter1 " + 0.8f); send("routerdelay crouter3_crouter4 " + 0.8f); send("routertest2d crouter2_crouter4 " + 1.0f + " " + 1.0f); } } } } public void updateValues() { if (counter >= 512) { reset(); counter = 0; } counter++; for (int c = 0; c < C; c++) { for (int a = 0; a < 3; a++) { for (int j = 0; j < totalCategories; j++) { xnodeVal[c][a][j][0] = xnodeVal[c][a][j][0] + 3; xnodeVal[c][a][j][1] = xnodeVal[c][a][j][1] + 3; } for (int j = 0; j < totalCategories; j++) { for (int i = 0; i < totalynodeValues; i++) { ynodeVal[c][a][j][i][0] = ynodeVal[c][a][j][i][0] + 3; ynodeVal[c][a][j][i][1] = ynodeVal[c][a][j][i][1] + 3; } } } } } public void send(String s) { try { sendMessage(s, hostVI, portVI); } catch (SocketException e) { e.printStackTrace(); } } public void sendMessage( String message, String host, int port) throws SocketException { try { DatagramPacket request = new DatagramPacket(message.getBytes(), message.length(), InetAddress.getByName(host), port); DatagramSocket socket = new DatagramSocket(); socket.send(request); } catch (SocketException e) { throw e; } catch (IOException e) { e.printStackTrace(); } } public static void reset() { xnodeVal = new int[C][3][totalCategories][2]; ynodeVal = new int[C][3][totalCategories][totalynodeValues][2]; for (int c = 0; c < C; c++) { for (int a = 0; a < 3; a++) { for (int j = 0; j < totalCategories; j++) { xnodeVal[c][a][j][0] = 0; xnodeVal[c][a][j][1] = 0; } for (int j = 0; j < totalCategories; j++) { for (int i = 0; i < totalynodeValues; i++) { ynodeVal[c][a][j][i][0] = 0; ynodeVal[c][a][j][i][1] = 0; } } } } } public static String parse(String s) { if (s == null || s.equals("")) { return null; } String[] clean = s.split(nullChar); String[] token = clean[0].split(space); String temp = ""; if (token[0].equals("chat")) { if (token.length > 1) { temp = token[1]; for (int i = 2; i < token.length; i++) { temp = temp + " " + token[i]; } return "serverchat " + temp; } else { return null; } } else if (token[0].equals("message")) { if (token.length > 1) { temp = token[1]; for (int i = 2; i < token.length; i++) { temp = temp + " " + token[i]; } return temp; } else { return null; } } else if (token[0].equals("scenario")) { /* if (token[1].equals("scenarioA1")) { PseudoTIC2.scenario = 1; return null; } else if (token[1].equals("scenarioA2")) { PseudoTIC2.scenario = 2; return null; } else if (token[1].equals("scenarioB1")) { PseudoTIC2.scenario = 3; return null; } else if (token[1].equals("scenarioB2")) { PseudoTIC2.scenario = 4; return null; } else if (token[1].equals("scenarioC0")) { PseudoTIC2.scenario = 5; return null; } else if (token[1].equals("scenarioC1")) { PseudoTIC2.scenario = 6; return null; } else if (token[1].equals("scenarioC2")) { PseudoTIC2.scenario = 7; return null; } else { return null; } */ } else if (token[0].equals("reset")) { return "reset"; } else if (token[0].equals("on")) { return "on"; } else if (token[0].equals("off")) { return "off"; } else if (token[0].equals("quit")) { System.exit(0); } return null; } public static void main(String[] args) { if (args.length == 0) { System.out.println(""); System.out.println(" Usage: java com.ericsson.tic.vi.pseudo.TICTest C"); System.out.println(" where C is an integer denoting the number of C-routers. (R = 1, 2, 3, ... 16)"); System.out.println(""); System.exit(0); } else { try { C = Integer.parseInt(args[0]); } catch (NumberFormatException e) { e.printStackTrace(); System.exit(1); } } totalNodes = C*22; totalRelations = C*21+(C*(C-1))/2; A = C*3; reset(); TICTest server = new TICTest(); server.start(); } }